Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Attempting to override the default UnityEvent drawer.
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine.Events;
using System.Reflection;
namespace Test
{
[CustomPropertyDrawer (typeof (UnityEventBase), true)]
public class Drawer : UnityEventDrawer
{
[InitializeOnLoadMethod]
static void OnLoad ()
{
Debug.LogFormat ("Attempting to disable built-in property drawer");
/*object drawer = typeof (UnityEventDrawer).GetCustomAttributes (true).FirstOrDefault (attribute => attribute is CustomPropertyDrawer);
typeof (CustomPropertyDrawer).GetField ("m_Type", BindingFlags.Instance | BindingFlags.NonPublic).SetValue (drawer, null);*/
ReplaceDrawer<UnityEventBase, Drawer> ();
}
static void ReplaceDrawer<TType, TDrawer> ()
where TType : class
where TDrawer : PropertyDrawer
{
MethodInfo dictionarySetter = ScriptAttributeUtility.DrawerTypeForTypeField.FieldType.GetMethod ("set_Item");
dictionarySetter.Invoke (
ScriptAttributeUtility.DrawerTypeForType,
new []
{
typeof (TType),
ScriptAttributeUtility.DrawerKeySet.Create<TType, TDrawer>()
}
);
}
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
base.OnGUI (position, property, label);
GUI.Label (position, "OVERRIDDEN");
Debug.Log ("PING");
}
}
}
using System;
using System.Reflection;
using UnityEditor;
namespace Test
{
public static class ScriptAttributeUtility
{
public static class DrawerKeySet
{
static Type s_Type;
public static Type Type
{
get
{
return s_Type ?? (s_Type = ScriptAttributeUtility.Type.GetNestedType ("DrawerKeySet", BindingFlags.NonPublic));
}
}
public static object Create<TType, TDrawer> ()
where TType : class
where TDrawer : PropertyDrawer
{
object instance = Activator.CreateInstance (Type);
Type.GetField ("drawer").SetValue (instance, typeof (TDrawer));
Type.GetField ("type").SetValue (instance, typeof (TType));
return instance;
}
}
static Type s_Type;
static FieldInfo s_DrawerTypeForTypeField;
static object s_DrawerTypeForType;
public static Type Type
{
get
{
return s_Type ?? (s_Type = typeof (EditorWindow).Assembly.
GetType ("UnityEditor.ScriptAttributeUtility", throwOnError: true, ignoreCase: false));
}
}
public static FieldInfo DrawerTypeForTypeField
{
get
{
return s_DrawerTypeForTypeField ?? (s_DrawerTypeForTypeField = Type.
GetField ("s_DrawerTypeForType", BindingFlags.Static | BindingFlags.NonPublic));
}
}
public static object DrawerTypeForType
{
get
{
if ((s_DrawerTypeForType ?? (s_DrawerTypeForType = DrawerTypeForTypeField.GetValue (null))) != null)
{
return s_DrawerTypeForType;
}
Type.GetMethod ("BuildDrawerTypeForTypeDictionary", BindingFlags.NonPublic | BindingFlags.Static).Invoke (null, null);
return s_DrawerTypeForType = DrawerTypeForTypeField.GetValue (null);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment