PlayTransitionOnClick.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using UnityEngine;
  4. namespace Animancer.Samples.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. ///
  11. /// <remarks>
  12. /// <strong>Sample:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/samples/basics/transitions">
  14. /// Transitions</see>
  15. /// </remarks>
  16. ///
  17. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Basics/PlayTransitionOnClick
  18. ///
  19. [AddComponentMenu(Strings.SamplesMenuPrefix + "Basics - Play Transition On Click")]
  20. [AnimancerHelpUrl(typeof(PlayTransitionOnClick))]
  21. public class PlayTransitionOnClick : MonoBehaviour
  22. {
  23. /************************************************************************************************************************/
  24. [SerializeField] private AnimancerComponent _Animancer;
  25. [SerializeField] private ClipTransition _Idle;
  26. [SerializeField] private ClipTransition _Action;
  27. /************************************************************************************************************************/
  28. protected virtual void Awake()
  29. {
  30. // Transitions store their events so we only initialize them once on startup
  31. // instead of setting the event every time the animation is played.
  32. _Action.Events.OnEnd = OnEnable;
  33. }
  34. /************************************************************************************************************************/
  35. protected virtual void OnEnable()
  36. {
  37. // The Fade Duration of this transition will be ignored
  38. // the first time because nothing else is playing yet.
  39. _Animancer.Play(_Idle);
  40. }
  41. /************************************************************************************************************************/
  42. protected virtual void Update()
  43. {
  44. if (SampleInput.LeftMouseUp)
  45. {
  46. _Animancer.Play(_Action);
  47. // If you want to cross fade without using Transitions
  48. // or override the fade duration of a Transition
  49. // then you can simply use the second parameter in the Play method.
  50. // _Animancer.Play(_Action, 0.25f);
  51. // When cross fading, setting the state.Time like the PlayAnimationOnClick script
  52. // would prevent it from smoothly blending so if you want to restart the animation
  53. // you should use FadeMode.FromStart.
  54. // _Animancer.Play(_Action, 0.25f, FadeMode.FromStart);
  55. // But if you use transitions then you don't need to specify each of those
  56. // because the Fade Duration is set in the Inspector and it automatically
  57. // picks the FadeMode based on whether the Start Time check box is enabled or not.
  58. }
  59. }
  60. /************************************************************************************************************************/
  61. }
  62. }