TransitionLibraryAsset.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Animancer.TransitionLibraries
  5. {
  6. /// <summary>[Pro-Only]
  7. /// A <see cref="ScriptableObject"/> which serializes a <see cref="TransitionLibraryDefinition"/>
  8. /// and creates a <see cref="TransitionLibrary"/> from it at runtime.
  9. /// </summary>
  10. /// <remarks>
  11. /// <strong>Documentation:</strong>
  12. /// <see href="https://kybernetik.com.au/animancer/docs/manual/transitions/libraries">
  13. /// Transition Libraries</see>
  14. /// </remarks>
  15. /// https://kybernetik.com.au/animancer/api/Animancer.TransitionLibraries/TransitionLibraryAsset
  16. [CreateAssetMenu(
  17. menuName = Strings.MenuPrefix + "Transition Library",
  18. order = Strings.AssetMenuOrder + 0)]
  19. [AnimancerHelpUrl(typeof(TransitionLibraryAsset))]
  20. public class TransitionLibraryAsset : ScriptableObject,
  21. IAnimationClipSource
  22. {
  23. /************************************************************************************************************************/
  24. [SerializeField]
  25. private TransitionLibraryDefinition _Definition;
  26. /// <summary>[<see cref="SerializeField"/>]
  27. /// The serialized data which will be used to initialize the <see cref="Library"/> at runtime.
  28. /// </summary>
  29. /// <remarks>
  30. /// If you modify the contents of this reference, either re-assign this property
  31. /// or call <see cref="OnDefinitionModified"/> to apply any changes to the <see cref="Library"/>.
  32. /// </remarks>
  33. public TransitionLibraryDefinition Definition
  34. {
  35. get => _Definition;
  36. set
  37. {
  38. _Definition = value ?? new();
  39. OnDefinitionModified();
  40. }
  41. }
  42. #if UNITY_EDITOR
  43. /// <summary>[Editor-Only] [Internal]
  44. /// The name of the field which stores the <see cref="Definition"/>.
  45. /// </summary>
  46. internal const string DefinitionField = nameof(_Definition);
  47. #endif
  48. /************************************************************************************************************************/
  49. /// <summary>The runtime <see cref="TransitionLibrary"/> created from the <see cref="Definition"/>.</summary>
  50. public TransitionLibrary Library { get; private set; }
  51. /************************************************************************************************************************/
  52. /// <summary>Initializes the <see cref="Library"/>.</summary>
  53. protected virtual void OnEnable()
  54. {
  55. _Definition ??= new();
  56. Library = new();
  57. Library.Initialize(_Definition);
  58. }
  59. /************************************************************************************************************************/
  60. /// <summary>
  61. /// Adds the contents of the <see cref="Definition"/>
  62. /// to the <see cref="Library"/> if it was already initialized.
  63. /// </summary>
  64. /// <remarks>
  65. /// Call this after modifying the contents of the <see cref="Definition"/>
  66. /// to ensure that the <see cref="Library"/> reflects any changes.
  67. /// <para></para>
  68. /// Note that this doesn't remove anything from the <see cref="Library"/>,
  69. /// it only adds or replaces values.
  70. /// </remarks>
  71. public void OnDefinitionModified()
  72. {
  73. Library.Initialize(_Definition);
  74. }
  75. /************************************************************************************************************************/
  76. /// <summary>Gathers all the animations in the <see cref="Definition"/> and <see cref="Library"/>.</summary>
  77. public void GetAnimationClips(List<AnimationClip> results)
  78. {
  79. results.GatherFromSource(_Definition);
  80. results.GatherFromSource(Library);
  81. }
  82. /************************************************************************************************************************/
  83. }
  84. }