Last active
October 13, 2015 07:47
-
-
Save FVSHaLuan/f82297b5d6a6649da884 to your computer and use it in GitHub Desktop.
**Function: Expose Sorting layer & Sorting Id in layer properties in inspector for any kind of renderer. **Usage: put SortingLayerSetterEditor.cs into any folder named Editor, add SortingLayerSetter to a GameObject with a render component attached (SpriteRenderer, MeshRenderer,...). **Disclaimer: I just extracted, learned and customized the code…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class SortingLayerSetter : MonoBehaviour { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEditor; | |
using System.Reflection; | |
using System; | |
[CustomEditor(typeof(SortingLayerSetter))] | |
public class SortingLayerSetterEditor : Editor | |
{ | |
private static MethodInfo EditorGUILayoutSortingLayerField; | |
protected SerializedObject rendererSerializedObject; | |
protected SerializedProperty sortingLayerIDProperty; | |
public void OnEnable() | |
{ | |
if (EditorGUILayoutSortingLayerField == null) | |
EditorGUILayoutSortingLayerField = typeof(EditorGUILayout).GetMethod("SortingLayerField", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[] { typeof(GUIContent), typeof(SerializedProperty), typeof(GUIStyle) }, null); | |
rendererSerializedObject = new SerializedObject(((SortingLayerSetter)target).GetComponent<Renderer>()); | |
sortingLayerIDProperty = rendererSerializedObject.FindProperty("m_SortingLayerID"); | |
} | |
public override void OnInspectorGUI() | |
{ | |
serializedObject.Update(); | |
gui(); | |
} | |
private void gui() | |
{ | |
SortingLayerSetter component = (SortingLayerSetter)target; | |
// Sorting Layers | |
{ | |
var renderer = component.GetComponent<Renderer>(); | |
if (renderer != null) | |
{ | |
EditorGUI.BeginChangeCheck(); | |
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); | |
} | |
renderer.sortingOrder = EditorGUILayout.IntField("Order in Layer", renderer.sortingOrder); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
rendererSerializedObject.ApplyModifiedProperties(); | |
EditorUtility.SetDirty(renderer); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment