I see that the GenericBinding struct and related Utility functions were introduced in Unity6, but I’m not sure when and where to use it. Referring to the official use cases, I thought it was a replacement for EditorCurveBinding in Runtime, but the current usage method is only to bind the animatable properties in components or scripts, and then set their values in Update, and it is not used with AnimationCurve. And it seems to be used separately from the Animation or Animator system. Is it because this function has not been fully developed, or I have misunderstood its usage?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEditor;
using Unity.Collections;
using System.Linq;
public class AnimationClipPlayer : MonoBehaviour
{
public AnimationClip animationClip;
public float time;
List<AnimationCurve> curves;
NativeArray<BoundProperty> floatProperties;
NativeArray<BoundProperty> intProperties;
NativeArray<float> floatValues;
NativeArray<int> intValues;
void Start()
{
var editorCurveBindings = AnimationUtility.GetCurveBindings(animationClip);
editorCurveBindings = editorCurveBindings.Where(editorCurveBinding =>
editorCurveBinding.type != typeof(Transform) && !editorCurveBinding.isPPtrCurve && !editorCurveBinding.isDiscreteCurve
).ToArray();
curves = new List<AnimationCurve>(editorCurveBindings.Length);
for (var i = 0; i < editorCurveBindings.Length; i++)
{
curves.Add(AnimationUtility.GetEditorCurve(animationClip, editorCurveBindings[i]));
}
var genericBindings = new NativeArray<GenericBinding>(AnimationUtility.EditorCurveBindingsToGenericBindings(editorCurveBindings), Allocator.Temp);
GenericBindingUtility.BindProperties(gameObject, genericBindings, out floatProperties, out intProperties, Allocator.Persistent);
floatValues = new NativeArray<float>(floatProperties.Length, Allocator.Persistent);
intValues = new NativeArray<int>(intProperties.Length, Allocator.Persistent);
}
private void OnDestroy()
{
floatProperties.Dispose();
floatValues.Dispose();
intProperties.Dispose();
intValues.Dispose();
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < curves.Count; i++)
{
floatValues[i] = curves[i].Evaluate(time);
}
GenericBindingUtility.SetValues(floatProperties, floatValues);
}
}