Skip to content

Commit 768d958

Browse files
Merge pull request #57 from ExtendRealityLtd/fix/change-handler-methods
fix(MemberChange): prevent Before/AfterChange being called at edit time
2 parents fc99b85 + 7d6077b commit 768d958

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

Sources/FodyRunner.UnityIntegration/InspectorEditor.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,24 @@ public override void OnInspectorGUI()
7777
EditorGUI.indentLevel--;
7878
}
7979

80-
if (changeCheckScope.changed)
80+
// No change has been done, nothing to do.
81+
if (!changeCheckScope.changed)
8182
{
82-
FindChangeHandlerMethods(property);
83+
continue;
8384
}
8485

85-
if (changeCheckScope.changed
86-
&& Application.isPlaying
87-
&& (!(targetObject is Behaviour behaviour) || behaviour.isActiveAndEnabled)
88-
&& ChangeHandlerMethodInfos.Count > 0)
86+
// At design time we need to still allow Unity to persist the change and enable undo.
87+
if (!Application.isPlaying
88+
|| (targetObject is Behaviour behaviour && !behaviour.isActiveAndEnabled))
89+
{
90+
ApplyModifiedProperty(property, false);
91+
continue;
92+
}
93+
94+
FindChangeHandlerMethods(property);
95+
96+
// There are change handlers. Run them manually and ensure Unity knows those change handlers did some changes (to persist them and enable undo for them).
97+
if (ChangeHandlerMethodInfos.Count > 0)
8998
{
9099
Undo.RecordObject(targetObject, "Before change handlers");
91100
BeforeChange(property);
@@ -111,10 +120,11 @@ public override void OnInspectorGUI()
111120
ApplyModifiedProperty(property, true);
112121
AfterChange(property);
113122
}
114-
else if (changeCheckScope.changed)
123+
// Ensure subclasses of this inspector can run before/after change logic. Also ensure Unity persists the change (including undo support).
124+
else
115125
{
116126
BeforeChange(property);
117-
ApplyModifiedProperty(property, ChangeHandlerMethodInfos.Count > 0);
127+
ApplyModifiedProperty(property, false);
118128
AfterChange(property);
119129
}
120130
}
@@ -180,9 +190,9 @@ protected virtual void FindChangeHandlerMethods(SerializedProperty property)
180190
ChangeHandlerMethodInfos.Clear();
181191
ChangeHandlerMethodInfos.AddRange(
182192
property.serializedObject.targetObject.GetType()
183-
.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
193+
.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
184194
.Where(
185-
info => info.GetCustomAttributes<HandlesMemberChangeAttribute>()
195+
info => info.GetCustomAttributes<HandlesMemberChangeAttribute>(true)
186196
.Any(attribute => IsAttributeForProperty(property, info, attribute))));
187197
}
188198

0 commit comments

Comments
 (0)