Skip to content

Instantly share code, notes, and snippets.

@GhatSmith
Last active September 5, 2018 11:07
Show Gist options
  • Save GhatSmith/e266dd029cc000ae5c2f0638a3e85270 to your computer and use it in GitHub Desktop.
Save GhatSmith/e266dd029cc000ae5c2f0638a3e85270 to your computer and use it in GitHub Desktop.
using UnityEditor;
namespace OddTales.Framework.Core.EditorExtension
{
/// <summary> Base class with logic to extend Unity built in inspector </summary>
public class ExtendUnityInspector<T> : Editor
{
/// <summary> Can be overriden for specific cases </summary>
protected virtual System.Type EditorType
{
get
{
System.Type type = typeof(Editor).Assembly.GetType("UnityEditor." + typeof(T).Name + "Editor");
if (type == null) type = typeof(Editor).Assembly.GetType("UnityEditor." + typeof(T).Name + "Inspector");
return type;
}
}
private Editor cachedEditor;
protected virtual void OnEnable()
{
if (EditorType == null) throw new System.NullReferenceException();
Editor.CreateCachedEditor(target, EditorType, ref cachedEditor);
}
public override void OnInspectorGUI()
{
if (cachedEditor != null) cachedEditor.OnInspectorGUI();
}
protected virtual void OnDisable()
{
if (cachedEditor != null) DestroyImmediate(cachedEditor); // Need to manually clean cached editor instance
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment