TransitionPreviewWindow.Animations.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #if UNITY_EDITOR
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using Object = UnityEngine.Object;
  8. namespace Animancer.Editor
  9. {
  10. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/TransitionPreviewWindow
  11. partial class TransitionPreviewWindow
  12. {
  13. /// <summary>Animation details for the <see cref="TransitionPreviewWindow"/>.</summary>
  14. /// <remarks>
  15. /// Documentation: <see href="https://kybernetik.com.au/animancer/docs/manual/transitions#previews">Previews</see>
  16. /// </remarks>
  17. [Serializable]
  18. private class Animations
  19. {
  20. /************************************************************************************************************************/
  21. public const string
  22. PreviousAnimationKey = "Previous Animation",
  23. NextAnimationKey = "Next Animation";
  24. /************************************************************************************************************************/
  25. [NonSerialized] private AnimationClip[] _OtherAnimations;
  26. [SerializeField]
  27. private AnimationClip _PreviousAnimation;
  28. public AnimationClip PreviousAnimation => _PreviousAnimation;
  29. [SerializeField]
  30. private AnimationClip _NextAnimation;
  31. public AnimationClip NextAnimation => _NextAnimation;
  32. /************************************************************************************************************************/
  33. public void DoGUI()
  34. {
  35. GUILayout.BeginVertical(GUI.skin.box);
  36. EditorGUILayout.LabelField("Preview Details", "(Not Serialized)");
  37. DoModelGUI();
  38. DoAnimatorSelectorGUI();
  39. using (ObjectPool.Disposable.AcquireContent(out var label, "Previous Animation",
  40. "The animation for the preview to play before the target transition"))
  41. {
  42. DoAnimationFieldGUI(label, ref _PreviousAnimation, (clip) => _PreviousAnimation = clip);
  43. }
  44. var animancer = _Instance._Scene.Animancer;
  45. DoCurrentAnimationGUI(animancer);
  46. using (ObjectPool.Disposable.AcquireContent(out var label, "Next Animation",
  47. "The animation for the preview to play after the target transition"))
  48. {
  49. DoAnimationFieldGUI(label, ref _NextAnimation, (clip) => _NextAnimation = clip);
  50. }
  51. if (animancer != null)
  52. {
  53. if (animancer.IsGraphPlaying)
  54. {
  55. if (GUILayout.Button("Pause", EditorStyles.miniButton))
  56. animancer.PauseGraph();
  57. }
  58. else
  59. {
  60. using (new EditorGUI.DisabledScope(!Transition.IsValid()))
  61. {
  62. if (GUILayout.Button("Play Transition", EditorStyles.miniButton))
  63. {
  64. if (_PreviousAnimation != null && _PreviousAnimation.length > 0)
  65. {
  66. _Instance._Scene.Animancer.Stop();
  67. var fromState = animancer.States.GetOrCreate(PreviousAnimationKey, _PreviousAnimation, true);
  68. animancer.Play(fromState);
  69. OnPlayAnimation();
  70. fromState.Time = 0;
  71. var warnings = OptionalWarning.UnsupportedEvents.DisableTemporarily();
  72. fromState.Events.EndEvent = new AnimancerEvent(1 / fromState.Length, PlayTransition);
  73. warnings.Enable();
  74. }
  75. else
  76. {
  77. PlayTransition();
  78. }
  79. _Instance._Scene.Animancer.UnpauseGraph();
  80. }
  81. }
  82. }
  83. }
  84. GUILayout.EndVertical();
  85. }
  86. /************************************************************************************************************************/
  87. private void DoModelGUI()
  88. {
  89. var model = _Instance._Scene.OriginalRoot != null ? _Instance._Scene.OriginalRoot.gameObject : null;
  90. EditorGUI.BeginChangeCheck();
  91. var warning = GetModelWarning(model);
  92. var color = GUI.color;
  93. if (warning != null)
  94. GUI.color = AnimancerGUI.WarningFieldColor;
  95. using (ObjectPool.Disposable.AcquireContent(out var label, "Model"))
  96. {
  97. if (DoDropdownObjectField(label, true, ref model, AnimancerGUI.SpacingMode.After))
  98. {
  99. var menu = new GenericMenu();
  100. menu.AddItem(new GUIContent("Default Humanoid"), Settings.IsDefaultHumanoid(model),
  101. () => _Instance._Scene.OriginalRoot = Settings.DefaultHumanoid.transform);
  102. menu.AddItem(new GUIContent("Default Sprite"), Settings.IsDefaultSprite(model),
  103. () => _Instance._Scene.OriginalRoot = Settings.DefaultSprite.transform);
  104. var persistentModels = Settings.Models;
  105. var temporaryModels = TemporarySettings.PreviewModels;
  106. if (persistentModels.Count == 0 && temporaryModels.Count == 0)
  107. {
  108. menu.AddDisabledItem(new GUIContent("No model prefabs have been used yet"));
  109. }
  110. else
  111. {
  112. AddModelSelectionFunctions(menu, persistentModels, model);
  113. AddModelSelectionFunctions(menu, temporaryModels, model);
  114. }
  115. menu.ShowAsContext();
  116. }
  117. }
  118. GUI.color = color;
  119. if (EditorGUI.EndChangeCheck())
  120. _Instance._Scene.OriginalRoot = model != null ? model.transform : null;
  121. if (warning != null)
  122. EditorGUILayout.HelpBox(warning, MessageType.Warning, true);
  123. }
  124. /************************************************************************************************************************/
  125. private static void AddModelSelectionFunctions(GenericMenu menu, List<GameObject> models, GameObject selected)
  126. {
  127. for (int i = models.Count - 1; i >= 0; i--)
  128. {
  129. var model = models[i];
  130. var path = AssetDatabase.GetAssetPath(model);
  131. if (!string.IsNullOrEmpty(path))
  132. path = path.Replace('/', '\\');
  133. else
  134. path = model.name;
  135. menu.AddItem(new GUIContent(path), model == selected,
  136. () => _Instance._Scene.OriginalRoot = model.transform);
  137. }
  138. }
  139. /************************************************************************************************************************/
  140. private string GetModelWarning(GameObject model)
  141. {
  142. if (model == null)
  143. return "No Model is selected so nothing can be previewed.";
  144. if (_Instance._Scene.Animancer == null)
  145. return "The selected Model has no Animator component.";
  146. return null;
  147. }
  148. /************************************************************************************************************************/
  149. private void DoAnimatorSelectorGUI()
  150. {
  151. var instanceAnimators = _Instance._Scene.InstanceAnimators;
  152. if (instanceAnimators == null ||
  153. instanceAnimators.Length <= 1)
  154. return;
  155. var area = AnimancerGUI.LayoutSingleLineRect(AnimancerGUI.SpacingMode.After);
  156. var labelArea = AnimancerGUI.StealFromLeft(ref area, EditorGUIUtility.labelWidth, AnimancerGUI.StandardSpacing);
  157. GUI.Label(labelArea, nameof(Animator));
  158. var selectedAnimator = _Instance._Scene.SelectedInstanceAnimator;
  159. using (ObjectPool.Disposable.AcquireContent(out var label, selectedAnimator != null ? selectedAnimator.name : "None"))
  160. {
  161. var clicked = EditorGUI.DropdownButton(area, label, FocusType.Passive);
  162. if (!clicked)
  163. return;
  164. var menu = new GenericMenu();
  165. for (int i = 0; i < instanceAnimators.Length; i++)
  166. {
  167. var animator = instanceAnimators[i];
  168. var index = i;
  169. menu.AddItem(new GUIContent(animator.name), animator == selectedAnimator, () =>
  170. {
  171. _Instance._Scene.SetSelectedAnimator(index);
  172. NormalizedTime = 0;
  173. });
  174. }
  175. menu.ShowAsContext();
  176. }
  177. }
  178. /************************************************************************************************************************/
  179. public void GatherAnimations()
  180. {
  181. AnimationGatherer.GatherFromGameObject(_Instance._Scene.OriginalRoot.gameObject, ref _OtherAnimations, true);
  182. if (_OtherAnimations.Length > 0 &&
  183. (_PreviousAnimation == null || _NextAnimation == null))
  184. {
  185. var defaultClip = _OtherAnimations[0];
  186. var defaultClipIsIdle = false;
  187. for (int i = 0; i < _OtherAnimations.Length; i++)
  188. {
  189. var clip = _OtherAnimations[i];
  190. if (defaultClipIsIdle && clip.name.Length > defaultClip.name.Length)
  191. continue;
  192. if (clip.name.IndexOf("idle", StringComparison.CurrentCultureIgnoreCase) >= 0)
  193. {
  194. defaultClip = clip;
  195. break;
  196. }
  197. }
  198. if (_PreviousAnimation == null)
  199. _PreviousAnimation = defaultClip;
  200. if (_NextAnimation == null)
  201. _NextAnimation = defaultClip;
  202. }
  203. }
  204. /************************************************************************************************************************/
  205. private void DoAnimationFieldGUI(GUIContent label, ref AnimationClip clip, Action<AnimationClip> setClip)
  206. {
  207. var showDropdown = !_OtherAnimations.IsNullOrEmpty();
  208. if (DoDropdownObjectField(label, showDropdown, ref clip))
  209. {
  210. var menu = new GenericMenu();
  211. menu.AddItem(new GUIContent("None"), clip == null, () => setClip(null));
  212. for (int i = 0; i < _OtherAnimations.Length; i++)
  213. {
  214. var animation = _OtherAnimations[i];
  215. menu.AddItem(new GUIContent(animation.name), animation == clip, () => setClip(animation));
  216. }
  217. menu.ShowAsContext();
  218. }
  219. }
  220. /************************************************************************************************************************/
  221. private static bool DoDropdownObjectField<T>(GUIContent label, bool showDropdown, ref T obj,
  222. AnimancerGUI.SpacingMode spacingMode = AnimancerGUI.SpacingMode.None) where T : Object
  223. {
  224. var area = AnimancerGUI.LayoutSingleLineRect(spacingMode);
  225. var labelWidth = EditorGUIUtility.labelWidth;
  226. labelWidth += 2;
  227. area.xMin -= 1;
  228. var spacing = AnimancerGUI.StandardSpacing;
  229. var labelArea = AnimancerGUI.StealFromLeft(ref area, labelWidth - spacing, spacing);
  230. obj = (T)EditorGUI.ObjectField(area, obj, typeof(T), true);
  231. if (showDropdown)
  232. {
  233. return EditorGUI.DropdownButton(labelArea, label, FocusType.Passive);
  234. }
  235. else
  236. {
  237. GUI.Label(labelArea, label);
  238. return false;
  239. }
  240. }
  241. /************************************************************************************************************************/
  242. private void DoCurrentAnimationGUI(AnimancerPlayable animancer)
  243. {
  244. string text;
  245. if (animancer != null)
  246. {
  247. var transition = Transition;
  248. if (transition.IsValid && transition.Key != null)
  249. text = animancer.States.GetOrCreate(transition).ToString();
  250. else
  251. text = transition.ToString();
  252. }
  253. else
  254. {
  255. text = _Instance._TransitionProperty.Property.GetFriendlyPath();
  256. }
  257. if (text != null)
  258. EditorGUILayout.LabelField("Current Animation", text);
  259. }
  260. /************************************************************************************************************************/
  261. private void PlayTransition()
  262. {
  263. var transition = Transition;
  264. var animancer = _Instance._Scene.Animancer;
  265. animancer.States.TryGet(transition, out var oldState);
  266. var targetState = animancer.Play(transition);
  267. OnPlayAnimation();
  268. if (oldState != null && oldState != targetState)
  269. oldState.Destroy();
  270. var warnings = OptionalWarning.UnsupportedEvents.DisableTemporarily();
  271. targetState.Events.OnEnd = () =>
  272. {
  273. if (_NextAnimation != null)
  274. {
  275. var fadeDuration = AnimancerEvent.GetFadeOutDuration(targetState, AnimancerPlayable.DefaultFadeDuration);
  276. PlayOther(NextAnimationKey, _NextAnimation, 0, fadeDuration);
  277. OnPlayAnimation();
  278. }
  279. else
  280. {
  281. animancer.Layers[0].IncrementCommandCount();
  282. }
  283. };
  284. warnings.Enable();
  285. }
  286. /************************************************************************************************************************/
  287. public void OnPlayAnimation()
  288. {
  289. var animancer = _Instance._Scene.Animancer;
  290. if (animancer == null ||
  291. animancer.States.Current == null)
  292. return;
  293. var state = animancer.States.Current;
  294. state.RecreatePlayableRecursive();
  295. if (state.HasEvents)
  296. {
  297. var warnings = OptionalWarning.UnsupportedEvents.DisableTemporarily();
  298. var normalizedEndTime = state.Events.NormalizedEndTime;
  299. state.Events = null;
  300. state.Events.NormalizedEndTime = normalizedEndTime;
  301. warnings.Enable();
  302. }
  303. }
  304. /************************************************************************************************************************/
  305. [SerializeField]
  306. private float _NormalizedTime;
  307. public float NormalizedTime
  308. {
  309. get => _NormalizedTime;
  310. set
  311. {
  312. if (!value.IsFinite())
  313. return;
  314. _NormalizedTime = value;
  315. if (!TryShowTransitionPaused(out var animancer, out var transition, out var state))
  316. return;
  317. var length = state.Length;
  318. var speed = state.Speed;
  319. var time = value * length;
  320. var fadeDuration = transition.FadeDuration * Math.Abs(speed);
  321. var startTime = TimelineGUI.GetStartTime(transition.NormalizedStartTime, speed, length);
  322. var normalizedEndTime = state.NormalizedEndTime;
  323. var endTime = normalizedEndTime * length;
  324. var fadeOutEnd = TimelineGUI.GetFadeOutEnd(speed, endTime, length);
  325. if (speed < 0)
  326. {
  327. time = length - time;
  328. startTime = length - startTime;
  329. value = 1 - value;
  330. normalizedEndTime = 1 - normalizedEndTime;
  331. endTime = length - endTime;
  332. fadeOutEnd = length - fadeOutEnd;
  333. }
  334. if (time < startTime)// Previous animation.
  335. {
  336. if (_PreviousAnimation != null)
  337. {
  338. PlayOther(PreviousAnimationKey, _PreviousAnimation, value);
  339. value = 0;
  340. }
  341. }
  342. else if (time < startTime + fadeDuration)// Fade from previous animation to the target.
  343. {
  344. if (_PreviousAnimation != null)
  345. {
  346. var fromState = PlayOther(PreviousAnimationKey, _PreviousAnimation, value);
  347. state.IsPlaying = true;
  348. state.Weight = (time - startTime) / fadeDuration;
  349. fromState.Weight = 1 - state.Weight;
  350. }
  351. }
  352. else if (_NextAnimation != null)
  353. {
  354. if (value < normalizedEndTime)
  355. {
  356. // Just the main state.
  357. }
  358. else
  359. {
  360. var toState = PlayOther(NextAnimationKey, _NextAnimation, value - normalizedEndTime);
  361. if (time < fadeOutEnd)// Fade from the target transition to the next animation.
  362. {
  363. state.IsPlaying = true;
  364. toState.Weight = (time - endTime) / (fadeOutEnd - endTime);
  365. state.Weight = 1 - toState.Weight;
  366. }
  367. // Else just the next animation.
  368. }
  369. }
  370. if (speed < 0)
  371. value = 1 - value;
  372. state.NormalizedTime = state.Weight > 0 ? value : 0;
  373. animancer.Evaluate();
  374. AnimancerGUI.RepaintEverything();
  375. }
  376. }
  377. /************************************************************************************************************************/
  378. private bool TryShowTransitionPaused(
  379. out AnimancerPlayable animancer, out ITransitionDetailed transition, out AnimancerState state)
  380. {
  381. animancer = _Instance._Scene.Animancer;
  382. transition = Transition;
  383. if (animancer == null || !transition.IsValid())
  384. {
  385. state = null;
  386. return false;
  387. }
  388. state = animancer.Play(transition, 0);
  389. OnPlayAnimation();
  390. animancer.PauseGraph();
  391. return true;
  392. }
  393. /************************************************************************************************************************/
  394. private AnimancerState PlayOther(object key, AnimationClip animation, float normalizedTime, float fadeDuration = 0)
  395. {
  396. var animancer = _Instance._Scene.Animancer;
  397. var state = animancer.States.GetOrCreate(key, animation, true);
  398. state = animancer.Play(state, fadeDuration);
  399. OnPlayAnimation();
  400. normalizedTime *= state.Length;
  401. state.Time = normalizedTime.IsFinite() ? normalizedTime : 0;
  402. return state;
  403. }
  404. /************************************************************************************************************************/
  405. internal class WindowMatchStateTime : Key, IUpdatable
  406. {
  407. /************************************************************************************************************************/
  408. public static readonly WindowMatchStateTime Instance = new WindowMatchStateTime();
  409. /************************************************************************************************************************/
  410. void IUpdatable.Update()
  411. {
  412. if (_Instance == null ||
  413. !AnimancerPlayable.Current.IsGraphPlaying)
  414. return;
  415. var transition = Transition;
  416. if (transition == null)
  417. return;
  418. if (AnimancerPlayable.Current.States.TryGet(transition, out var state))
  419. _Instance._Animations._NormalizedTime = state.NormalizedTime;
  420. }
  421. /************************************************************************************************************************/
  422. }
  423. /************************************************************************************************************************/
  424. }
  425. }
  426. }
  427. #endif