Last active
October 6, 2021 20:55
-
-
Save AngryAnt/b5995c9b2cf2a0979758ad17f6a59ef0 to your computer and use it in GitHub Desktop.
Attempting to override the default UnityEvent drawer.
This file contains hidden or 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 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"); | |
} | |
} | |
} |
This file contains hidden or 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 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); | |
} | |
} | |
} | |
} |
No worries. Thanks again for the thorough assist :)
Do you guys know how to make Unity Events show up conditionally in the inspector? Please see the last post here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ah lol didn't notice the date