Skip to content

Instantly share code, notes, and snippets.

@nicloay
Created January 28, 2014 16:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicloay/8671358 to your computer and use it in GitHub Desktop.
Save nicloay/8671358 to your computer and use it in GitHub Desktop.
Unity wizard to change sorting layer and order for one or several objects. Here http://youtu.be/lIx9j5o8uEQ you can find screencast how to use it
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
public class SetRendererLayerWizard : ScriptableWizard {
public Renderer renderer;
public Renderer[] rendererList;
public SortingLayer sortingLayer;
public int sortingOrder = 2;
[MenuItem ("GameObject/Set Renderer SortingLayer")]
static void CreateWizard () {
ScriptableWizard.DisplayWizard<SetRendererLayerWizard>("Create Light", "Update");
}
void OnWizardUpdate() {
if(renderer == null && (rendererList == null || rendererList.Length == 0)) {
helpString = "";
errorString = "Select gameObject with renderer on it \n or drag couple of these objects in to the rendererList";
isValid = false;
} else {
if (renderer != null)
helpString = "Current sortingLayerName = " + renderer.sortingLayerID+ "\n" +
"sortingOrder =" + renderer.sortingOrder;
errorString = "";
isValid = true;
}
}
void OnWizardCreate() {
if (renderer!=null)
updateRenderer(renderer, sortingOrder, sortingLayer.id);
if (rendererList != null && rendererList.Length >0)
foreach(Renderer r in rendererList)
updateRenderer(r,sortingOrder, sortingLayer.id);
}
void updateRenderer(Renderer renderer, int sortingOrder, int layerId){
renderer.sortingOrder = sortingOrder;
renderer.gameObject.layer = layerId;
EditorUtility.SetDirty(renderer);
}
}
[Serializable]
public class SortingLayer{
public int id;
}
[CustomPropertyDrawer(typeof(SortingLayer))]
public class SortingLayerIdPropDrawer:PropertyDrawer{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position,label,property);
float offset = 4.0f;
position.x+=offset;
position.width -=offset * 2;
int origId = property.FindPropertyRelative("id").intValue;
string origString = LayerMask.LayerToName(origId);
int selectionId=0;
List<String> layerNames = new List<string>();
for (int i = 0; i < 32; i++)
{
string layerName = LayerMask.LayerToName(i);
if (layerName != string.Empty){
layerNames.Add(layerName);
if (layerName.Equals(origString))
selectionId = layerNames.Count - 1;
}
}
Rect labelRect = position;
labelRect.width = EditorGUIUtility.labelWidth;
position.x += EditorGUIUtility.labelWidth;
position.width -= EditorGUIUtility.labelWidth;
EditorGUI.indentLevel = 0;
EditorGUI.LabelField(labelRect, label);
int newId = EditorGUI.Popup(position, selectionId, layerNames.ToArray());
if (newId != selectionId){
int newLayerId = LayerMask.NameToLayer(layerNames[newId]);
property.FindPropertyRelative("id").intValue = newLayerId;
}
EditorGUI.EndProperty();
}
public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment