Last active
September 5, 2018 11:07
-
-
Save GhatSmith/e266dd029cc000ae5c2f0638a3e85270 to your computer and use it in GitHub Desktop.
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 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