PlayAnimationOnClick.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /// Starts with an idle animation and performs an action when the user clicks the mouse, then returns to idle.
  8. /// </summary>
  9. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/basics/action">Basic Action</see></example>
  10. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Basics/PlayAnimationOnClick
  11. ///
  12. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Basics - Play Animation On Click")]
  13. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(Basics) + "/" + nameof(PlayAnimationOnClick))]
  14. public sealed class PlayAnimationOnClick : MonoBehaviour
  15. {
  16. /************************************************************************************************************************/
  17. [SerializeField] private AnimancerComponent _Animancer;
  18. [SerializeField] private AnimationClip _Idle;
  19. [SerializeField] private AnimationClip _Action;
  20. /************************************************************************************************************************/
  21. private void OnEnable()
  22. {
  23. _Animancer.Play(_Idle);
  24. }
  25. /************************************************************************************************************************/
  26. private void Update()
  27. {
  28. if (ExampleInput.LeftMouseUp)
  29. {
  30. // Play the action animation and grab the internal state which controls it.
  31. AnimancerState state = _Animancer.Play(_Action);
  32. // Go back to the beginning of the animation.
  33. // Otherwise if the animation was already playing, it would continue from there.
  34. state.Time = 0;
  35. // When the animation reaches its end, call the OnEnable method to go back to idle.
  36. // The Events examples explain this feature in more detail.
  37. state.Events.OnEnd = OnEnable;
  38. }
  39. }
  40. /************************************************************************************************************************/
  41. }
  42. }