BindingUtility
Utility functions to find the index of a binding or a socket from its identifier.
Read time 2 minutesLast updated 3 hours ago
Definition
- Type: Class
- Namespace: Unity.Animations
- Assembly: UnityEngine.AnimationCoreModule
public static class BindingUtility
Remarks
Rigs and clips store the identifiers of their bindings in arrays of StringHash, and a rig stores its sockets, each with its identifier, in RigDefinition.SocketValues. Use these functions to turn an identifier into an index into one of these arrays, for example to match a binding of a clip with a binding of a rig.
The index that BindingUtility.FindBindingIndex returns is the position in the array that you search. For a rig, that is the index among the bindings of one kind, such as BindingSet.TranslationBindings. To get the index among all the bindings of the rig, add the first-binding index of that kind, such as BindingSet.TranslationBindingIndex.
Both functions search the array linearly and return -1 if nothing matches.
The following example uses the and namespaces. It finds a float channel by its identifier, then reads and writes its value in an animation stream.
Unity.AnimationsUnity.EntitiesAdditional Resources: BindingSet
Examples
// This example finds a float channel by its identifier, then increases its value in the animation stream.// Returns false if the rig has no float channel with that identifier.public static bool TryIncreaseFloatChannel( ref AnimationStream stream, StringHash channelId, float amount, out float newValue){ // Search the float bindings of the rig of the stream. FindBindingIndex returns -1 when there is no match. ref BindingSet bindings = ref stream.RigDefinition.Bindings; int channelIndex = BindingUtility.FindBindingIndex(ref bindings.FloatBindings, channelId); if (channelIndex < 0) { newValue = 0f; return false; } // The binding index is also the index of the float in the stream. newValue = stream.GetFloat(channelIndex) + amount; stream.SetFloat(channelIndex, newValue); return true;}
Static Methods
Method | Description |
|---|---|
| FindBindingIndex | Find a binding in a binding array. |
| FindSocketIndex | Finds the index of the socket that matches a binding in an array of sockets. |