PreviewSceneManager has Prefab

This commit is contained in:
ousttrue 2018-04-08 01:43:37 +09:00
parent 49916176e2
commit f3947ffbd6
4 changed files with 66 additions and 44 deletions

View File

@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
@ -34,18 +32,7 @@ namespace VRM
public static void DrawElement(Rect position, SerializedProperty property,
PreviewSceneManager scene)
{
/*
for (var depth = property.depth;
property.NextVisible(true) && property.depth > depth;
)
{
{
var height = EditorGUI.GetPropertyHeight(property);
EditorGUI.PropertyField(new Rect(position.x, y, position.width, height), property, false);
y += height;
}
}
*/
if (scene == null) return;
var height = 16;
var y = position.y;
@ -63,6 +50,8 @@ namespace VRM
static int StringPopup(Rect rect, SerializedProperty prop, string[] options)
{
if (options == null) { return -1; }
var oldIndex = Array.IndexOf(options, prop.stringValue);
var newIndex = EditorGUI.Popup(rect, oldIndex, options);
if (newIndex != oldIndex && newIndex >= 0 && newIndex < options.Length)
@ -72,14 +61,17 @@ namespace VRM
return newIndex;
}
static void IntPopup(Rect rect, SerializedProperty prop, string[] options)
static int IntPopup(Rect rect, SerializedProperty prop, string[] options)
{
if (options == null){ return -1; }
var oldIndex = prop.intValue;
var newIndex = EditorGUI.Popup(rect, oldIndex, options);
if (newIndex != oldIndex && newIndex >= 0 && newIndex < options.Length)
{
prop.intValue = newIndex;
}
return newIndex;
}
static void FloatSlider(Rect rect, SerializedProperty prop, float maxValue)

View File

@ -12,6 +12,33 @@ namespace VRM
PreviewSceneManager m_scene;
PreviewFaceRenderer m_renderer;
GameObject m_prefab;
GameObject Prefab
{
get { return m_prefab; }
set
{
if (m_prefab == value) return;
m_prefab = value;
if (m_scene != null)
{
//Debug.LogFormat("OnDestroy");
GameObject.DestroyImmediate(m_scene.gameObject);
m_scene = null;
}
if (m_prefab != null)
{
m_scene = PreviewSceneManager.GetOrCreate(m_prefab);
if (m_scene != null)
{
m_scene.gameObject.SetActive(false);
}
}
}
}
#region for Editor
SerializedProperty m_BlendShapeNameProp;
SerializedProperty m_PresetProp;
@ -52,8 +79,10 @@ namespace VRM
m_renderer = new PreviewFaceRenderer();
var assetPath = AssetDatabase.GetAssetPath(target);
m_scene = PreviewSceneManager.GetOrCreate(assetPath);
m_scene.gameObject.SetActive(false);
if (!string.IsNullOrEmpty(assetPath))
{
Prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
}
}
private void OnDisable()
@ -77,10 +106,7 @@ namespace VRM
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
//攻撃力の数値をラベルとして表示する
//EditorGUILayout.LabelField("攻撃力", character.攻撃力.ToString());
m_prefab = (GameObject)EditorGUILayout.ObjectField("prefab", m_prefab, typeof(GameObject), false);
serializedObject.Update();
@ -114,17 +140,20 @@ namespace VRM
return;
}
if (Event.current.type != EventType.Repaint)
{ // if we don't need to update yet, then don't
return;
}
if (m_renderer == null)
{
// if we don't need to update yet, then don't
return;
}
var image = m_renderer.Render(r, background, m_scene);
GUI.DrawTexture(r, image, ScaleMode.StretchToFill, false); // draw the RenderTexture in the ObjectPreview pane
if (m_renderer != null && m_scene != null)
{
var image = m_renderer.Render(r, background, m_scene);
if (image != null)
{
// draw the RenderTexture in the ObjectPreview pane
GUI.DrawTexture(r, image, ScaleMode.StretchToFill, false);
}
}
EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40f),
BlendShapeKey.CreateFrom((BlendShapeClip)target).ToString());

