TransitionAssetReference.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace Animancer
  6. {
  7. /// <summary>A [<see cref="SerializableAttribute"/>] wrapper around an <see cref="TransitionAssetBase"/>.</summary>
  8. /// <remarks>
  9. /// This allows Transition Assets to be referenced inside [<see cref="SerializeReference"/>]
  10. /// fields which can't directly reference <see cref="UnityEngine.Object"/>s.
  11. /// <para></para>
  12. /// <strong>Documentation:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/manual/transitions/assets">
  14. /// Transition Assets</see>
  15. /// </remarks>
  16. /// https://kybernetik.com.au/animancer/api/Animancer/TransitionAssetReference
  17. [Serializable]
  18. public class TransitionAssetReference :
  19. IAnimationClipSource,
  20. ICopyable<TransitionAssetReference>,
  21. IPolymorphic,
  22. ITransitionDetailed,
  23. IWrapper
  24. {
  25. /************************************************************************************************************************/
  26. [SerializeField]
  27. private TransitionAssetBase _Asset;
  28. /// <summary>[<see cref="SerializeField"/>] The wrapped Transition Asset.</summary>
  29. public ref TransitionAssetBase Asset
  30. => ref _Asset;
  31. /************************************************************************************************************************/
  32. /// <summary>Creates a new <see cref="TransitionAssetReference"/>.</summary>
  33. public TransitionAssetReference() { }
  34. /// <summary>Creates a new <see cref="TransitionAssetReference"/>.</summary>
  35. public TransitionAssetReference(TransitionAssetBase asset)
  36. {
  37. _Asset = asset;
  38. }
  39. /************************************************************************************************************************/
  40. /// <inheritdoc/>
  41. object IWrapper.WrappedObject
  42. => _Asset;
  43. /************************************************************************************************************************/
  44. /// <summary>Can this transition create a valid <see cref="AnimancerState"/>?</summary>
  45. public virtual bool IsValid
  46. => _Asset.IsValid();
  47. /// <inheritdoc/>
  48. public virtual float FadeDuration
  49. => _Asset != null
  50. ? _Asset.FadeDuration
  51. : 0;
  52. /// <inheritdoc/>
  53. public virtual object Key
  54. => _Asset != null
  55. ? _Asset.Key
  56. : null;
  57. /// <inheritdoc/>
  58. public virtual FadeMode FadeMode
  59. => _Asset != null
  60. ? _Asset.FadeMode
  61. : default;
  62. /// <inheritdoc/>
  63. public bool IsLooping
  64. => _Asset != null
  65. && _Asset.IsLooping;
  66. /// <inheritdoc/>
  67. public float NormalizedStartTime
  68. {
  69. get => _Asset != null
  70. ? _Asset.NormalizedStartTime
  71. : float.NaN;
  72. set => _Asset.NormalizedStartTime = value;// No null check. Don't silently ignore commands.
  73. }
  74. /// <inheritdoc/>
  75. public float MaximumDuration
  76. => _Asset != null
  77. ? _Asset.MaximumDuration
  78. : 0;
  79. /// <inheritdoc/>
  80. public float Speed
  81. {
  82. get => _Asset != null
  83. ? _Asset.Speed
  84. : 1;
  85. set => _Asset.Speed = value;// No null check. Don't silently ignore commands.
  86. }
  87. /// <inheritdoc/>
  88. public virtual AnimancerState CreateState()
  89. => _Asset.CreateState();
  90. /// <inheritdoc/>
  91. public virtual void Apply(AnimancerState state)
  92. => _Asset.Apply(state);
  93. /************************************************************************************************************************/
  94. /// <summary>[<see cref="IAnimationClipSource"/>]
  95. /// Calls <see cref="AnimancerUtilities.GatherFromSource(ICollection{AnimationClip}, object)"/>.
  96. /// </summary>
  97. public virtual void GetAnimationClips(List<AnimationClip> clips)
  98. => clips.GatherFromSource(_Asset);
  99. /************************************************************************************************************************/
  100. /// <inheritdoc/>
  101. public void CopyFrom(TransitionAssetReference copyFrom, CloneContext context)
  102. {
  103. if (copyFrom == null)
  104. {
  105. _Asset = default;
  106. return;
  107. }
  108. _Asset = copyFrom._Asset;
  109. }
  110. /************************************************************************************************************************/
  111. /// <summary>Describes the <see cref="Asset"/>.</summary>
  112. public override string ToString()
  113. => $"{nameof(TransitionAssetReference)}({_Asset})";
  114. /************************************************************************************************************************/
  115. }
  116. }