From e96fc796de6e28c5072742d18ea9769be4fa6f90 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 15 Sep 2020 12:40:42 +0900 Subject: [PATCH] add validation to VRMFirstPersonEditor --- .../RendererFirstPersonFlagsDrawer.cs | 16 +---- .../FirstPerson/VRMFirstPersonEditor.cs | 67 +++++++++++++++++-- .../FirstPerson/VRMFirstPersonValidator.cs | 43 ++++++++---- 3 files changed, 93 insertions(+), 33 deletions(-) diff --git a/Assets/VRM/UniVRM/Editor/FirstPerson/RendererFirstPersonFlagsDrawer.cs b/Assets/VRM/UniVRM/Editor/FirstPerson/RendererFirstPersonFlagsDrawer.cs index 9a278708c..0e2e57cfc 100644 --- a/Assets/VRM/UniVRM/Editor/FirstPerson/RendererFirstPersonFlagsDrawer.cs +++ b/Assets/VRM/UniVRM/Editor/FirstPerson/RendererFirstPersonFlagsDrawer.cs @@ -1,6 +1,4 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEditor; +using UnityEditor; using UnityEngine; @@ -11,11 +9,11 @@ namespace VRM { 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); } public override void OnGUI(Rect position, @@ -28,13 +26,5 @@ namespace VRM EditorGUI.PropertyField(LeftSide(position, WIDTH), rendererProp, new GUIContent(""), true); EditorGUI.PropertyField(RightSide(position, WIDTH), flagProp, new GUIContent(""), true); } - - /* - public override float GetPropertyHeight(SerializedProperty property, - GUIContent label) - { - return 60.0f; - } - */ } } diff --git a/Assets/VRM/UniVRM/Editor/FirstPerson/VRMFirstPersonEditor.cs b/Assets/VRM/UniVRM/Editor/FirstPerson/VRMFirstPersonEditor.cs index e0a6a1f5c..5d4781120 100644 --- a/Assets/VRM/UniVRM/Editor/FirstPerson/VRMFirstPersonEditor.cs +++ b/Assets/VRM/UniVRM/Editor/FirstPerson/VRMFirstPersonEditor.cs @@ -1,15 +1,29 @@ using UnityEditor; +using UnityEngine; namespace VRM { [CustomEditor(typeof(VRMFirstPerson))] class VRMFirstPersonEditor : Editor { + VRMFirstPerson m_target; + + void OnEnable() + { + m_target = target as VRMFirstPerson; + } + + void OnDisable() + { + + } + + /// + /// SceneView gizmo + /// void OnSceneGUI() { - var component = target as VRMFirstPerson; - - var head = component.FirstPersonBone; + var head = m_target.FirstPersonBone; if (head == null) { return; @@ -17,17 +31,58 @@ namespace VRM EditorGUI.BeginChangeCheck(); - var worldOffset = head.localToWorldMatrix.MultiplyPoint(component.FirstPersonOffset); + var worldOffset = head.localToWorldMatrix.MultiplyPoint(m_target.FirstPersonOffset); worldOffset = Handles.PositionHandle(worldOffset, head.rotation); Handles.Label(worldOffset, "FirstPersonOffset"); if (EditorGUI.EndChangeCheck()) { - Undo.RecordObject(component, "Changed FirstPerson"); + Undo.RecordObject(m_target, "Changed FirstPerson"); - component.FirstPersonOffset = head.worldToLocalMatrix.MultiplyPoint(worldOffset); + m_target.FirstPersonOffset = head.worldToLocalMatrix.MultiplyPoint(worldOffset); } } + + public static void Separator(int indentLevel = 0) + { + EditorGUILayout.BeginHorizontal(); + GUILayout.Space(indentLevel * 15); + GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1)); + EditorGUILayout.EndHorizontal(); + } + + public override void OnInspectorGUI() + { + VRMFirstPersonValidator.Hierarchy = m_target.transform.GetComponentsInChildren(true); + + // show vaildation + bool isValid = true; + for (int i = 0; i < m_target.Renderers.Count; ++i) + { + if (VRMFirstPersonValidator.IsValid(m_target.Renderers[i], $"Renderers[{i}]", out Validation v)) + { + continue; + } + if (isValid) + { + EditorGUILayout.LabelField("Validation Errors"); + } + v.DrawGUI(); + isValid = false; + } + if (!isValid) + { + if (GUILayout.Button("reset renderers")) + { + m_target.TraverseRenderers(); + } + GUILayout.Space(10); + Separator(); + GUILayout.Space(10); + } + + base.OnInspectorGUI(); + } } } diff --git a/Assets/VRM/UniVRM/Editor/FirstPerson/VRMFirstPersonValidator.cs b/Assets/VRM/UniVRM/Editor/FirstPerson/VRMFirstPersonValidator.cs index acb8540ef..f7cd0a80c 100644 --- a/Assets/VRM/UniVRM/Editor/FirstPerson/VRMFirstPersonValidator.cs +++ b/Assets/VRM/UniVRM/Editor/FirstPerson/VRMFirstPersonValidator.cs @@ -6,28 +6,43 @@ namespace VRM { public static class VRMFirstPersonValidator { + public static Transform[] Hierarchy; + + public static bool IsValid(this VRMFirstPerson.RendererFirstPersonFlags r, string name, out Validation validation) + { + if (r.Renderer == null) + { + validation = Validation.Error($"{name}.Renderer is null"); + return false; + } + + if (!Hierarchy.Contains(r.Renderer.transform)) + { + validation = Validation.Error($"{name}.Renderer is out of hierarchy"); + return false; + } + + if (!r.Renderer.EnableForExport()) + { + validation = Validation.Error($"{name}.Renderer is not active"); + return false; + } + + validation = default; + return true; + } + public static IEnumerable Validate(this VRMFirstPerson self) { - var hierarchy = self.GetComponentsInChildren(true); + Hierarchy = self.GetComponentsInChildren(true); for (int i = 0; i < self.Renderers.Count; ++i) { - var r = self.Renderers[i]; - if (r.Renderer == null) + if (!IsValid(self.Renderers[i], $"[VRMFirstPerson]{self.name}.Renderers[{i}]", out Validation v)) { - yield return Validation.Error($"[VRMFirstPerson]{self.name}.Renderers[{i}].Renderer is null"); - continue; - } - if (!hierarchy.Contains(r.Renderer.transform)) - { - yield return Validation.Error($"[VRMFirstPerson]{self.name}.Renderers[{i}].Renderer is out of hierarchy"); - } - if (!r.Renderer.EnableForExport()) - { - yield return Validation.Error($"[VRMFirstPerson]{self.name}.Renderers[{i}].Renderer is not active"); + yield return v; } } - yield break; } } }