From 5145dd09cab55a896d510482f2e22f74b4d4b76f Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 5 Jun 2021 19:51:03 +0800 Subject: *misc --- .../Editor/InspectorExtends/InspectorExt.cs | 64 ++++++++++ .../Editor/InspectorExtends/InspectorExt.cs.meta | 11 ++ .../InspectorExtends/MeshRendererInspector.cs | 80 +++++++++++++ .../InspectorExtends/MeshRendererInspector.cs.meta | 11 ++ .../SkinnedMeshRendererInspector.cs | 132 +++++++++++++++++++++ .../SkinnedMeshRendererInspector.cs.meta | 11 ++ .../Editor/InspectorExtends/TransformInspector.cs | 50 ++++++++ .../InspectorExtends/TransformInspector.cs.meta | 11 ++ 8 files changed, 370 insertions(+) create mode 100644 UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/InspectorExt.cs create mode 100644 UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/InspectorExt.cs.meta create mode 100644 UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/MeshRendererInspector.cs create mode 100644 UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/MeshRendererInspector.cs.meta create mode 100644 UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/SkinnedMeshRendererInspector.cs create mode 100644 UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/SkinnedMeshRendererInspector.cs.meta create mode 100644 UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/TransformInspector.cs create mode 100644 UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/TransformInspector.cs.meta (limited to 'UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends') diff --git a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/InspectorExt.cs b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/InspectorExt.cs new file mode 100644 index 0000000..8ec839d --- /dev/null +++ b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/InspectorExt.cs @@ -0,0 +1,64 @@ +using System; +using System.Reflection; +using UnityEngine; +using UnityEditor; + +public abstract class InspectorExt : Editor +{ + protected Editor defaultEditor; + + protected abstract string defaultEditorName { get; } + + bool s_MoreInfo = true; + static readonly GUIContent more = EditorGUIUtility.TrTextContent("More Information", null, (Texture2D)null); + + public virtual void OnEnable() + { + defaultEditor = Editor.CreateEditor(targets, Type.GetType(defaultEditorName)); + if(defaultEditor == null) + Debug.LogError("No such editor class \""+ defaultEditorName + "\""); + TryInvokeDefaultMethod("OnEnable"); + } + + public virtual void OnDisable() + { + //When OnDisable is called, the default editor we created should be destroyed to avoid memory leakage. + //Also, make sure to call any required methods like OnDisable + TryInvokeDefaultMethod("OnDisable"); + DestroyImmediate(defaultEditor); + + s_MoreInfo = true; + } + + public override void OnInspectorGUI() + { + if (defaultEditor) + defaultEditor.OnInspectorGUI(); + } + + public virtual void OnSceneGUI () + { + TryInvokeDefaultMethod("OnSceneGUI"); + } + + protected bool BeginMore() + { + s_MoreInfo = EditorGUILayout.BeginFoldoutHeaderGroup(s_MoreInfo, more, null, null, null); + return s_MoreInfo; + } + + protected void EndMore() + { + EditorGUILayout.EndFoldoutHeaderGroup(); + } + + protected void TryInvokeDefaultMethod(string method, BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) + { + MethodInfo disableMethod = defaultEditor.GetType().GetMethod(method, flag); + if (disableMethod != null) + { + disableMethod.Invoke(defaultEditor, null); + } + } + +} \ No newline at end of file diff --git a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/InspectorExt.cs.meta b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/InspectorExt.cs.meta new file mode 100644 index 0000000..33a6747 --- /dev/null +++ b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/InspectorExt.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0ac8e9c82eb57e5489438cd41fa88ba5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/MeshRendererInspector.cs b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/MeshRendererInspector.cs new file mode 100644 index 0000000..8a639d6 --- /dev/null +++ b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/MeshRendererInspector.cs @@ -0,0 +1,80 @@ +using System; +using System.Reflection; +using UnityEngine; +using UnityEditor; + +// 扩展meshrender editor,显示 +// sortingLayer +// sortingOrder +// bounds +[CustomEditor(typeof(MeshRenderer), true)] +[CanEditMultipleObjects] +public class MeshRendererInspector : InspectorExt +{ + MeshRenderer renderer; + + bool s_ShowBounds = false; + + string s_BoundsIconPath = EditorGUIHelperSetUp.root + "Icons/bounds.png"; + GUIContent s_BoundsIcon; + + protected override string defaultEditorName => "UnityEditor.MeshRendererEditor, UnityEditor"; + + public override void OnEnable() + { + base.OnEnable(); + renderer = target as MeshRenderer; + Setup(); + } + + public override void OnDisable() + { + base.OnDisable(); + Setup(); + } + + void Setup() + { + s_ShowBounds = false; + if(s_BoundsIcon == null) + s_BoundsIcon = EditorGUIUtility.IconContent(s_BoundsIconPath); + } + + public sealed override void OnInspectorGUI() + { + base.OnInspectorGUI(); + + if (BeginMore()) + { + string sortingLayer = renderer.sortingLayerName; + string[] layers = new string[SortingLayer.layers.Length]; + int index = 0; + for (int i = 0; i < layers.Length; ++i) + { + layers[i] = SortingLayer.layers[i].name; + if (sortingLayer == layers[i]) + index = i; + } + index = EditorGUILayout.Popup("Sorting Layer", index, layers); + renderer.sortingLayerName = layers[index]; + renderer.sortingOrder = EditorGUILayout.IntField("Sorting Order", renderer.sortingOrder); + s_ShowBounds = EditorGUIHelper.ToggleButton("Show Bounds", s_ShowBounds, s_BoundsIcon); + } + EndMore(); + } + + public override void OnSceneGUI () + { + base.OnSceneGUI(); + if (s_ShowBounds) + { + Vector3 center = renderer.bounds.center; + float radius = renderer.bounds.extents.magnitude; + Handles.color = Color.white; + EditorHandlesHelper.WireCube(center, renderer.bounds.size, Color.blue); + Handles.color = Color.white; + EditorHandlesHelper.Label(center, "bounds:" + renderer.bounds.size,Color.blue); + } + } + +} \ No newline at end of file diff --git a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/MeshRendererInspector.cs.meta b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/MeshRendererInspector.cs.meta new file mode 100644 index 0000000..764508e --- /dev/null +++ b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/MeshRendererInspector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4a54e71946e679847a74bc44ae85768f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/SkinnedMeshRendererInspector.cs b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/SkinnedMeshRendererInspector.cs new file mode 100644 index 0000000..e4859b9 --- /dev/null +++ b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/SkinnedMeshRendererInspector.cs @@ -0,0 +1,132 @@ +using System; +using System.Reflection; +using UnityEngine; +using UnityEditor; + +// sortingLayer +// sortingOrder +// bounds +// bonecount +[CustomEditor(typeof(SkinnedMeshRenderer), true)] +[CanEditMultipleObjects] +public class SkinnedMeshRendererInspector : InspectorExt +{ + protected override string defaultEditorName => "UnityEditor.SkinnedMeshRendererEditor, UnityEditor"; + + SkinnedMeshRenderer renderer; + + string s_BoundsIconPath = EditorGUIHelperSetUp.root + "Icons/bounds.png"; + GUIContent s_BoundsIcon; + + bool s_ShowBounds = false; + + public override void OnEnable() + { + base.OnEnable(); + renderer = target as SkinnedMeshRenderer; + if (s_BoundsIcon == null) + s_BoundsIcon = EditorGUIUtility.IconContent(s_BoundsIconPath); + s_ShowBounds = false; + } + + public override void OnDisable() + { + base.OnDisable(); + s_ShowBounds = false; + } + + public override void OnInspectorGUI() + { + base.OnInspectorGUI(); + + if (renderer == null) + return; + + if(BeginMore()) + { + string sortingLayer = renderer.sortingLayerName; + string[] layers = new string[SortingLayer.layers.Length]; + int index = 0; + for (int i = 0; i < layers.Length; ++i) + { + layers[i] = SortingLayer.layers[i].name; + if (sortingLayer == layers[i]) + index = i; + } + index = EditorGUILayout.Popup("Sorting Layer", index, layers); + renderer.sortingLayerName = layers[index]; + renderer.sortingOrder = EditorGUILayout.IntField("Sorting Order", renderer.sortingOrder); + s_ShowBounds = EditorGUIHelper.ToggleButton("Show Bounds", s_ShowBounds, s_BoundsIcon); + Transform bone0 = renderer.bones != null && renderer.bones.Length > 0 ? renderer.bones[0] : null; + GUI.enabled = false; + EditorGUILayout.ObjectField("Bones[0]", bone0, typeof(Transform), true); + GUI.enabled = true; + //EditorGUIHelper.LabelField("Bone Count", GetBoneCount(bone0).ToString()); + EditorGUIHelper.LabelField("Bones Length", renderer.bones.Length.ToString()); + + // Mesh info + Mesh mesh = renderer.sharedMesh; + if(mesh) + { + EditorGUIHelper.LabelField("Mesh Info", "Vertex Count (VBO): " + mesh.vertexCount); + EditorGUIHelper.LabelFieldRight("Topology Count (IBO): " + mesh.triangles.Length); + EditorGUIHelper.LabelFieldRight("Possible Triangles Count: " + mesh.triangles.Length / 3); + EditorGUIHelper.LabelFieldRight("SubMesh Count: " + mesh.subMeshCount); + EditorGUIHelper.LabelFieldRight("Attributes: " + GetAttributesEnabled()); + } + } + EndMore(); + } + + + private int GetBoneCount(Transform root) + { + if (root == null) + return 0; + int childCount = root.childCount; + for (int i = 0; i < root.childCount; ++i) + { + childCount += GetBoneCount(root.GetChild(i)); + } + return childCount; + } + + public override void OnSceneGUI() + { + base.OnSceneGUI(); + if (s_ShowBounds) + { + Vector3 center = renderer.bounds.center; + float radius = renderer.bounds.extents.magnitude; + Handles.color = Color.white; + EditorHandlesHelper.WireCube(center, renderer.bounds.size, Color.blue); + Handles.color = Color.white; + EditorHandlesHelper.Label(center, "bounds:" + renderer.bounds.size, Color.blue); + } + } + + private string GetAttributesEnabled() + { + if (renderer == null) + return ""; + string attr = ""; + Mesh mesh = renderer.sharedMesh; + if (mesh.vertices.Length > 0) attr += "[position] "; + if (mesh.normals.Length > 0) attr += "[normal] "; + if (mesh.tangents.Length > 0) attr += "[tangent] "; + if (mesh.colors.Length > 0) attr += "[color] "; + if (mesh.uv.Length > 0) attr += "[uv1] "; + if (mesh.uv2.Length > 0) attr += "[uv2] "; + if (mesh.uv3.Length > 0) attr += "[uv3] "; + if (mesh.uv4.Length > 0) attr += "[uv4] "; +#if UNITY_2019 || UNITY_2020 || UNITY_2021 + if (mesh.uv5.Length > 0) attr += "[uv5] "; + if (mesh.uv6.Length > 0) attr += "[uv6] "; + if (mesh.uv7.Length > 0) attr += "[uv7] "; + if (mesh.uv8.Length > 0) attr += "[uv8] "; +#endif + if (mesh.boneWeights.Length > 0) attr += "[boneWeights] "; + return attr; + } + +} diff --git a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/SkinnedMeshRendererInspector.cs.meta b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/SkinnedMeshRendererInspector.cs.meta new file mode 100644 index 0000000..5d0e921 --- /dev/null +++ b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/SkinnedMeshRendererInspector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e27c6d4aff57a454292d44ec5fab325f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/TransformInspector.cs b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/TransformInspector.cs new file mode 100644 index 0000000..c475a8c --- /dev/null +++ b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/TransformInspector.cs @@ -0,0 +1,50 @@ +//https://forum.unity.com/threads/extending-instead-of-replacing-built-in-inspectors.407612/ +using System; +using System.Reflection; +using UnityEngine; +using UnityEditor; + +[CustomEditor(typeof(Transform), true)] +[CanEditMultipleObjects] +public class TransformInspector : InspectorExt +{ + Transform transform; + + bool s_ShowInformation = false; + + protected override string defaultEditorName => "UnityEditor.TransformInspector, UnityEditor"; + + public override void OnEnable() + { + base.OnEnable(); + transform = target as Transform; + s_ShowInformation = false; + } + + public override void OnDisable() + { + base.OnDisable(); + s_ShowInformation = false; + } + + public override void OnInspectorGUI() + { + EditorGUILayout.LabelField("Local Space", EditorStyles.boldLabel); + defaultEditor.OnInspectorGUI(); + + if(BeginMore()) + { + // 世界坐标 + EditorGUILayout.LabelField("World Space", EditorStyles.boldLabel); + + GUI.enabled = false; + + EditorGUILayout.Vector3Field("Position", transform.position); + EditorGUILayout.Vector3Field("Rotation", transform.rotation.ToEuler() * Mathf.Rad2Deg); + EditorGUILayout.Vector3Field("Scale", transform.lossyScale); + + GUI.enabled = true; + } + EndMore(); + } +} \ No newline at end of file diff --git a/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/TransformInspector.cs.meta b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/TransformInspector.cs.meta new file mode 100644 index 0000000..bd8715e --- /dev/null +++ b/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/InspectorExtends/TransformInspector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 96be6f39599da8249ac5d7ab713e15ce +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- cgit v1.1-26-g67d0