Version: 2023.1

GenericBindingUtility

class in UnityEngine.Animations

切换到手册

描述

Animation utility functions for reading and writing values from Unity components.

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) &amp;&amp; !editorCurveBinding.isPPtrCurve &amp;&amp; !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); } }

静态函数

BindPropertiesRetrieves the list of BoundProperty defined by the list of GenericBinding.
CreateGenericBindingRetrieves the GenericBinding that represents a specific property associated with a GameObject or one of its components.
GetAnimatableBindingsRetrieves the animatable bindings for a specific GameObject.
GetCurveBindingsRetrieves the curve bindings from an animation clip.
GetValuesRetrieves the float or integer value for each [[BoundProperty].
SetValuesSets the float or integer value for each [[BoundProperty].
UnbindPropertiesUnbinds and frees all resources used by a list of BoundProperty.