Skip to content

Instantly share code, notes, and snippets.

@phi-lira
Created January 20, 2016 11:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phi-lira/9457433aef5c5aedda4b to your computer and use it in GitHub Desktop.
Save phi-lira/9457433aef5c5aedda4b to your computer and use it in GitHub Desktop.
CustomMaterialEditor - Unity
//Copyright(c) 2015 Phil Lira - phil.rlira [at] gmail [dot] com
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
using UnityEngine;
using UnityEditor;
// Custom Material Inspector to allow manual rendering sorting per material
// In order to use it add the following line in the shader you want a custom inspector:
// CustomEditor "CustomMaterialEditor"
// for more info on custom inspectors see the following link:
// http://docs.unity3d.com/460/Documentation/Manual/SL-CustomMaterialEditors.html
public class CustomMaterialEditor : MaterialEditor
{
private bool _OverrideSorting;
private Material _Material;
private string[] _LayerOptions = {"Background", "Opaque", "Transparent", "Overlay"};
private int _LayerID;
private int _LayerOrder;
public override void OnEnable()
{
base.OnEnable();
_Material = (target as Material);
if (_Material.renderQueue > -1)
{
_LayerID = (_Material.renderQueue / 1000) - 1;
_LayerOrder = _Material.renderQueue % 1000;
// There's no means to query if the current settings is equal to shader. So we check if
// order in layer is 0 and assume no override is wanted in that case.
_OverrideSorting = (_LayerOrder > 0);
}
else
{
_OverrideSorting = false;
}
}
public override void OnInspectorGUI()
{
if (_Material != null)
{
// Setting Material.renderQueue to -1 tells Unity to use value from shader.
int _RenderQueue = -1;
int layerID = _LayerID;
int layerOrder = _LayerOrder;
_OverrideSorting = EditorGUILayout.Toggle("Override Sorting", _OverrideSorting);
if (_OverrideSorting)
{
layerID = EditorGUILayout.Popup("Layer", _LayerID, _LayerOptions);
layerOrder = Mathf.Max(0, EditorGUILayout.IntField("Order", _LayerOrder));
_RenderQueue = ((layerID + 1) * 1000) + layerOrder;
}
if (GUI.changed)
{
if (_OverrideSorting == false || layerID != _LayerID || layerOrder != _LayerOrder)
{
_LayerID = layerID;
_LayerOrder = layerOrder;
Undo.RecordObject(_Material, "SortOrder");
_Material.renderQueue = _RenderQueue;
EditorUtility.SetDirty(_Material);
}
}
}
base.OnInspectorGUI();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment