Hi!
I’m using UI Toolkit with PanelRenderer.OnPanelReload and I’m wondering what the recommended pattern is for registering callbacks and events.
For example, inside OnPanelReload I re-query my UI elements:
private void OnPanelReload()
{
_button = root.Q<Button>("MyButton");
_button.RegisterCallback<ClickEvent>(OnButtonClicked);
_button.clicked += OnClicked;
}
Should I always unregister callbacks and events before registering them again, for example:
_button.UnregisterCallback<ClickEvent>(OnButtonClicked);
_button.RegisterCallback<ClickEvent>(OnButtonClicked);
_button.clicked -= OnClicked;
_button.clicked += OnClicked;
Or is it expected that OnPanelReload provides new VisualElement instances, making explicit unregistration unnecessary?
I know that RegisterCallback<T>() ignores duplicate registrations on the same VisualElement, while clicked += is a regular C# event and will accumulate subscriptions. However, I couldn’t find any official guidance on the recommended lifecycle for OnPanelReload.
Is there an official recommendation or best practice from Unity for handling callback registration in OnPanelReload?
Thanks!