From cd198cc970a4c1c6e24dcceee103c6c29bf03a1c Mon Sep 17 00:00:00 2001 From: pharan Date: Tue, 24 Feb 2015 14:02:07 +0800 Subject: [PATCH 1/3] Added Sorting Layers GUI to SkeletonRenderer and SkeletonAnimation Inspectors. --- .../Editor/SkeletonAnimationInspector.cs | 6 +- .../Editor/SkeletonRendererInspector.cs | 112 +++++++++++++++--- 2 files changed, 99 insertions(+), 19 deletions(-) diff --git a/spine-unity/Assets/spine-unity/Editor/SkeletonAnimationInspector.cs b/spine-unity/Assets/spine-unity/Editor/SkeletonAnimationInspector.cs index f338626b2..d099e3710 100644 --- a/spine-unity/Assets/spine-unity/Editor/SkeletonAnimationInspector.cs +++ b/spine-unity/Assets/spine-unity/Editor/SkeletonAnimationInspector.cs @@ -66,6 +66,7 @@ public class SkeletonAnimationInspector : SkeletonRendererInspector { } } + EditorGUILayout.Space(); //TODO: Refactor this to use GenericMenu and callbacks to avoid interfering with control by other behaviours. // Animation name. @@ -80,10 +81,7 @@ public class SkeletonAnimationInspector : SkeletonRendererInspector { animationIndex = i + 1; } - EditorGUILayout.BeginHorizontal(); - EditorGUILayout.LabelField("Animation", GUILayout.Width(EditorGUIUtility.labelWidth)); - animationIndex = EditorGUILayout.Popup(animationIndex, animations); - EditorGUILayout.EndHorizontal(); + animationIndex = EditorGUILayout.Popup("Animation", animationIndex, animations); String selectedAnimationName = animationIndex == 0 ? null : animations[animationIndex]; if (component.AnimationName != selectedAnimationName) { diff --git a/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs b/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs index e2f8ff52f..4845a07ee 100644 --- a/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs +++ b/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs @@ -28,12 +28,17 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ using System; +using System.Reflection; using UnityEditor; using UnityEngine; [CustomEditor(typeof(SkeletonRenderer))] public class SkeletonRendererInspector : Editor { protected SerializedProperty skeletonDataAsset, initialSkinName, normals, tangents, meshes, immutableTriangles, submeshSeparators, front; + protected bool advancedFoldout; + + private static PropertyInfo SortingLayerNamesProperty; + private static MethodInfo GetSortingLayerUserID; protected virtual void OnEnable () { SpineEditorUtilities.ConfirmInitialization(); @@ -45,6 +50,15 @@ public class SkeletonRendererInspector : Editor { immutableTriangles = serializedObject.FindProperty("immutableTriangles"); submeshSeparators = serializedObject.FindProperty("submeshSeparators"); front = serializedObject.FindProperty("frontFacing"); + + + var unityInternalEditorUtility = Type.GetType("UnityEditorInternal.InternalEditorUtility, UnityEditor"); + + if(SortingLayerNamesProperty == null) + SortingLayerNamesProperty = unityInternalEditorUtility.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic); + + if(GetSortingLayerUserID == null) + GetSortingLayerUserID = unityInternalEditorUtility.GetMethod("GetSortingLayerUserID", BindingFlags.Static | BindingFlags.NonPublic); } protected virtual void gui () { @@ -83,23 +97,62 @@ public class SkeletonRendererInspector : Editor { if (name == initialSkinName.stringValue) skinIndex = i; } - - EditorGUILayout.BeginHorizontal(); - EditorGUILayout.LabelField("Initial Skin", GUILayout.Width(EditorGUIUtility.labelWidth)); - skinIndex = EditorGUILayout.Popup(skinIndex, skins); - EditorGUILayout.EndHorizontal(); - + + skinIndex = EditorGUILayout.Popup("Initial Skin", skinIndex, skins); initialSkinName.stringValue = skins[skinIndex]; } - - EditorGUILayout.PropertyField(meshes, - new GUIContent("Render Meshes", "Disable to optimize rendering for skeletons that don't use meshes")); - EditorGUILayout.PropertyField(immutableTriangles, - new GUIContent("Immutable Triangles", "Enable to optimize rendering for skeletons that never change attachment visbility")); - EditorGUILayout.PropertyField(normals); - EditorGUILayout.PropertyField(tangents); - EditorGUILayout.PropertyField(front); - EditorGUILayout.PropertyField(submeshSeparators, true); + + EditorGUILayout.Space(); + + // Sorting Layers + { + var renderer = component.GetComponent(); + if(renderer != null) { + var sortingLayerNames = GetSortingLayerNames(); + + if (sortingLayerNames != null) { + var layerName = SortingLayerNameOfLayerID(renderer.sortingLayerID); + + int oldIndex = Array.IndexOf(sortingLayerNames, layerName); + int newIndex = EditorGUILayout.Popup("Sorting Layer", oldIndex, sortingLayerNames); + + if (newIndex != oldIndex) { + Undo.RecordObject(renderer, "Sorting Layer Changed"); + renderer.sortingLayerID = SortingLayerIdOfLayerIndex(newIndex); + EditorUtility.SetDirty(renderer); + } + } else { + EditorGUI.BeginChangeCheck(); + renderer.sortingLayerID = EditorGUILayout.IntField("Sorting Layer ID", renderer.sortingLayerID); + if(EditorGUI.EndChangeCheck()) { + EditorUtility.SetDirty(renderer); + } + } + + EditorGUI.BeginChangeCheck(); + renderer.sortingOrder = EditorGUILayout.IntField("Order in Layer", renderer.sortingOrder); + if(EditorGUI.EndChangeCheck()) { + EditorUtility.SetDirty(renderer); + } + } + } + + // More Render Options... + { + advancedFoldout = EditorGUILayout.Foldout(advancedFoldout, "Advanced"); + if(advancedFoldout) { + EditorGUI.indentLevel++; + EditorGUILayout.PropertyField(meshes, + new GUIContent("Render Meshes", "Disable to optimize rendering for skeletons that don't use meshes")); + EditorGUILayout.PropertyField(immutableTriangles, + new GUIContent("Immutable Triangles", "Enable to optimize rendering for skeletons that never change attachment visbility")); + EditorGUILayout.PropertyField(normals); + EditorGUILayout.PropertyField(tangents); + EditorGUILayout.PropertyField(front); + EditorGUILayout.PropertyField(submeshSeparators, true); + EditorGUI.indentLevel--; + } + } } override public void OnInspectorGUI () { @@ -112,4 +165,33 @@ public class SkeletonRendererInspector : Editor { ((SkeletonRenderer)target).Reset(); } } + + #region Sorting Layers + protected static string[] GetSortingLayerNames () { + if(SortingLayerNamesProperty == null) + return null; + + return SortingLayerNamesProperty.GetValue(null, null) as string[]; + } + + protected static string SortingLayerNameOfLayerID (int id) { + var layerNames = GetSortingLayerNames(); + if(layerNames == null) + return null; + + for (int i = 0, n = layerNames.Length; i < n; i++) { + if (SortingLayerIdOfLayerIndex(i) == id) + return layerNames[i]; + } + + return null; + } + + protected static int SortingLayerIdOfLayerIndex (int index) { + if(GetSortingLayerUserID == null) return 0; + + var parameters = new object[] {index}; + return (int)( GetSortingLayerUserID.Invoke( null, parameters ) ); + } + #endregion } From 2de88fc6aa938a5d0bd87a2fc58d702b62b22e42 Mon Sep 17 00:00:00 2001 From: pharan Date: Thu, 26 Feb 2015 11:00:17 +0800 Subject: [PATCH 2/3] Unity 5 compatible SkeletonRendererInspector sorting layer fields. --- .../Editor/SkeletonRendererInspector.cs | 66 ++++--------------- 1 file changed, 12 insertions(+), 54 deletions(-) diff --git a/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs b/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs index 4845a07ee..036db53a7 100644 --- a/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs +++ b/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs @@ -37,8 +37,9 @@ public class SkeletonRendererInspector : Editor { protected SerializedProperty skeletonDataAsset, initialSkinName, normals, tangents, meshes, immutableTriangles, submeshSeparators, front; protected bool advancedFoldout; - private static PropertyInfo SortingLayerNamesProperty; - private static MethodInfo GetSortingLayerUserID; + private static MethodInfo EditorGUILayoutSortingLayerField; + protected SerializedObject rendererSerializedObject; + protected SerializedProperty sortingLayerIDProperty; protected virtual void OnEnable () { SpineEditorUtilities.ConfirmInitialization(); @@ -51,14 +52,11 @@ public class SkeletonRendererInspector : Editor { submeshSeparators = serializedObject.FindProperty("submeshSeparators"); front = serializedObject.FindProperty("frontFacing"); + if(EditorGUILayoutSortingLayerField == null) + EditorGUILayoutSortingLayerField = typeof(EditorGUILayout).GetMethod("SortingLayerField", BindingFlags.Static | BindingFlags.NonPublic); - var unityInternalEditorUtility = Type.GetType("UnityEditorInternal.InternalEditorUtility, UnityEditor"); - - if(SortingLayerNamesProperty == null) - SortingLayerNamesProperty = unityInternalEditorUtility.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic); - - if(GetSortingLayerUserID == null) - GetSortingLayerUserID = unityInternalEditorUtility.GetMethod("GetSortingLayerUserID", BindingFlags.Static | BindingFlags.NonPublic); + rendererSerializedObject = new SerializedObject(((SkeletonRenderer)target).renderer); + sortingLayerIDProperty = rendererSerializedObject.FindProperty("m_SortingLayerID"); } protected virtual void gui () { @@ -108,30 +106,18 @@ public class SkeletonRendererInspector : Editor { { var renderer = component.GetComponent(); if(renderer != null) { - var sortingLayerNames = GetSortingLayerNames(); + EditorGUI.BeginChangeCheck(); - if (sortingLayerNames != null) { - var layerName = SortingLayerNameOfLayerID(renderer.sortingLayerID); - - int oldIndex = Array.IndexOf(sortingLayerNames, layerName); - int newIndex = EditorGUILayout.Popup("Sorting Layer", oldIndex, sortingLayerNames); - - if (newIndex != oldIndex) { - Undo.RecordObject(renderer, "Sorting Layer Changed"); - renderer.sortingLayerID = SortingLayerIdOfLayerIndex(newIndex); - EditorUtility.SetDirty(renderer); - } + if(sortingLayerIDProperty != null) { + EditorGUILayoutSortingLayerField.Invoke(null, new object[] { new GUIContent("Sorting Layer"), sortingLayerIDProperty, EditorStyles.popup } ); } else { - EditorGUI.BeginChangeCheck(); renderer.sortingLayerID = EditorGUILayout.IntField("Sorting Layer ID", renderer.sortingLayerID); - if(EditorGUI.EndChangeCheck()) { - EditorUtility.SetDirty(renderer); - } } - EditorGUI.BeginChangeCheck(); renderer.sortingOrder = EditorGUILayout.IntField("Order in Layer", renderer.sortingOrder); + if(EditorGUI.EndChangeCheck()) { + rendererSerializedObject.ApplyModifiedProperties(); EditorUtility.SetDirty(renderer); } } @@ -166,32 +152,4 @@ public class SkeletonRendererInspector : Editor { } } - #region Sorting Layers - protected static string[] GetSortingLayerNames () { - if(SortingLayerNamesProperty == null) - return null; - - return SortingLayerNamesProperty.GetValue(null, null) as string[]; - } - - protected static string SortingLayerNameOfLayerID (int id) { - var layerNames = GetSortingLayerNames(); - if(layerNames == null) - return null; - - for (int i = 0, n = layerNames.Length; i < n; i++) { - if (SortingLayerIdOfLayerIndex(i) == id) - return layerNames[i]; - } - - return null; - } - - protected static int SortingLayerIdOfLayerIndex (int index) { - if(GetSortingLayerUserID == null) return 0; - - var parameters = new object[] {index}; - return (int)( GetSortingLayerUserID.Invoke( null, parameters ) ); - } - #endregion } From b28b7674d80268c2a964f61cf48221cc5c94eccb Mon Sep 17 00:00:00 2001 From: pharan Date: Wed, 4 Mar 2015 10:13:53 +0800 Subject: [PATCH 3/3] Fixed ambiguous method return in Unity 5 release. --- .../Assets/spine-unity/Editor/SkeletonRendererInspector.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs b/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs index 036db53a7..693aee295 100644 --- a/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs +++ b/spine-unity/Assets/spine-unity/Editor/SkeletonRendererInspector.cs @@ -53,9 +53,9 @@ public class SkeletonRendererInspector : Editor { front = serializedObject.FindProperty("frontFacing"); if(EditorGUILayoutSortingLayerField == null) - EditorGUILayoutSortingLayerField = typeof(EditorGUILayout).GetMethod("SortingLayerField", BindingFlags.Static | BindingFlags.NonPublic); + EditorGUILayoutSortingLayerField = typeof(EditorGUILayout).GetMethod("SortingLayerField", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[] { typeof(GUIContent), typeof(SerializedProperty), typeof(GUIStyle) }, null); - rendererSerializedObject = new SerializedObject(((SkeletonRenderer)target).renderer); + rendererSerializedObject = new SerializedObject(((SkeletonRenderer)target).GetComponent()); sortingLayerIDProperty = rendererSerializedObject.FindProperty("m_SortingLayerID"); } @@ -108,7 +108,7 @@ public class SkeletonRendererInspector : Editor { if(renderer != null) { EditorGUI.BeginChangeCheck(); - if(sortingLayerIDProperty != null) { + if(EditorGUILayoutSortingLayerField != null && sortingLayerIDProperty != null) { EditorGUILayoutSortingLayerField.Invoke(null, new object[] { new GUIContent("Sorting Layer"), sortingLayerIDProperty, EditorStyles.popup } ); } else { renderer.sortingLayerID = EditorGUILayout.IntField("Sorting Layer ID", renderer.sortingLayerID);