The documentation page for Unity's Slider class (as of version 2021.3, which appears to be the latest version of this documentation) can be found here: https://docs.unity3d.com/2023.1/Documentation/ScriptReference/UIElements.Slider.html
This documentation does not give the one critical piece of information that, it seems to me, every potential user of the Slider class needs to know: the signature of its callback function.
One might guess that the callback signature was documented in the documentation for the Slider's RegisterCallback() function, but it is not. (See https://docs.unity3d.com/ScriptReference/UIElements.CallbackEventHandler.RegisterCallback.html.)
I assume that I am much stupider than the people who wrote this documentation, and therefore that the signature of the callback is documented somewhere else in Unity's official, up-to-date documentation.
I am NOT asking for third-party tutorials that may or may not be out of date, or which present some approach that may or may not break the next time Unity is updated. I am looking for Unity's official documentation of this callback's signature.
Where can I find the official documentation of this crucial factoid?
CodePudding user response:
You can peek at the Unity source code here, and solve all your questions about signatures.
About Slider,
In the same Unity's documentation page page that you shared, you can see that the method RegisterCallback
is under the grouper "Inherited Members". Inherited from whom? Well, at the top of the page, you can see in the inheritance breadcrumb "Inherits from:UIElements.BaseSlider_1", which is a link to the parent class. If you keep following up the inheritance chain, you will end up at the class CallbackEventHandler
, which is where RegisterCallback
is originally declared.
In the github page for CallbackEventHandler
, you can see that there are a few overloads for that method:
public void RegisterCallback<TEventType>(
EventCallback<TEventType> callback, TrickleDown useTrickleDown = TrickleDown.NoTrickleDown
) where TEventType : EventBase<TEventType>, new()
public void RegisterCallback<TEventType, TUserArgsType>(
EventCallback<TEventType, TUserArgsType> callback, TUserArgsType userArgs, TrickleDown useTrickleDown = TrickleDown.NoTrickleDown
) where TEventType : EventBase<TEventType>, new()
The interesting parameter here is callback
, that can be either EventCallback<TEventType>
(for events without arguments) or EventCallback<TEventType, TUserArgsType>
(for events with arguments), where TEventType
is EventBase
.
EventCallback
itself is a delegate:
public delegate void EventCallback<in TEventType>(TEventType evt)
public delegate void EventCallback<in TEventType, in TCallbackArgs>(TEventType evt, TCallbackArgs userArgs)
And EventBase
is the base class for all UIElements events, which are the events that you can ultimately register with the method RegisterCallback
. You can find the reference of available events in the "Event Reference" documentation page.
Specifically to Slider, you will probably be most interested in the ChangeEvent
. Interestingly, because Slider
implements INotifyValueChange<T>
, you'd better register your callback using RegisterValueChangedCallback
instead of RegisterCallback
. Something like this:
slider.RegisterValueChangedCallback(v => {
var oldValue = v.previousValue;
var newValue = v.newValue;
// (...) whatever your logic here
});