Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mob-sakai/UnityEventDrawerEx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.3.0
Choose a base ref
...
head repository: mob-sakai/UnityEventDrawerEx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.4.0
Choose a head ref
  • 4 commits
  • 5 files changed
  • 1 contributor

Commits on Jun 18, 2018

  1. fix #13; Error has occurred with UnityEvent collection such as List<U…

    …nityEvent> and UnityEvent[].
    mob-sakai committed Jun 18, 2018
    Copy the full SHA
    8566095 View commit details
  2. Add test

    mob-sakai committed Jun 18, 2018
    Copy the full SHA
    c0fd7d8 View commit details
  3. update change log

    mob-sakai committed Jun 18, 2018
    Copy the full SHA
    af49ae7 View commit details
  4. release

    mob-sakai committed Jun 18, 2018
    Copy the full SHA
    0ea76c2 View commit details
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ public static class PropertyGetterDelegate
{
const BindingFlags kBfAll = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
static readonly Dictionary<Type,Dictionary<string, MemberInfo>> s_TypeMemberMap = new Dictionary<Type,Dictionary<string, MemberInfo>>();
static readonly Regex s_RegexArray = new Regex(@"^Array\.data\[([0-9]+)\]\.!?", RegexOptions.Compiled);
static readonly Regex s_RegexArray = new Regex(@"^Array\.data\[([0-9]+)\](\.!?|$)", RegexOptions.Compiled);
static readonly Regex s_RegexNested = new Regex(@"^(\w+)\.!?", RegexOptions.Compiled);
static readonly Dictionary<int,Func<object,object>> s_PropertyGetterMap = new Dictionary<int, Func<object, object>>();

@@ -20,51 +20,58 @@ public static class PropertyGetterDelegate
/// </summary>
public static object GetInstance(this SerializedProperty property)
{
int hash = property.propertyPath.GetHashCode();

// Find getter delegate.
Func<object,object> getter = null;
if (s_PropertyGetterMap.TryGetValue(hash, out getter))
try
{
return getter(property);
}
int hash = property.propertyPath.GetHashCode();

// Create getter delegate.
object instance;
string path = property.propertyPath;
getter = p => (p as SerializedProperty).serializedObject.targetObject;
while (0 < path.Length)
{
var preGetter = getter;
instance = preGetter(property);
Match match;

// Array property.
if ((match = s_RegexArray.Match(path)).Success)
{
var arg = new object[]{ int.Parse(match.Groups[1].Value) };
var mi = instance.GetMemberInfo("get_Item", MemberTypes.Method) as MethodInfo;
getter = x => mi.Invoke(preGetter(x), arg);
path = s_RegexArray.Replace(path, "");
}
// Nested property.
else if ((match = s_RegexNested.Match(path)).Success)
// Find getter delegate.
Func<object,object> getter = null;
if (s_PropertyGetterMap.TryGetValue(hash, out getter))
{
var fi = instance.GetMemberInfo(match.Groups[1].Value, MemberTypes.Field) as FieldInfo;
getter = x => fi.GetValue(preGetter(x));
path = s_RegexNested.Replace(path, "");
return getter(property);
}
// Property.
else

// Create getter delegate.
object instance;
string path = property.propertyPath;
getter = p => (p as SerializedProperty).serializedObject.targetObject;
while (0 < path.Length)
{
var fi = instance.GetMemberInfo(path, MemberTypes.Field) as FieldInfo;
getter = x => fi.GetValue(preGetter(x));
break;
var preGetter = getter;
instance = preGetter(property);
Match match;

// Array property.
if ((match = s_RegexArray.Match(path)).Success)
{
var arg = new object[]{ int.Parse(match.Groups[1].Value) };
var mi = instance.GetMemberInfo(instance is Array ? "Get" : "get_Item", MemberTypes.Method) as MethodInfo;
getter = x => mi.Invoke(preGetter(x), arg);
path = s_RegexArray.Replace(path, "");
}
// Nested property.
else if ((match = s_RegexNested.Match(path)).Success)
{
var fi = instance.GetMemberInfo(match.Groups[1].Value, MemberTypes.Field) as FieldInfo;
getter = x => fi.GetValue(preGetter(x));
path = s_RegexNested.Replace(path, "");
}
// Property.
else
{
var fi = instance.GetMemberInfo(path, MemberTypes.Field) as FieldInfo;
getter = x => fi.GetValue(preGetter(x));
break;
}
}
}
s_PropertyGetterMap.Add(hash, getter);
s_PropertyGetterMap.Add(hash, getter);

return getter(property);
return getter(property);
}
catch
{
return null;
}
}

/// <summary>
Original file line number Diff line number Diff line change
@@ -173,7 +173,10 @@ static void DrawDelegate(Rect position, Delegate del)
public static IList GetRuntimeCalls(SerializedProperty property)
{
var propertyInstance = property.GetInstance();
return s_FiRuntimeCalls.GetValue(s_FiCalls.GetValue(propertyInstance)) as IList;

return propertyInstance != null
? s_FiRuntimeCalls.GetValue(s_FiCalls.GetValue(propertyInstance)) as IList
: new List<object>() as IList;
}
}

18 changes: 17 additions & 1 deletion Assets/Test/TestBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;


public class TestBehavior : MonoBehaviour
{
@@ -8,7 +10,21 @@ public class TestBehavior : MonoBehaviour

};

[SerializeField] TransformUnityEvent onYourCustomEvent = new TransformUnityEvent();
[System.Serializable]
public class Nested
{
public TransformUnityEvent nestedEvent = new TransformUnityEvent();
public TransformUnityEvent[] nestedEventArray = new TransformUnityEvent[]{};
public List<TransformUnityEvent> nestedEventList = new List<TransformUnityEvent>();
}

public TransformUnityEvent onYourCustomEvent = new TransformUnityEvent();
public TransformUnityEvent[] onYourCustomEventArray = new TransformUnityEvent[]{};
public List<TransformUnityEvent> onYourCustomEventList = new List<TransformUnityEvent>();

public Nested nested = new Nested();
public Nested[] nestedArray = new Nested[]{};
public List<Nested> nestedList = new List<Nested>{};

void OnEnable()
{
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [v0.4.0](https://github.com/mob-sakai/UnityEventDrawerEx/tree/v0.4.0) (2018-06-18)

[Full Changelog](https://github.com/mob-sakai/UnityEventDrawerEx/compare/v0.3.0...v0.4.0)

Supports UnityEvent collection such as `List<UnityEvent>` and `UnityEvent[]`.

**Fixed bugs:**

- Error has occurred with UnityEvent collection such as List\<UnityEvent\> and UnityEvent\[\]. [\#13](https://github.com/mob-sakai/UnityEventDrawerEx/issues/13)

## [v0.3.0](https://github.com/mob-sakai/UnityEventDrawerEx/tree/v0.3.0) (2018-06-11)

[Full Changelog](https://github.com/mob-sakai/UnityEventDrawerEx/compare/v0.2.0...v0.3.0)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "UnityEventDrawerEx",
"version": "0.3.0",
"version": "0.4.0",
"repository": {
"type": "git",
"url": "git+https://github.com/mob-sakai/UnityEventDrawerEx.git"