From 91306f4a8343e209c71f171bee8b09a501cc7ff8 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 3 Nov 2018 03:21:45 +0900 Subject: [PATCH] Added BlendShapeClipDrawer --- Scripts/BlendShape/BlendShapeAvatar.cs | 22 +---- Scripts/BlendShape/BlendShapeClip.cs | 5 +- .../Editor/BlendShapeAvatarEditor.cs | 99 +++++++++---------- .../BlendShape/Editor/BlendShapeClipDrawer.cs | 56 +++++++++++ .../Editor/BlendShapeClipDrawer.cs.meta | 12 +++ .../Editor/BlendShapeClipSelector.cs | 2 +- .../Editor/SerializedBlendShapeClipEditor.cs | 4 + 7 files changed, 130 insertions(+), 70 deletions(-) create mode 100644 Scripts/BlendShape/Editor/BlendShapeClipDrawer.cs create mode 100644 Scripts/BlendShape/Editor/BlendShapeClipDrawer.cs.meta diff --git a/Scripts/BlendShape/BlendShapeAvatar.cs b/Scripts/BlendShape/BlendShapeAvatar.cs index f988186e6..5606b0fce 100644 --- a/Scripts/BlendShape/BlendShapeAvatar.cs +++ b/Scripts/BlendShape/BlendShapeAvatar.cs @@ -60,29 +60,17 @@ namespace VRM Clips = Clips.OrderBy(x => BlendShapeKey.CreateFrom(x)).ToList(); } - public void AddBlendShapeClip() + static public BlendShapeClip CreateBlendShapeClip(string path) { - var dir = Path.GetDirectoryName(AssetDatabase.GetAssetPath(this)); - var path = EditorUtility.SaveFilePanel( - "Create BlendShapeClip", - dir, - string.Format("BlendShapeClip#{0}.asset", Clips.Count), - "asset"); - if (string.IsNullOrEmpty(path)) - { - return; - } - path = path.ToUnityRelativePath(); //Debug.LogFormat("{0}", path); var clip = ScriptableObject.CreateInstance(); clip.BlendShapeName = Path.GetFileNameWithoutExtension(path); - clip.Prefab = AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(this)); AssetDatabase.CreateAsset(clip, path); AssetDatabase.ImportAsset(path); - - Clips.Add(clip); - EditorUtility.SetDirty(this); - AssetDatabase.SaveAssets(); + return clip; + //Clips.Add(clip); + //EditorUtility.SetDirty(this); + //AssetDatabase.SaveAssets(); } #endif diff --git a/Scripts/BlendShape/BlendShapeClip.cs b/Scripts/BlendShape/BlendShapeClip.cs index 99b28f35c..96253b651 100644 --- a/Scripts/BlendShape/BlendShapeClip.cs +++ b/Scripts/BlendShape/BlendShapeClip.cs @@ -73,7 +73,7 @@ namespace VRM public BlendShapePreset Preset; /// - /// BlendShapeに対する参照(indexベース) + /// BlendShapeに対する参照(index ベース) /// /// [SerializeField] @@ -91,5 +91,8 @@ namespace VRM /// [SerializeField] public bool IsBinary; + + [SerializeField] + public Texture2D Thumbnail; } } diff --git a/Scripts/BlendShape/Editor/BlendShapeAvatarEditor.cs b/Scripts/BlendShape/Editor/BlendShapeAvatarEditor.cs index ace08a187..e87e43682 100644 --- a/Scripts/BlendShape/Editor/BlendShapeAvatarEditor.cs +++ b/Scripts/BlendShape/Editor/BlendShapeAvatarEditor.cs @@ -4,74 +4,71 @@ using System.IO; using System.Linq; using UniGLTF; using UnityEditor; +using UnityEditorInternal; using UnityEngine; namespace VRM { [CustomEditor(typeof(BlendShapeAvatar))] - public class BlendShapeAvatarEditor : PreviewEditor + public class BlendShapeAvatarEditor : Editor { - static String[] Presets = ((BlendShapePreset[])Enum.GetValues(typeof(BlendShapePreset))) - .Select(x => x.ToString()).ToArray(); + ReorderableList m_clipList; - BlendShapeClipSelector m_selector; - - SerializedBlendShapeEditor m_clipEditor; - - void OnPrefabChanged() + void OnEnable() { - if (m_selector != null) + var prop = serializedObject.FindProperty("Clips"); + m_clipList = new ReorderableList(serializedObject, prop); + + m_clipList.drawHeaderCallback = (rect) => + EditorGUI.LabelField(rect, "BlendShapeClips"); + + m_clipList.elementHeight = BlendShapeClipDrawer.Height; + m_clipList.drawElementCallback = (rect, index, isActive, isFocused) => { - Bake(m_selector.Selected, 1.0f); - } + var element = prop.GetArrayElementAtIndex(index); + rect.height -= 4; + rect.y += 2; + EditorGUI.PropertyField(rect, element); + }; + + m_clipList.onAddCallback += (list) => + { + // Add slot + prop.arraySize++; + // select last item + list.index = prop.arraySize - 1; + // get last item + var element = prop.GetArrayElementAtIndex(list.index); + element.objectReferenceValue = null; + + var dir = Path.GetDirectoryName(AssetDatabase.GetAssetPath(target)); + var path = EditorUtility.SaveFilePanel( + "Create BlendShapeClip", + dir, + string.Format("BlendShapeClip#{0}.asset", list.count), + "asset"); + if (!string.IsNullOrEmpty(path)) + { + var clip = BlendShapeAvatar.CreateBlendShapeClip(path.ToUnityRelativePath()); + //clip.Prefab = AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(target)); + + element.objectReferenceValue = clip; + } + }; + + m_clipList.onCanRemoveCallback += list => true; } - void OnSelected(BlendShapeClip clip) + void OnDisable() { - Bake(clip, 1.0f); - if (clip != null) - { - m_clipEditor = new SerializedBlendShapeEditor(clip, PreviewSceneManager); - } - else - { - m_clipEditor = null; - } - } - - protected override void OnEnable() - { - PrefabChanged += OnPrefabChanged; - base.OnEnable(); - - m_selector = new BlendShapeClipSelector((BlendShapeAvatar)target, OnSelected); - } - - protected override void OnDisable() - { - base.OnDisable(); - PrefabChanged -= OnPrefabChanged; } public override void OnInspectorGUI() { - base.OnInspectorGUI(); - - m_selector.SelectGUI(); - - if (m_clipEditor != null) - { - Separator(); - - m_selector.DuplicateWarn(); - - var result = m_clipEditor.Draw(); - if (result.Changed) - { - Bake(result.BlendShapeBindings, result.MaterialValueBindings, result.Weight); - } - } + serializedObject.Update(); + m_clipList.DoLayoutList(); + serializedObject.ApplyModifiedProperties(); } } } diff --git a/Scripts/BlendShape/Editor/BlendShapeClipDrawer.cs b/Scripts/BlendShape/Editor/BlendShapeClipDrawer.cs new file mode 100644 index 000000000..5a432cecb --- /dev/null +++ b/Scripts/BlendShape/Editor/BlendShapeClipDrawer.cs @@ -0,0 +1,56 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + + +namespace VRM +{ + + [CustomPropertyDrawer(typeof(BlendShapeClip))] + public class BlendShapeClipDrawer : PropertyDrawer + { + public const int Height = 72; + + public override void OnGUI(Rect position, + SerializedProperty property, GUIContent label) + { + using (new EditorGUI.PropertyScope(position, label, property)) + { + EditorGUIUtility.labelWidth = 80; + + position.height = EditorGUIUtility.singleLineHeight; + + var halfWidth = position.width * 0.5f; + + var rect = new Rect(position.x + 64, position.y, position.width - 64, position.height); + EditorGUI.PropertyField(rect, property); + + var clip = property.objectReferenceValue as BlendShapeClip; + if (clip != null) + { + var clipObj = new SerializedObject(clip); + var thumbnail = clipObj.FindProperty("Thumbnail"); + var blendShapeName = clipObj.FindProperty("BlendShapeName"); + var preset = clipObj.FindProperty("Preset"); + var isBinary = clipObj.FindProperty("IsBinary"); + + EditorGUI.ObjectField(new Rect(position) + { + width = 64, + height = 64 + }, thumbnail.objectReferenceValue, typeof(Texture), false); + + rect.y += (EditorGUIUtility.singleLineHeight + 2); + EditorGUI.PropertyField(rect, blendShapeName); + rect.y += (EditorGUIUtility.singleLineHeight + 2); + EditorGUI.PropertyField(rect, preset); + rect.y += (EditorGUIUtility.singleLineHeight + 2); + EditorGUI.PropertyField(rect, isBinary); + + clipObj.ApplyModifiedProperties(); + } + } + } + } +} \ No newline at end of file diff --git a/Scripts/BlendShape/Editor/BlendShapeClipDrawer.cs.meta b/Scripts/BlendShape/Editor/BlendShapeClipDrawer.cs.meta new file mode 100644 index 000000000..b5ef0081e --- /dev/null +++ b/Scripts/BlendShape/Editor/BlendShapeClipDrawer.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 6a31667cfcf461c47b3b384211e2d9ff +timeCreated: 1541179390 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/BlendShape/Editor/BlendShapeClipSelector.cs b/Scripts/BlendShape/Editor/BlendShapeClipSelector.cs index 84fd6301d..b37d7c21d 100644 --- a/Scripts/BlendShape/Editor/BlendShapeClipSelector.cs +++ b/Scripts/BlendShape/Editor/BlendShapeClipSelector.cs @@ -69,7 +69,7 @@ namespace VRM if (GUILayout.Button("Add BlendShapeClip")) { - m_avatar.AddBlendShapeClip(); + //m_avatar.AddBlendShapeClip(); } } diff --git a/Scripts/BlendShape/Editor/SerializedBlendShapeClipEditor.cs b/Scripts/BlendShape/Editor/SerializedBlendShapeClipEditor.cs index 4959f74a7..ebeb4c894 100644 --- a/Scripts/BlendShape/Editor/SerializedBlendShapeClipEditor.cs +++ b/Scripts/BlendShape/Editor/SerializedBlendShapeClipEditor.cs @@ -14,6 +14,7 @@ namespace VRM SerializedObject m_serializedObject; #region Properties + SerializedProperty m_thumbnail; SerializedProperty m_blendShapeNameProp; SerializedProperty m_presetProp; @@ -63,6 +64,7 @@ namespace VRM this.m_serializedObject = serializedObject; this.m_targetObject = targetObject; + m_thumbnail = serializedObject.FindProperty("Thumbnail"); m_blendShapeNameProp = serializedObject.FindProperty("BlendShapeName"); m_presetProp = serializedObject.FindProperty("Preset"); m_isBinaryProp = serializedObject.FindProperty("IsBinary"); @@ -131,6 +133,8 @@ namespace VRM m_targetObject, typeof(BlendShapeClip), false); GUI.enabled = true; + m_thumbnail.objectReferenceValue = EditorGUILayout.ObjectField(m_thumbnail.objectReferenceValue, typeof(Texture), false); + EditorGUILayout.PropertyField(m_thumbnail, true); EditorGUILayout.PropertyField(m_blendShapeNameProp, true); EditorGUILayout.PropertyField(m_presetProp, true);