PlayTransitionOnClick.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using UnityEngine;
  4. namespace Animancer.Examples.Basics
  5. {
  6. /// <summary>
  7. /// This script is basically the same as <see cref="PlayAnimationOnClick"/>, except that it uses
  8. /// <see href="https://kybernetik.com.au/animancer/docs/manual/transitions">Transitions</see>.
  9. /// </summary>
  10. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/basics/transitions">Transitions</see></example>
  11. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Basics/PlayTransitionOnClick
  12. ///
  13. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Basics - Play Transition On Click")]
  14. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(Basics) + "/" + nameof(PlayTransitionOnClick))]
  15. public sealed class PlayTransitionOnClick : MonoBehaviour
  16. {
  17. /************************************************************************************************************************/
  18. [SerializeField] private AnimancerComponent _Animancer;
  19. [SerializeField] private ClipTransition _Idle;
  20. [SerializeField] private ClipTransition _Action;
  21. /************************************************************************************************************************/
  22. private void OnEnable()
  23. {
  24. // Transitions store their events so we only initialize them once on startup
  25. // instead of setting the event every time the animation is played.
  26. _Action.Events.OnEnd = OnActionEnd;
  27. // The Fade Duration of this transition will be ignored because nothing else is playing yet so there is
  28. // nothing to fade from.
  29. _Animancer.Play(_Idle);
  30. }
  31. /************************************************************************************************************************/
  32. private void OnActionEnd()
  33. {
  34. _Animancer.Play(_Idle);
  35. }
  36. /************************************************************************************************************************/
  37. private void Update()
  38. {
  39. if (ExampleInput.LeftMouseUp)
  40. {
  41. _Animancer.Play(_Action);
  42. // If you want to cross fade without using Transitions or override the fade duration of a Transition
  43. // then you can simply use the second parameter in the Play method.
  44. // _Animancer.Play(_Action, 0.25f);
  45. // When cross fading, setting the state.Time like the PlayAnimationOnClick script would prevent it from
  46. // smoothly blending so if you want to restart the animation you can use FadeMode.FromStart.
  47. // _Animancer.Play(_Action, 0.25f, FadeMode.FromStart);
  48. // But if you use transitions then you don't need to specify each of those because the Fade Duration is
  49. // set in the Inspector and it automatically picks the FadeMode based on whether the Start Time check
  50. // box is enabled or not.
  51. }
  52. }
  53. /************************************************************************************************************************/
  54. }
  55. }