Editor script for setting the sorting layer of an object

Hi there,

I have a script that changes the sorting layer on start up based on values I defined in the editor.

Here is the script :
using UnityEngine;
using System.Collections;

public class ZHK_SetSortingLayer : MonoBehaviour {

	// Use this for initialization
	public string sortingLayerName;
	public int sortingOrder;
	void Start () {
		renderer.sortingLayerName = sortingLayerName;
		renderer.sortingOrder = sortingOrder;
	}
}

Now I dont want to run the game each time just to see the effects of that script. So I was wondering if someone could show me how to write an editor script to update the sorting layer automatically in the editor.

Any help is appreciated.

Maybe it is not shortest way, but it works. Hope that i understand correct what you want to do.

DDSSortingLayer.cs - behavior script

using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
using System.Reflection;

[ExecuteInEditMode]
[System.Serializable]
#endif

public class DDSSortingLayer : MonoBehaviour 
{
	[SerializeField] public string currentSortingLayer = "";
	[SerializeField] public string[] sortingLayerNamess = null;

#if UNITY_EDITOR
	public void OnEnable ()
	{
		if (this.GetComponent <SpriteRenderer> () != null)
		{
			this.currentSortingLayer = this.GetComponent <SpriteRenderer> ().sortingLayerName;
			this.GetComponent <DDSSortingLayer> ().hideFlags = HideFlags.None;

			System.Type internalEditorUtilityType = typeof (InternalEditorUtility);
			PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty ("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
			
			this.sortingLayerNamess = (string[]) sortingLayersProperty.GetValue (null, new object[0]);
		}

		EditorApplication.update += CheckSortingLayer;
	}

	public int GetLayerIndex (string layerToFind = "Default")
	{
		if (this.sortingLayerNamess.Length == 0 || this.sortingLayerNamess == null)
						return -1;

		for (int i = 0; i < this.sortingLayerNamess.Length; i++)
		{
			if (this.sortingLayerNamess*.Equals (layerToFind))*
  •  		return i;*
    
  •  }*
    
  •  return -1;*
    
  • }*

  • public void CheckSortingLayer ()*

  • {*

  •  if (this.GetComponent <SpriteRenderer> () != null)*
    
  •  {*
    
  •  	this.GetComponent <SpriteRenderer> ().sortingLayerName = this.currentSortingLayer;*
    
  •  }*
    
  • }*
    #endif

}
DDSSortingLayerEditor.cs - should be placed in Editor folder
using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor (typeof(DDSSortingLayer))]

public class DDSSortingLayerEditor : Editor
{

  • private DDSSortingLayer layer = null;*

  • public void OnEnable ()*

  • {*

  •  this.layer = (DDSSortingLayer)target;*
    
  • }*

  • public override void OnInspectorGUI ()*

  • {*

  •  int _index = this.layer.GetLayerIndex (this.layer.currentSortingLayer);*
    
  •  _index = EditorGUILayout.Popup (_index, this.layer.sortingLayerNamess, GUILayout.ExpandWidth (true));*
    
  •  this.layer.currentSortingLayer = this.layer.sortingLayerNamess [_index];*
    
  •  EditorUtility.SetDirty (target);*
    
  •  //this.layer.currentSortingLayer* 
    
  • }*

}
If you want to add new sorting layer by taping in the field, it will be different code.
P.S. I am not allowed to answer twice, this why i edit my previos answer.

From here: https://github.com/nickgravelyn/UnityToolbag/tree/master/SortingLayer

Edited a little myself.

Usage:

[myNameSpace.SortingLayer]
public string sortingLayer;

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace myNameSpace
{
    public class SortingLayerAttribute : PropertyAttribute
    {
        [CustomPropertyDrawer(typeof(SortingLayerAttribute))]
        public class SortingLayerDrawer : PropertyDrawer
        {
            public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
            {
                string[] sortingLayerNames = new string[SortingLayer.layers.Length];
                for (int a = 0; a < SortingLayer.layers.Length; a++)
                    sortingLayerNames[a] = SortingLayer.layers[a].name;
                if (property.propertyType != SerializedPropertyType.String)
                {
                    EditorGUI.HelpBox(position, property.name + "{0} is not an string but has [SortingLayer].", MessageType.Error);
                }
                else if (sortingLayerNames.Length == 0)
                {
                    EditorGUI.HelpBox(position, "There is no Sorting Layers.", MessageType.Error);
                }
                else if (sortingLayerNames != null)
                {
                    EditorGUI.BeginProperty(position, label, property);

                    // Look up the layer name using the current layer ID
                    string oldName = property.stringValue;

                    // Use the name to look up our array index into the names list
                    int oldLayerIndex = -1;
                    for (int a = 0; a < sortingLayerNames.Length; a++)
                        if (sortingLayerNames[a].Equals(oldName)) oldLayerIndex = a;

                    // Show the popup for the names
                    int newLayerIndex = EditorGUI.Popup(position, label.text, oldLayerIndex, sortingLayerNames);

                    // If the index changes, look up the ID for the new index to store as the new ID
                    if (newLayerIndex != oldLayerIndex)
                    {
                        property.stringValue = sortingLayerNames[newLayerIndex];
                    }

                    EditorGUI.EndProperty();
                }
            }
        }
    }
}