Skip to content

Commit dd55623

Browse files
Refresh active gui using callbacks instead of time schedule
1 parent cf79402 commit dd55623

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

Packages/com.unity.inputsystem/InputSystem/Editor/InputAssetEditorUtils.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,20 @@ internal static T CreateAsset<T>(T asset, string relativePath) where T : Scripta
8383
return asset;
8484
}
8585

86-
public static VisualElement CreateMakeActiveGui<T>(Func<T> getCurrent, T target, string targetName, string entity, Action<T> apply, bool allowAssignActive = true)
86+
public static VisualElement CreateMakeActiveGui<T>(Func<T> getCurrent, T target, string targetName, string entity,
87+
Action<T> apply, Action<Action> subscribeToChanges, Action<Action> unsubscribeFromChanges,
88+
bool allowAssignActive = true)
8789
where T : ScriptableObject
8890
{
8991
var container = new VisualElement();
90-
var lastKnownCurrent = getCurrent();
91-
PopulateMakeActiveGui(container, lastKnownCurrent, target, entity, apply, allowAssignActive);
9292

93-
// Poll for external changes to the active asset (e.g. from Project Settings or Undo).
94-
// The scheduled item is automatically stopped when the element leaves the panel.
95-
container.schedule.Execute(() =>
96-
{
97-
var current = getCurrent();
98-
if (current == lastKnownCurrent)
99-
return;
100-
lastKnownCurrent = current;
101-
PopulateMakeActiveGui(container, current, target, entity, apply, allowAssignActive);
102-
}).Every(500);
93+
void Refresh() => PopulateMakeActiveGui(container, getCurrent(), target, entity, apply, allowAssignActive);
94+
95+
Refresh();
96+
97+
// Subscribe for as long as the element is part of a panel, matching the pattern used in InputParameterEditor.
98+
container.RegisterCallback<AttachToPanelEvent>(_ => subscribeToChanges(Refresh));
99+
container.RegisterCallback<DetachFromPanelEvent>(_ => unsubscribeFromChanges(Refresh));
103100

104101
return container;
105102
}

Packages/com.unity.inputsystem/InputSystem/Editor/Settings/InputSettingsProvider.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,23 @@ public override VisualElement CreateInspectorGUI()
500500
};
501501
root.Add(openButton);
502502

503-
root.Add(InputAssetEditorUtils.CreateMakeActiveGui(() => InputSystem.settings, target as InputSettings,
504-
target.name, "settings", (value) => InputSystem.settings = value));
503+
// UndoRedoCallback is void(), not Action, so an adapter is required.
504+
// The variable is shared between the two lambdas so the same instance is removed on unsubscribe.
505+
Undo.UndoRedoCallback undoRedoAdapter = null;
506+
root.Add(InputAssetEditorUtils.CreateMakeActiveGui(
507+
() => InputSystem.settings, target as InputSettings,
508+
target.name, "settings", (value) => InputSystem.settings = value,
509+
handler =>
510+
{
511+
InputSystem.onSettingsChange += handler;
512+
undoRedoAdapter = () => handler();
513+
Undo.undoRedoPerformed += undoRedoAdapter;
514+
},
515+
handler =>
516+
{
517+
InputSystem.onSettingsChange -= handler;
518+
Undo.undoRedoPerformed -= undoRedoAdapter;
519+
}));
505520

506521
return root;
507522
}

0 commit comments

Comments
 (0)