Skip to content

Commit

Permalink
fix(Data): prevent null exception in collapsible drawer in 2022.3
Browse files Browse the repository at this point in the history
Due to a change in Unity 2022.3, there is now no actual need to do
the reflection for replacing a default drawer and in fact the
attempt to get `DrawerKeySet` will return a null, thus causing the
existing error.

The fix is to simply do a null check to prevent the error and the
reflection will simply be ignored on versions of Unity of 2022.3
and above.
  • Loading branch information
thestonefox committed Apr 17, 2024
1 parent 208570d commit 2ce9cef
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions Editor/Data/Type/CollapsibleUnityEventDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ public class CollapsibleUnityEventDrawer : UnityEventDrawer
public static void ReplaceDefaultDrawer()
{
System.Type utilityType = System.Type.GetType("UnityEditor.ScriptAttributeUtility, UnityEditor");
MethodInfo buildMethod = utilityType.GetMethod(
"BuildDrawerTypeForTypeDictionary",
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static,
null,
System.Type.EmptyTypes,
null);
FieldInfo dictionaryField = utilityType.GetField(
"s_DrawerTypeForType",
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
FieldInfo drawerField = utilityType
.GetNestedType("DrawerKeySet", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.GetField("drawer", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (utilityType == null)
{
return;
}

MethodInfo buildMethod = utilityType.GetMethod("BuildDrawerTypeForTypeDictionary", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, System.Type.EmptyTypes, null);
FieldInfo dictionaryField = utilityType.GetField("s_DrawerTypeForType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
System.Type drawerKeySet = utilityType.GetNestedType("DrawerKeySet", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (drawerKeySet == null)
{
return;
}
FieldInfo drawerField = drawerKeySet.GetField("drawer", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

// Ensure the key set is populated.
buildMethod.Invoke(null, null);
Expand Down

0 comments on commit 2ce9cef

Please sign in to comment.
  NODES
COMMUNITY 1
Note 1
Project 1
USERS 1