add validation to VRMFirstPersonEditor

This commit is contained in:
ousttrue 2020-09-15 12:40:42 +09:00
parent ef3234ffe5
commit e96fc796de
3 changed files with 93 additions and 33 deletions

View File

@ -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;
}
*/
}
}

View File

@ -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()
{
}
/// <summary>
/// SceneView gizmo
/// </summary>
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<Transform>(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();
}
}
}

View File

@ -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<Validation> Validate(this VRMFirstPerson self)
{
var hierarchy = self.GetComponentsInChildren<Transform>(true);
Hierarchy = self.GetComponentsInChildren<Transform>(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;
}
}
}