AnimancerTransitionAssetBase.UnShared.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using UnityEngine;
  3. using System;
  4. #if UNITY_EDITOR
  5. using Animancer.Editor;
  6. using UnityEditor;
  7. #endif
  8. namespace Animancer
  9. {
  10. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerTransitionAssetBase
  11. ///
  12. public partial class AnimancerTransitionAssetBase
  13. {
  14. /************************************************************************************************************************/
  15. /// <inheritdoc/>
  16. [Serializable]
  17. public class UnShared : UnShared<AnimancerTransitionAssetBase> { }
  18. /// <summary>
  19. /// An <see cref="AnimancerTransitionAsset{TTransition}"/> wrapper which stores its own <see cref="BaseState"/>
  20. /// and <see cref="Events"/> to allow multiple objects to reference the same transition asset without
  21. /// interfering with each other.
  22. /// </summary>
  23. /// <remarks>
  24. /// Documentation: <see href="https://kybernetik.com.au/animancer/docs/manual/transitions/assets#unshared">
  25. /// Transition Assets - UnShared</see>
  26. /// </remarks>
  27. /// https://kybernetik.com.au/animancer/api/Animancer/UnShared_1
  28. ///
  29. [Serializable]
  30. public class UnShared<TAsset> : ITransition, ITransitionWithEvents, IWrapper
  31. where TAsset : AnimancerTransitionAssetBase
  32. {
  33. /************************************************************************************************************************/
  34. [SerializeField]
  35. private TAsset _Asset;
  36. /// <summary>The <see cref="AnimancerTransitionAssetBase"/> wrapped by this object.</summary>
  37. public TAsset Asset
  38. {
  39. get
  40. {
  41. AssertAsset();
  42. return _Asset;
  43. }
  44. set
  45. {
  46. _Asset = value;
  47. BaseState = null;
  48. ClearCachedEvents();
  49. }
  50. }
  51. /// <inheritdoc/>
  52. object IWrapper.WrappedObject => _Asset;
  53. /// <summary>The <see cref="ITransition"/> wrapped by this object.</summary>
  54. public ITransition BaseTransition => _Asset.GetTransition();
  55. /************************************************************************************************************************/
  56. /// <summary>Can this transition create a valid <see cref="AnimancerState"/>?</summary>
  57. public virtual bool IsValid
  58. {
  59. get
  60. {
  61. AssertAsset();
  62. return _Asset.IsValid();
  63. }
  64. }
  65. /************************************************************************************************************************/
  66. /// <summary>Is the <see cref="Asset"/> assigned (i.e. not <c>null</c>)?</summary>
  67. public bool HasAsset => _Asset != null;
  68. /************************************************************************************************************************/
  69. /// <summary>[Assert-Conditional] Logs an error if the <see cref="Asset"/> is null.</summary>
  70. [System.Diagnostics.Conditional(Strings.Assertions)]
  71. private void AssertAsset()
  72. {
  73. if (_Asset == null)
  74. Debug.LogError($"{GetType().Name}.{nameof(Asset)} is not assigned." +
  75. $" {nameof(HasAsset)} can be used to check without triggering this error.");
  76. }
  77. /************************************************************************************************************************/
  78. private AnimancerState _BaseState;
  79. /// <summary>
  80. /// The state that was created by this object. Specifically, this is the state that was most recently
  81. /// passed into <see cref="Apply"/> (usually by <see cref="AnimancerPlayable.Play(ITransition)"/>).
  82. /// <para></para>
  83. /// You can use <see cref="AnimancerPlayable.StateDictionary.GetOrCreate(ITransition)"/> or
  84. /// <see cref="AnimancerLayer.GetOrCreateState(ITransition)"/> to get or create the state for a
  85. /// specific object.
  86. /// <para></para>
  87. /// <see cref="State"/> is simply a shorthand for casting this to <typeparamref name="TState"/>.
  88. /// </summary>
  89. public AnimancerState BaseState
  90. {
  91. get => _BaseState;
  92. protected set
  93. {
  94. _BaseState = value;
  95. OnSetBaseState();
  96. }
  97. }
  98. /// <summary>Called when the <see cref="BaseState"/> is set.</summary>
  99. protected virtual void OnSetBaseState() { }
  100. /************************************************************************************************************************/
  101. private AnimancerEvent.Sequence _Events;
  102. /// <inheritdoc/>
  103. /// <remarks>This property stores a copy of the events from the <see cref="Transition"/>.</remarks>
  104. public virtual AnimancerEvent.Sequence Events
  105. {
  106. get
  107. {
  108. if (_Events == null)
  109. _Events = new AnimancerEvent.Sequence(SerializedEvents.GetEventsOptional());
  110. return _Events;
  111. }
  112. }
  113. /// <inheritdoc/>
  114. public virtual ref AnimancerEvent.Sequence.Serializable SerializedEvents
  115. {
  116. get
  117. {
  118. AssertAsset();
  119. return ref ((ITransitionWithEvents)_Asset.GetTransition()).SerializedEvents;
  120. }
  121. }
  122. /// <summary>
  123. /// Clears the cached <see cref="Events"/> so that next time they are accessed they will be copied from the
  124. /// <see cref="SerializedEvents"/> again.
  125. /// </summary>
  126. public void ClearCachedEvents()
  127. {
  128. _Events = null;
  129. }
  130. /************************************************************************************************************************/
  131. /// <summary>Wraps <see cref="ITransition.Apply"/> and assigns the local <see cref="Events"/>.</summary>
  132. public virtual void Apply(AnimancerState state)
  133. {
  134. BaseState = state;
  135. _Asset.Apply(state);
  136. if (_Events == null)
  137. {
  138. _Events = SerializedEvents.GetEventsOptional();
  139. if (_Events == null)
  140. return;
  141. _Events = new AnimancerEvent.Sequence(_Events);
  142. }
  143. state.Events = _Events;
  144. }
  145. /************************************************************************************************************************/
  146. /// <inheritdoc/>
  147. public virtual object Key
  148. {
  149. get
  150. {
  151. AssertAsset();
  152. return _Asset.Key;
  153. }
  154. }
  155. /// <inheritdoc/>
  156. public virtual float FadeDuration
  157. {
  158. get
  159. {
  160. AssertAsset();
  161. return _Asset.FadeDuration;
  162. }
  163. }
  164. /// <inheritdoc/>
  165. public virtual FadeMode FadeMode
  166. {
  167. get
  168. {
  169. AssertAsset();
  170. return _Asset.FadeMode;
  171. }
  172. }
  173. /// <inheritdoc/>
  174. AnimancerState ITransition.CreateState()
  175. {
  176. AssertAsset();
  177. return BaseState = _Asset.CreateState();
  178. }
  179. /************************************************************************************************************************/
  180. }
  181. /************************************************************************************************************************/
  182. /// <inheritdoc/>
  183. [Serializable]
  184. public class UnShared<TAsset, TTransition, TState> : UnShared<TAsset>, ITransition<TState>
  185. where TAsset : AnimancerTransitionAsset<TTransition>
  186. where TTransition : ITransition<TState>, IHasEvents
  187. where TState : AnimancerState
  188. {
  189. /************************************************************************************************************************/
  190. /// <summary>The <see cref="ITransition"/> wrapped by this object.</summary>
  191. public TTransition Transition
  192. {
  193. get => Asset.Transition;
  194. set => Asset.Transition = value;
  195. }
  196. /************************************************************************************************************************/
  197. /// <inheritdoc/>
  198. protected override void OnSetBaseState()
  199. {
  200. base.OnSetBaseState();
  201. if (_State != BaseState)
  202. _State = null;
  203. }
  204. /************************************************************************************************************************/
  205. private TState _State;
  206. /// <summary>
  207. /// The state that was created by this object. Specifically, this is the state that was most recently
  208. /// passed into <see cref="Apply"/> (usually by <see cref="AnimancerPlayable.Play(ITransition)"/>).
  209. /// </summary>
  210. ///
  211. /// <remarks>
  212. /// You can use <see cref="AnimancerPlayable.StateDictionary.GetOrCreate(ITransition)"/> or
  213. /// <see cref="AnimancerLayer.GetOrCreateState(ITransition)"/> to get or create the state for a
  214. /// specific object.
  215. /// <para></para>
  216. /// This property is shorthand for casting the <see cref="BaseState"/> to <typeparamref name="TState"/>.
  217. /// </remarks>
  218. ///
  219. /// <exception cref="InvalidCastException">
  220. /// The <see cref="BaseState"/> is not actually a <typeparamref name="TState"/>. This should only
  221. /// happen if a different type of state was created by something else and registered using the
  222. /// <see cref="Key"/>, causing this <see cref="AnimancerPlayable.Play(ITransition)"/> to pass that
  223. /// state into <see cref="Apply"/> instead of calling <see cref="CreateState"/> to make the correct type of
  224. /// state.
  225. /// </exception>
  226. public TState State
  227. {
  228. get
  229. {
  230. if (_State == null)
  231. _State = (TState)BaseState;
  232. return _State;
  233. }
  234. protected set
  235. {
  236. BaseState = _State = value;
  237. }
  238. }
  239. /************************************************************************************************************************/
  240. /// <inheritdoc/>
  241. public override ref AnimancerEvent.Sequence.Serializable SerializedEvents
  242. => ref Asset.Transition.SerializedEvents;
  243. /************************************************************************************************************************/
  244. /// <inheritdoc/>
  245. public virtual TState CreateState()
  246. => State = (TState)Asset.CreateState();
  247. /************************************************************************************************************************/
  248. }
  249. /************************************************************************************************************************/
  250. #if UNITY_EDITOR
  251. /// <summary>[Editor-Only]
  252. /// A <see cref="PropertyDrawer"/> for <see cref="UnShared{TAsset}"/>.
  253. /// </summary>
  254. /// <remarks>
  255. /// This class doesn't inherit from <see cref="TransitionDrawer"/> (which would let it draw the button to open the
  256. /// <see cref="TransitionPreviewWindow"/>) because it would only show the Transition Asset reference field without
  257. /// any of the actual values (fade duration, speed, etc.) and trying to redirect everything necessary to preview
  258. /// the asset's transition would require significant additional complexity.
  259. /// <para></para>
  260. /// This issue can be avoided using the
  261. /// <see href="https://kybernetik.com.au/inspector-gadgets/docs/script-inspector/object-reference-fields#nested-inspector">
  262. /// Nested Inspectors in Inspector Gadgets</see> by opening the asset's Inspector and previewing it directly.
  263. /// </remarks>
  264. [CustomPropertyDrawer(typeof(UnShared<>), true)]
  265. public class UnSharedTransitionDrawer : PropertyDrawer
  266. {
  267. /************************************************************************************************************************/
  268. /// <inheritdoc/>
  269. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  270. {
  271. var height = AnimancerGUI.LineHeight;
  272. if (property.propertyType == SerializedPropertyType.ManagedReference &&
  273. property.isExpanded)
  274. height += AnimancerGUI.LineHeight + AnimancerGUI.StandardSpacing;
  275. return height;
  276. }
  277. /************************************************************************************************************************/
  278. /// <inheritdoc/>
  279. public override void OnGUI(Rect area, SerializedProperty property, GUIContent label)
  280. {
  281. if (property.propertyType == SerializedPropertyType.ManagedReference)
  282. {
  283. using (new TypeSelectionButton(area, property, true))
  284. EditorGUI.PropertyField(area, property, label, true);
  285. }
  286. else
  287. {
  288. var transitionProperty = property.FindPropertyRelative("_Asset");
  289. EditorGUI.PropertyField(area, transitionProperty, label, false);
  290. }
  291. }
  292. /************************************************************************************************************************/
  293. }
  294. #endif
  295. /************************************************************************************************************************/
  296. }
  297. }