10using System.Reflection;
36 private Action<UnityObject, object> _cachedDelegate;
38 public override void OnPropertyChanged(SerializedProperty property) {
39 base.OnPropertyChanged(property);
41 if (_cachedDelegate ==
null) {
42 Type type =
targets[0].GetType();
44 PropertyInfo propertyInfo = type.GetProperty(
methodName,
46 BindingFlags.NonPublic |
49 if (propertyInfo !=
null) {
50 _cachedDelegate = (obj, arg) => propertyInfo.SetValue(obj, arg,
null);
55 BindingFlags.NonPublic |
57 BindingFlags.Instance |
58 BindingFlags.FlattenHierarchy
62 Debug.LogWarning(
"Could not find a property or method of the name " +
63 methodName +
" " +
"to invoke for the OnChange attribute.");
67 int paramCount = method.GetParameters().Length;
68 if (paramCount == 0) {
69 _cachedDelegate = (obj, arg) => method.Invoke(obj,
null);
70 }
else if (paramCount == 1) {
71 object[] argArray =
new object[1];
72 _cachedDelegate = (obj, arg) => {
74 method.Invoke(obj, argArray);
78 " from OnChange because the method had more than 1 argument.");
83 property.serializedObject.ApplyModifiedProperties();
85 foreach (var target
in targets) {
86 object newValue =
fieldInfo.GetValue(target);
87 _cachedDelegate(target, newValue);
UnityEngine.Object UnityObject
Use the OnChange attribute to recieve a callback whenever a field is changed. The callback can be in ...
OnEditorChangeAttribute(string methodName)
readonly string methodName