AnimatedProperty.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.Animations;
  5. using Unity.Collections;
  6. namespace Animancer
  7. {
  8. /// <summary>[Pro-Only]
  9. /// A base wrapper which allows access to the value of properties that are controlled by animations.
  10. /// </summary>
  11. /// <remarks>
  12. /// Documentation: <see href="https://kybernetik.com.au/animancer/docs/manual/ik#animated-properties">Animated Properties</see>
  13. /// </remarks>
  14. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/jobs">Animation Jobs</see></example>
  15. /// https://kybernetik.com.au/animancer/api/Animancer/AnimatedProperty_2
  16. ///
  17. public abstract class AnimatedProperty<TJob, TValue> : AnimancerJob<TJob>, IDisposable
  18. where TJob : struct, IAnimationJob
  19. where TValue : struct
  20. {
  21. /************************************************************************************************************************/
  22. /// <summary>The properties wrapped by this object.</summary>
  23. protected NativeArray<PropertyStreamHandle> _Properties;
  24. /// <summary>The value of each of the <see cref="_Properties"/> from the most recent update.</summary>
  25. protected NativeArray<TValue> _Values;
  26. /************************************************************************************************************************/
  27. #region Initialisation
  28. /************************************************************************************************************************/
  29. /// <summary>
  30. /// Allocates room for a specified number of properties to be filled by
  31. /// <see cref="InitializeProperty(int, Transform, Type, string)"/>.
  32. /// </summary>
  33. public AnimatedProperty(IAnimancerComponent animancer, int propertyCount,
  34. NativeArrayOptions options = NativeArrayOptions.ClearMemory)
  35. {
  36. _Properties = new NativeArray<PropertyStreamHandle>(propertyCount, Allocator.Persistent, options);
  37. _Values = new NativeArray<TValue>(propertyCount, Allocator.Persistent);
  38. CreateJob();
  39. var playable = animancer.Playable;
  40. CreatePlayable(playable);
  41. playable.Disposables.Add(this);
  42. }
  43. /// <summary>Initializes a single property.</summary>
  44. public AnimatedProperty(IAnimancerComponent animancer, string propertyName)
  45. : this(animancer, 1, NativeArrayOptions.UninitializedMemory)
  46. {
  47. var animator = animancer.Animator;
  48. _Properties[0] = animator.BindStreamProperty(animator.transform, typeof(Animator), propertyName);
  49. }
  50. /// <summary>Initializes a group of properties.</summary>
  51. public AnimatedProperty(IAnimancerComponent animancer, params string[] propertyNames)
  52. : this(animancer, propertyNames.Length, NativeArrayOptions.UninitializedMemory)
  53. {
  54. var count = propertyNames.Length;
  55. var animator = animancer.Animator;
  56. var transform = animator.transform;
  57. for (int i = 0; i < count; i++)
  58. InitializeProperty(animator, i, transform, typeof(Animator), propertyNames[i]);
  59. }
  60. /************************************************************************************************************************/
  61. /// <summary>Initializes a property on the target <see cref="Animator"/>.</summary>
  62. public void InitializeProperty(Animator animator, int index, string name)
  63. => InitializeProperty(animator, index, animator.transform, typeof(Animator), name);
  64. /// <summary>Initializes the specified `index` to read a property with the specified `name`.</summary>
  65. public void InitializeProperty(Animator animator, int index, Transform transform, Type type, string name)
  66. => _Properties[index] = animator.BindStreamProperty(transform, type, name);
  67. /************************************************************************************************************************/
  68. /// <summary>Creates and assigns the <see cref="AnimancerJob._Job"/>.</summary>
  69. protected abstract void CreateJob();
  70. /************************************************************************************************************************/
  71. #endregion
  72. /************************************************************************************************************************/
  73. #region Accessors
  74. /************************************************************************************************************************/
  75. /// <summary>Returns the value of the first property.</summary>
  76. public TValue Value => this[0];
  77. /// <summary>Returns the value of the first property.</summary>
  78. public static implicit operator TValue(AnimatedProperty<TJob, TValue> properties) => properties[0];
  79. /************************************************************************************************************************/
  80. /// <summary>Returns the value of the property at the specified `index`.</summary>
  81. /// <remarks>This method is identical to <see cref="this[int]"/>.</remarks>
  82. public TValue GetValue(int index) => _Values[index];
  83. /// <summary>Returns the value of the property at the specified `index`.</summary>
  84. /// <remarks>This indexer is identical to <see cref="GetValue(int)"/>.</remarks>
  85. public TValue this[int index] => _Values[index];
  86. /************************************************************************************************************************/
  87. /// <summary>Resizes the `values` if necessary and copies the value of each property into it.</summary>
  88. public void GetValues(ref TValue[] values)
  89. {
  90. AnimancerUtilities.SetLength(ref values, _Values.Length);
  91. _Values.CopyTo(values);
  92. }
  93. /// <summary>Returns a new array containing the values of all properties.</summary>
  94. /// <remarks>Use <see cref="GetValues(ref TValue[])"/> to avoid allocating a new array every call.</remarks>
  95. public TValue[] GetValues()
  96. {
  97. var values = new TValue[_Values.Length];
  98. _Values.CopyTo(values);
  99. return values;
  100. }
  101. /************************************************************************************************************************/
  102. #endregion
  103. /************************************************************************************************************************/
  104. void IDisposable.Dispose() => Dispose();
  105. /// <summary>Cleans up the <see cref="NativeArray{T}"/>s.</summary>
  106. /// <remarks>Called by <see cref="AnimancerPlayable.OnPlayableDestroy"/>.</remarks>
  107. protected virtual void Dispose()
  108. {
  109. if (_Properties.IsCreated)
  110. {
  111. _Properties.Dispose();
  112. _Values.Dispose();
  113. }
  114. }
  115. /// <summary>Destroys the <see cref="_Playable"/> and restores the graph connection it was intercepting.</summary>
  116. public override void Destroy()
  117. {
  118. Dispose();
  119. base.Destroy();
  120. }
  121. /************************************************************************************************************************/
  122. }
  123. }