I have a TextField UI element that I bind to different variables based on what is selected. This field has a bunch of data it has to process at RegisterValueChangedCallback.
Currently I’m passing lambda functions as callbacks, but as there doesn’t seem to be a way to clear all previous callbacks of an UI element, I would have to store references to the callbacks I pass.
In the code below, what type of List would I have to create to store the reference to the EventCallback<ChangeEvent>? I keep getting error of the unknown T if I try to make a List outside that method (I’m sure it’s by design, but I’m not so good with generics).
Or is there a way to clear all callbacks at once, like reset them?
Or as a third option, can I somehow pass custom arguments to the callback without using a lambda? Maybe some kind of a wrapper function or delegate? In this case, how can I store references to those into a list, how would that look like?
This is where I’m at currently. The goal is to be able to unregister and then reregister the callback when I have new arguments and data. Any ideas are welcome!
private void _RegisterStaticSerializedPropertyOnChangeCallback<T>(BaseField<T> UIElement, SerializedObject serializedObject, SerializedProperty serializedProperty, long entityID, Fjylling.ServiceDataType originDataType, bool valueAffectsActionOrTaskLength = false, bool setupOnChangeCallback = true, Action performActionsBeforeRecalculate = null)
{
if (UIElement.IsBound()) UIElement.Unbind();
UIElement.BindProperty(serializedProperty);
// Save a reference to the bound element
_boundElements.Add(UIElement);
if (setupOnChangeCallback)
{
EventCallback<ChangeEvent<T>> changeEvent = (e) => {
// Performed when a value is changed
serializedObject.ApplyModifiedProperties();
if (!UnityEngine.Object.Equals(e.newValue, e.previousValue))
{
// This might change settings
performActionsBeforeRecalculate?.Invoke();
serializedObject.ApplyModifiedProperties();
if (valueAffectsActionOrTaskLength) _CalculateAndUpdateActionLengths(originDataType, true);
EntityService.instance.MarkDirty(entityID, ServiceType.Editor, ServiceType.Editor, originDataType);
EntityService.instance.NotifyChanges();
};
};
_serializedPropertyCallbacks<T>.Add(changeEvent);
// Register callback
UIElement.RegisterValueChangedCallback(changeEvent);
}
}