TransitionAssetT.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /// <inheritdoc/>
  8. /// https://kybernetik.com.au/animancer/api/Animancer/TransitionAsset_1
  9. [AnimancerHelpUrl(typeof(TransitionAsset<ITransitionDetailed>))]
  10. public class TransitionAsset<TTransition> : TransitionAssetBase
  11. where TTransition : ITransitionDetailed
  12. {
  13. /************************************************************************************************************************/
  14. [SerializeReference]
  15. private TTransition _Transition;
  16. /// <summary>[<see cref="SerializeReference"/>]
  17. /// The <see cref="ITransition"/> wrapped by this <see cref="ScriptableObject"/>.
  18. /// </summary>
  19. /// <remarks>
  20. /// WARNING: the <see cref="Transition{TState}.State"/> holds the most recently played state, so
  21. /// if you're sharing this transition between multiple objects it will only remember one of them.
  22. /// <para></para>
  23. /// You can use <see cref="AnimancerStateDictionary.GetOrCreate(ITransition)"/>
  24. /// or <see cref="AnimancerLayer.GetOrCreateState(ITransition)"/>
  25. /// to get or create the state for a specific object.
  26. /// </remarks>
  27. public TTransition Transition
  28. {
  29. get
  30. {
  31. AssertTransition();
  32. return _Transition;
  33. }
  34. set => _Transition = value;
  35. }
  36. /// <summary>Returns the <see cref="ITransition"/> wrapped by this <see cref="ScriptableObject"/>.</summary>
  37. public override ITransitionDetailed GetTransition()
  38. {
  39. AssertTransition();
  40. return _Transition;
  41. }
  42. /************************************************************************************************************************/
  43. /// <summary>[Assert-Conditional]
  44. /// Throws a <see cref="NullReferenceException"/> if the <see cref="Transition"/> is null.
  45. /// </summary>
  46. [System.Diagnostics.Conditional(Strings.Assertions)]
  47. private void AssertTransition()
  48. {
  49. if (_Transition == null)
  50. throw new NullReferenceException(
  51. $"'{name}' {nameof(Transition)} is null." +
  52. $" {nameof(HasTransition)} can be used to check without triggering this error.");
  53. }
  54. /************************************************************************************************************************/
  55. /// <inheritdoc/>
  56. public override bool IsValid => _Transition.IsValid();
  57. /// <inheritdoc/>
  58. public override float FadeDuration => _Transition.FadeDuration;
  59. /// <inheritdoc/>
  60. public override object Key => _Transition.Key;
  61. /// <inheritdoc/>
  62. public override FadeMode FadeMode => _Transition.FadeMode;
  63. /// <inheritdoc/>
  64. public override AnimancerState CreateState() => _Transition.CreateState();
  65. /// <inheritdoc/>
  66. public override void Apply(AnimancerState state)
  67. {
  68. _Transition.Apply(state);
  69. state.SetDebugName(this);
  70. }
  71. /// <inheritdoc/>
  72. public override void GetAnimationClips(List<AnimationClip> clips)
  73. => clips.GatherFromSource(_Transition);
  74. /************************************************************************************************************************/
  75. /// <summary>Is the <see cref="Transition"/> assigned (i.e. not <c>null</c>)?</summary>
  76. public bool HasTransition => _Transition != null;
  77. /************************************************************************************************************************/
  78. #if UNITY_EDITOR
  79. /// <summary>[Editor-Only]
  80. /// Assigns a default <typeparamref name="TTransition"/> to the <see cref="Transition"/> field.
  81. /// </summary>
  82. protected virtual void Reset()
  83. {
  84. _Transition = AnimancerReflection.CreateDefaultInstance<TTransition>();
  85. }
  86. #endif
  87. /************************************************************************************************************************/
  88. }
  89. }