123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System;
- using UnityEngine;
- using UnityEngine.Animations;
- using Unity.Collections;
- namespace Animancer
- {
-
-
-
-
-
-
-
-
-
- public abstract class AnimatedProperty<TJob, TValue> : AnimancerJob<TJob>, IDisposable
- where TJob : struct, IAnimationJob
- where TValue : struct
- {
-
-
- protected NativeArray<PropertyStreamHandle> _Properties;
-
- protected NativeArray<TValue> _Values;
-
- #region Initialisation
-
-
-
-
-
- public AnimatedProperty(IAnimancerComponent animancer, int propertyCount,
- NativeArrayOptions options = NativeArrayOptions.ClearMemory)
- {
- _Properties = new NativeArray<PropertyStreamHandle>(propertyCount, Allocator.Persistent, options);
- _Values = new NativeArray<TValue>(propertyCount, Allocator.Persistent);
- CreateJob();
- var playable = animancer.Playable;
- CreatePlayable(playable);
- playable.Disposables.Add(this);
- }
-
- public AnimatedProperty(IAnimancerComponent animancer, string propertyName)
- : this(animancer, 1, NativeArrayOptions.UninitializedMemory)
- {
- var animator = animancer.Animator;
- _Properties[0] = animator.BindStreamProperty(animator.transform, typeof(Animator), propertyName);
- }
-
- public AnimatedProperty(IAnimancerComponent animancer, params string[] propertyNames)
- : this(animancer, propertyNames.Length, NativeArrayOptions.UninitializedMemory)
- {
- var count = propertyNames.Length;
- var animator = animancer.Animator;
- var transform = animator.transform;
- for (int i = 0; i < count; i++)
- InitializeProperty(animator, i, transform, typeof(Animator), propertyNames[i]);
- }
-
-
- public void InitializeProperty(Animator animator, int index, string name)
- => InitializeProperty(animator, index, animator.transform, typeof(Animator), name);
-
- public void InitializeProperty(Animator animator, int index, Transform transform, Type type, string name)
- => _Properties[index] = animator.BindStreamProperty(transform, type, name);
-
-
- protected abstract void CreateJob();
-
- #endregion
-
- #region Accessors
-
-
- public TValue Value => this[0];
-
- public static implicit operator TValue(AnimatedProperty<TJob, TValue> properties) => properties[0];
-
-
-
- public TValue GetValue(int index) => _Values[index];
-
-
- public TValue this[int index] => _Values[index];
-
-
- public void GetValues(ref TValue[] values)
- {
- AnimancerUtilities.SetLength(ref values, _Values.Length);
- _Values.CopyTo(values);
- }
-
-
- public TValue[] GetValues()
- {
- var values = new TValue[_Values.Length];
- _Values.CopyTo(values);
- return values;
- }
-
- #endregion
-
- void IDisposable.Dispose() => Dispose();
-
-
- protected virtual void Dispose()
- {
- if (_Properties.IsCreated)
- {
- _Properties.Dispose();
- _Values.Dispose();
- }
- }
-
- public override void Destroy()
- {
- Dispose();
- base.Destroy();
- }
-
- }
- }
|