View File

@ -26,6 +26,8 @@ namespace VRM
public Texture Render(Rect r, GUIStyle background, PreviewSceneManager scene)
{
if (scene == null) return null;
// we are technically rendering everything in the scene, so scene fog might affect it...
bool fog = RenderSettings.fog; // ... let's remember the current fog setting...
Unsupported.SetRenderSettingsUseFogNoDirty(false); // ... and then temporarily turn it off
@ -34,7 +36,7 @@ namespace VRM
m_previewUtility.BeginPreview(r, background); // set up the PreviewRenderUtility's mini internal scene
// setup the ObjectPreview's camera
scene.SetupCamera(m_previewUtility.m_Camera);
scene.SetupCamera(m_previewUtility.camera);
foreach (var item in scene.EnumRenderItems)
{
@ -51,7 +53,7 @@ namespace VRM
}
// VERY IMPORTANT: this manually tells the camera to render and produce the render texture
m_previewUtility.m_Camera.Render();
m_previewUtility.camera.Render();
// reset the scene's fog from before
return m_previewUtility.EndPreview(); // grab the RenderTexture resulting from DoRenderPreview() > RenderMeshPreview() > PreviewRenderUtility.m_Camera.Render()

View File

@ -17,18 +17,23 @@ namespace VRM
/// </summary>
public class PreviewSceneManager : MonoBehaviour
{
public string AssetPath;
public GameObject Prefab;
#if UNITY_EDITOR
public static PreviewSceneManager GetOrCreate(string assetPath)
public static PreviewSceneManager GetOrCreate(GameObject prefab)
{
if (prefab == null)
{
return null;
}
PreviewSceneManager manager = null;
// if we already instantiated a PreviewInstance previously but just lost the reference, then use that same instance instead of making a new one
var managers = GameObject.FindObjectsOfType<PreviewSceneManager>();
foreach (var x in managers)
{
if (x.AssetPath == assetPath)
if (x.Prefab == prefab)
{
Debug.LogFormat("find {0}", manager);
return manager;
@ -37,12 +42,6 @@ namespace VRM
GameObject.DestroyImmediate(x.gameObject);
}
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
if (prefab == null)
{
return null;
}
// no previous instance detected, so now let's make a fresh one
// very important: this loads the PreviewInstance prefab and temporarily instantiates it into PreviewInstance
var go = GameObject.Instantiate(prefab,
@ -51,7 +50,7 @@ namespace VRM
);
go.name = "__PREVIEW_SCENE_MANGER__";
manager = go.AddComponent<PreviewSceneManager>();
manager.Initialize(assetPath);
manager.Initialize(prefab);
// HideFlags are special editor-only settings that let you have *secret* GameObjects in a scene, or to tell Unity not to save that temporary GameObject as part of the scene
go.hideFlags |= HideFlags.DontSaveInEditor;
@ -61,15 +60,15 @@ namespace VRM
go.hideFlags |= HideFlags.HideAndDontSave;
; // you could also hide it from the hierarchy or inspector, but personally I like knowing everything that's there
#endif
//Debug.LogFormat("Create {0}", manager);
Debug.LogFormat("Create {0}", manager);
return manager;
}
#endif
private void Initialize(string assetPath)
private void Initialize(GameObject prefab)
{
AssetPath = assetPath;
Prefab = prefab;
var flags = BindingFlags.Static | BindingFlags.NonPublic;
var propInfo = typeof(Camera).GetProperty("PreviewCullingLayer", flags);
@ -227,7 +226,7 @@ namespace VRM
Bounds m_bounds;
public void Bake()
{
//Debug.LogFormat("Bake");
Debug.LogFormat("Bake");
m_bounds = default(Bounds);
foreach (var x in m_meshes)
{