From 1e958ae4088be02be4b61270e8c521cbaf1a7612 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 18 Jun 2021 18:52:14 +0900 Subject: [PATCH 1/2] impl custom editor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit renderer を pulldown で選択できるようになった --- .../RendererFirstPersonFlagsDrawer.cs | 31 ++++++++--- .../Components/Expression/VRM10Expression.cs | 38 +------------- .../PrefabRelatedScriptableObject.cs | 52 +++++++++++++++++++ .../PrefabRelatedScriptableObject.cs.meta | 11 ++++ .../Components/VRM10Object/VRM10Object.cs | 2 +- 5 files changed, 90 insertions(+), 44 deletions(-) create mode 100644 Assets/VRM10/Runtime/Components/PrefabRelatedScriptableObject.cs create mode 100644 Assets/VRM10/Runtime/Components/PrefabRelatedScriptableObject.cs.meta diff --git a/Assets/VRM10/Editor/Components/FirstPerson/RendererFirstPersonFlagsDrawer.cs b/Assets/VRM10/Editor/Components/FirstPerson/RendererFirstPersonFlagsDrawer.cs index 6020db328..714cefd5d 100644 --- a/Assets/VRM10/Editor/Components/FirstPerson/RendererFirstPersonFlagsDrawer.cs +++ b/Assets/VRM10/Editor/Components/FirstPerson/RendererFirstPersonFlagsDrawer.cs @@ -1,5 +1,7 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEditor; using UnityEngine; @@ -11,22 +13,39 @@ namespace UniVRM10 { static Rect LeftSide(Rect position, float width) { - return new Rect(position.x, position.y, position.width-width, position.height); + return new Rect(position.x, position.y, position.width - width, position.height); } static Rect RightSide(Rect position, float width) { - return new Rect(position.x + (position.width-width), position.y, width, position.height); + return new Rect(position.x + (position.width - width), position.y, width, position.height); } + const float WIDTH = 140.0f; + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var rendererProp = property.FindPropertyRelative("Renderer"); var flagProp = property.FindPropertyRelative("FirstPersonFlag"); - const float WIDTH = 140.0f; - EditorGUI.PropertyField(LeftSide(position, WIDTH), rendererProp, new GUIContent(""), true); - EditorGUI.PropertyField(RightSide(position, WIDTH), flagProp, new GUIContent(""), true); + var root = (property.serializedObject.targetObject as VRM10Object).Prefab; + if (root != null) + { + var renderers = root.GetComponentsInChildren(); + var paths = renderers.Select(x => x.transform.RelativePathFrom(root.transform)).ToArray(); + var selected = Array.IndexOf(paths, rendererProp.stringValue); + var newSelect = EditorGUI.Popup(LeftSide(position, WIDTH), selected, renderers.Select(x => x.name).ToArray()); + if (newSelect != selected && newSelect != -1) + { + rendererProp.stringValue = paths[newSelect]; + } + EditorGUI.PropertyField(RightSide(position, WIDTH), flagProp, new GUIContent(""), true); + } + else + { + EditorGUI.PropertyField(LeftSide(position, WIDTH), rendererProp, new GUIContent(""), true); + EditorGUI.PropertyField(RightSide(position, WIDTH), flagProp, new GUIContent(""), true); + } } /* diff --git a/Assets/VRM10/Runtime/Components/Expression/VRM10Expression.cs b/Assets/VRM10/Runtime/Components/Expression/VRM10Expression.cs index 135e7e945..45fe2159f 100644 --- a/Assets/VRM10/Runtime/Components/Expression/VRM10Expression.cs +++ b/Assets/VRM10/Runtime/Components/Expression/VRM10Expression.cs @@ -4,44 +4,8 @@ using UnityEngine; namespace UniVRM10 { [CreateAssetMenu(menuName = "VRM10/Expression")] - public sealed class VRM10Expression : ScriptableObject + public sealed class VRM10Expression : PrefabRelatedScriptableObject { -#if UNITY_EDITOR - /// - /// Preview 用のObject参照 - /// - [SerializeField] - GameObject m_prefab; - public GameObject Prefab - { - set { m_prefab = value; } - get - { - if (m_prefab == null) - { - var assetPath = UnityEditor.AssetDatabase.GetAssetPath(this); - if (!string.IsNullOrEmpty(assetPath)) - { - // if asset is subasset of prefab - m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath); - if (m_prefab != null) return m_prefab; - - var parent = UnityPath.FromUnityPath(assetPath).Parent; - var prefabPath = parent.Parent.Child(parent.FileNameWithoutExtension + ".prefab"); - m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath.Value); - if (m_prefab != null) return m_prefab; - - var parentParent = UnityPath.FromUnityPath(assetPath).Parent.Parent; - var vrmPath = parent.Parent.Child(parent.FileNameWithoutExtension + ".vrm"); - m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(vrmPath.Value); - if (m_prefab != null) return m_prefab; - } - } - return m_prefab; - } - } -#endif - /// /// ExpressionPreset が Unknown 場合の識別子 /// diff --git a/Assets/VRM10/Runtime/Components/PrefabRelatedScriptableObject.cs b/Assets/VRM10/Runtime/Components/PrefabRelatedScriptableObject.cs new file mode 100644 index 000000000..bc24cf26b --- /dev/null +++ b/Assets/VRM10/Runtime/Components/PrefabRelatedScriptableObject.cs @@ -0,0 +1,52 @@ +using UniGLTF; +using UnityEngine; + +namespace UniVRM10 +{ + /// + /// ScriptableObject なのだけど Editor では、ヒエラルキーを参照したい。 + /// 参照して、ヒエラルキー内の Renderer を選択したりしたい。 + /// + /// そういうオブジェクト。 + /// + public class PrefabRelatedScriptableObject : ScriptableObject + { +#if UNITY_EDITOR + /// + /// Preview 用のObject参照 + /// + [SerializeField] + GameObject m_prefab; + public GameObject Prefab + { + set { m_prefab = value; } + get + { + if (m_prefab == null) + { + var assetPath = UnityEditor.AssetDatabase.GetAssetPath(this); + if (!string.IsNullOrEmpty(assetPath)) + { + // if asset is subasset of prefab + m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath); + if (m_prefab != null) return m_prefab; + + // 同じフォルダに prefab が居る + var parent = UnityPath.FromUnityPath(assetPath).Parent; + var prefabPath = parent.Parent.Child(parent.FileNameWithoutExtension + ".prefab"); + m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath.Value); + if (m_prefab != null) return m_prefab; + + // 同じフォルダに vrm(ScriptedImporter) が居る + var parentParent = UnityPath.FromUnityPath(assetPath).Parent.Parent; + var vrmPath = parent.Parent.Child(parent.FileNameWithoutExtension + ".vrm"); + m_prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(vrmPath.Value); + if (m_prefab != null) return m_prefab; + } + } + return m_prefab; + } + } +#endif + } +} diff --git a/Assets/VRM10/Runtime/Components/PrefabRelatedScriptableObject.cs.meta b/Assets/VRM10/Runtime/Components/PrefabRelatedScriptableObject.cs.meta new file mode 100644 index 000000000..1f7427741 --- /dev/null +++ b/Assets/VRM10/Runtime/Components/PrefabRelatedScriptableObject.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fa46394d71fe3814ca9b60f0b3409c6d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Components/VRM10Object/VRM10Object.cs b/Assets/VRM10/Runtime/Components/VRM10Object/VRM10Object.cs index 35c412953..4ff09e44f 100644 --- a/Assets/VRM10/Runtime/Components/VRM10Object/VRM10Object.cs +++ b/Assets/VRM10/Runtime/Components/VRM10Object/VRM10Object.cs @@ -17,7 +17,7 @@ namespace UniVRM10 /// * FirstPerson /// /// - public class VRM10Object : ScriptableObject + public class VRM10Object : PrefabRelatedScriptableObject { public static SubAssetKey SubAssetKey => new SubAssetKey(typeof(VRM10Object), "_vrm1_"); From 061eae602951aef50504b2ecb7f05328ee9dac75 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 18 Jun 2021 18:53:10 +0900 Subject: [PATCH 2/2] static --- Assets/VRM10/Editor/Components/VRM10ObjectEditor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRM10/Editor/Components/VRM10ObjectEditor.cs b/Assets/VRM10/Editor/Components/VRM10ObjectEditor.cs index 4c3b37ee0..4c9a19464 100644 --- a/Assets/VRM10/Editor/Components/VRM10ObjectEditor.cs +++ b/Assets/VRM10/Editor/Components/VRM10ObjectEditor.cs @@ -18,7 +18,7 @@ namespace UniVRM10 LookAt, FirstPerson, } - Tabs _tab = Tabs.Meta; + static Tabs _tab = Tabs.Meta; // for SerializedProperty PropGui m_expression;