PlayableOutputRefresher.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine.Playables;
  3. namespace Animancer
  4. {
  5. /// <summary>A utility for re-assigning Animancer's <see cref="PlayableOutput"/>.</summary>
  6. ///
  7. /// <remarks>
  8. /// This should be totally useless, but for some reason it seems to fix an issue with Unity's
  9. /// Animation Rigging package. Normally, all of the Rig's parameters get reset to their
  10. /// starting values any time a playable is connected or disconnected (which Animancer does frequently),
  11. /// but using this utility effectively re-captures the starting values
  12. /// so any subsequent resets retain the values you set.
  13. /// <para></para>
  14. /// <strong>Example:</strong>
  15. /// <para></para><code>
  16. /// public class PlayableOutputRefresherExample : MonoBehaviour
  17. /// {
  18. /// [SerializeField] private AnimancerComponent _Animancer;
  19. /// [SerializeField] private Rig _Rig;
  20. ///
  21. /// // A field to store it in.
  22. /// private PlayableOutputRefresher _OutputRefresher;
  23. ///
  24. /// protected virtual void OnEnable()
  25. /// {
  26. /// // Initialize on startup.
  27. /// _OutputRefresher = new(_Animancer);
  28. /// }
  29. ///
  30. /// public void SetWeight(float weight)
  31. /// {
  32. /// // Change something that would be reset.
  33. /// _Rig.weight = weight;
  34. ///
  35. /// // Then call this afterwards.
  36. /// _OutputRefresher.Refresh();
  37. /// }
  38. /// }
  39. /// </code></remarks>
  40. ///
  41. /// https://kybernetik.com.au/animancer/api/Animancer/PlayableOutputRefresher
  42. ///
  43. public struct PlayableOutputRefresher
  44. {
  45. /************************************************************************************************************************/
  46. /// <summary>The <see cref="PlayableOutput"/> of Animancer's <see cref="PlayableGraph"/>.</summary>
  47. public PlayableOutput Output { get; set; }
  48. /// <summary>The root <see cref="Playable"/> of Animancer's <see cref="PlayableGraph"/>.</summary>
  49. public Playable Root { get; set; }
  50. /************************************************************************************************************************/
  51. /// <summary>Creates a new <see cref="PlayableOutputRefresher"/>.</summary>
  52. public PlayableOutputRefresher(PlayableOutput output)
  53. {
  54. Output = output;
  55. Root = Output.GetSourcePlayable();
  56. }
  57. /************************************************************************************************************************/
  58. /// <summary>Creates a new <see cref="PlayableOutputRefresher"/>.</summary>
  59. public PlayableOutputRefresher(AnimancerGraph animancer)
  60. : this(animancer.Output)
  61. { }
  62. /************************************************************************************************************************/
  63. /// <summary>Re-assigns the <see cref="Root"/> as the source playable of the <see cref="Output"/>.</summary>
  64. public readonly void Refresh()
  65. => Output.SetSourcePlayable(Root);
  66. /************************************************************************************************************************/
  67. /// <summary>Re-acquires the <see cref="Root"/> from the <see cref="Output"/>.</summary>
  68. /// <remarks>Call this after <see cref="AnimancerGraph.InsertOutputPlayable"/>.</remarks>
  69. public void OnSourcePlayableChanged()
  70. => Root = Output.GetSourcePlayable();
  71. /************************************************************************************************************************/
  72. }
  73. }