Skip to content

Commit

Permalink
Fix issue preventing custom event handliers from being found
Browse files Browse the repository at this point in the history
- Addresses issue #7
  • Loading branch information
MerlinVR committed Jun 7, 2020
1 parent 5258496 commit b57776c
Showing 1 changed file with 63 additions and 45 deletions.
108 changes: 63 additions & 45 deletions EasyEventEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,68 @@ public FunctionData(SerializedProperty listener, Object target = null, MethodInf
MethodInfo cachedFindMethodInfo = null;
static EasyEventEditorHandler.EEESettings cachedSettings;

#if UNITY_2018_4_OR_NEWER
private static UnityEventBase GetDummyEventStep(string propertyPath, System.Type propertyType, BindingFlags bindingFlags)
{
UnityEventBase dummyEvent = null;

while (propertyPath.Length > 0)
{
if (propertyPath.StartsWith("."))
propertyPath = propertyPath.Substring(1);

string[] splitPath = propertyPath.Split(new char[] { '.' }, 2);

FieldInfo newField = propertyType.GetField(splitPath[0], bindingFlags);

if (newField == null)
break;

propertyType = newField.FieldType;
if (propertyType.IsArray)
{
propertyType = propertyType.GetElementType();
}
else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>))
{
propertyType = propertyType.GetGenericArguments()[0];
}

if (splitPath.Length == 1)
break;

propertyPath = splitPath[1];
if (propertyPath.StartsWith("Array.data["))
propertyPath = propertyPath.Split(new char[] { ']' }, 2)[1];
}

if (propertyType.IsSubclassOf(typeof(UnityEventBase)))
dummyEvent = System.Activator.CreateInstance(propertyType) as UnityEventBase;

return dummyEvent;
}

private static UnityEventBase GetDummyEvent(SerializedProperty property)
{
Object targetObject = property.serializedObject.targetObject;
if (targetObject == null)
return new UnityEvent();

UnityEventBase dummyEvent = null;
System.Type targetType = targetObject.GetType();
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

do
{
dummyEvent = GetDummyEventStep(property.propertyPath, targetType, bindingFlags);
bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
targetType = targetType.BaseType;
} while (dummyEvent == null && targetType != null);

return dummyEvent ?? new UnityEvent();
}
#endif

private void PrepareState(SerializedProperty propertyForState)
{
DrawerState state;
Expand Down Expand Up @@ -540,51 +602,7 @@ private void PrepareState(SerializedProperty propertyForState)

// Setup dummy event
#if UNITY_2018_4_OR_NEWER
Object targetObject = currentProperty.serializedObject.targetObject;
if (targetObject == null)
{
dummyEvent = new UnityEvent();
}
else
{
string propertyPath = currentProperty.propertyPath;
System.Type propertyType = targetObject.GetType();

while (propertyPath.Length > 0)
{
if (propertyPath.StartsWith("."))
propertyPath = propertyPath.Substring(1);

string[] splitPath = propertyPath.Split(new char[] { '.' }, 2);

FieldInfo newField = propertyType.GetField(splitPath[0], BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

if (newField == null)
break;

propertyType = newField.FieldType;
if (propertyType.IsArray)
{
propertyType = propertyType.GetElementType();
}
else if (propertyType.IsGenericType && typeof(List<>).IsAssignableFrom(propertyType))
{
propertyType = propertyType.GetGenericArguments()[0];
}

if (splitPath.Length == 1)
break;

propertyPath = splitPath[1];
if (propertyPath.StartsWith("Array.data["))
propertyPath = propertyPath.Split(new char[] { ']' }, 2)[1];
}

if (propertyType.IsSubclassOf(typeof(UnityEventBase)))
dummyEvent = System.Activator.CreateInstance(propertyType) as UnityEventBase;
else
dummyEvent = new UnityEvent();
}
dummyEvent = GetDummyEvent(propertyForState);
#else
string eventTypeName = currentProperty.FindPropertyRelative("m_TypeName").stringValue;
System.Type eventType = EasyEventEditorHandler.FindTypeInAllAssemblies(eventTypeName);
Expand Down

0 comments on commit b57776c

Please sign in to comment.