Unity 5 compatible SkeletonRendererInspector sorting layer fields.

This commit is contained in:
pharan 2015-02-26 11:00:17 +08:00
parent cd198cc970
commit 2de88fc6aa

View File

@ -37,8 +37,9 @@ public class SkeletonRendererInspector : Editor {
protected SerializedProperty skeletonDataAsset, initialSkinName, normals, tangents, meshes, immutableTriangles, submeshSeparators, front; protected SerializedProperty skeletonDataAsset, initialSkinName, normals, tangents, meshes, immutableTriangles, submeshSeparators, front;
protected bool advancedFoldout; protected bool advancedFoldout;
private static PropertyInfo SortingLayerNamesProperty; private static MethodInfo EditorGUILayoutSortingLayerField;
private static MethodInfo GetSortingLayerUserID; protected SerializedObject rendererSerializedObject;
protected SerializedProperty sortingLayerIDProperty;
protected virtual void OnEnable () { protected virtual void OnEnable () {
SpineEditorUtilities.ConfirmInitialization(); SpineEditorUtilities.ConfirmInitialization();
@ -51,14 +52,11 @@ public class SkeletonRendererInspector : Editor {
submeshSeparators = serializedObject.FindProperty("submeshSeparators"); submeshSeparators = serializedObject.FindProperty("submeshSeparators");
front = serializedObject.FindProperty("frontFacing"); front = serializedObject.FindProperty("frontFacing");
if(EditorGUILayoutSortingLayerField == null)
EditorGUILayoutSortingLayerField = typeof(EditorGUILayout).GetMethod("SortingLayerField", BindingFlags.Static | BindingFlags.NonPublic);
var unityInternalEditorUtility = Type.GetType("UnityEditorInternal.InternalEditorUtility, UnityEditor"); rendererSerializedObject = new SerializedObject(((SkeletonRenderer)target).renderer);
sortingLayerIDProperty = rendererSerializedObject.FindProperty("m_SortingLayerID");
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 () { protected virtual void gui () {
@ -108,30 +106,18 @@ public class SkeletonRendererInspector : Editor {
{ {
var renderer = component.GetComponent<Renderer>(); var renderer = component.GetComponent<Renderer>();
if(renderer != null) { if(renderer != null) {
var sortingLayerNames = GetSortingLayerNames(); EditorGUI.BeginChangeCheck();
if (sortingLayerNames != null) { if(sortingLayerIDProperty != null) {
var layerName = SortingLayerNameOfLayerID(renderer.sortingLayerID); EditorGUILayoutSortingLayerField.Invoke(null, new object[] { new GUIContent("Sorting Layer"), sortingLayerIDProperty, EditorStyles.popup } );
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 { } else {
EditorGUI.BeginChangeCheck();
renderer.sortingLayerID = EditorGUILayout.IntField("Sorting Layer ID", renderer.sortingLayerID); 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); renderer.sortingOrder = EditorGUILayout.IntField("Order in Layer", renderer.sortingOrder);
if(EditorGUI.EndChangeCheck()) { if(EditorGUI.EndChangeCheck()) {
rendererSerializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(renderer); 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
} }