NamedCharacterAnimations.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.FineControl
  5. {
  6. /// <summary>
  7. /// Implements the same behaviour as <see cref="Basics.LibraryCharacterAnimations"/>
  8. /// but refers to the animations by name instead of using direct references.
  9. /// </summary>
  10. ///
  11. /// <remarks>
  12. /// <strong>Sample:</strong>
  13. /// <see href="https://kybernetik.com.au/animancer/docs/samples/fine-control/named">
  14. /// Named Character</see>
  15. /// </remarks>
  16. ///
  17. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.FineControl/NamedCharacterAnimations
  18. ///
  19. [AddComponentMenu(Strings.SamplesMenuPrefix + "Fine Control - Named Character Animations")]
  20. [AnimancerHelpUrl(typeof(NamedCharacterAnimations))]
  21. public class NamedCharacterAnimations : MonoBehaviour
  22. {
  23. /************************************************************************************************************************/
  24. // This script is almost identical to LibraryCharacterAnimations, with a few differences:
  25. // - It uses StringAsset instead of TransitionAssets.
  26. // - It calls TryPlay instead of Play.
  27. /************************************************************************************************************************/
  28. [SerializeField] private AnimancerComponent _Animancer;
  29. [SerializeField] private StringAsset _Idle;
  30. [SerializeField] private StringAsset _Move;
  31. [SerializeField] private StringAsset _Action;
  32. private State _CurrentState;
  33. private enum State
  34. {
  35. NotActing,// Idle and Move can be interrupted.
  36. Acting,// Action can only be interrupted by itself.
  37. }
  38. /************************************************************************************************************************/
  39. protected virtual void Update()
  40. {
  41. switch (_CurrentState)
  42. {
  43. case State.NotActing:
  44. UpdateMovement();
  45. UpdateAction();
  46. break;
  47. case State.Acting:
  48. UpdateAction();
  49. break;
  50. }
  51. }
  52. /************************************************************************************************************************/
  53. private void UpdateMovement()
  54. {
  55. _CurrentState = State.NotActing;
  56. float forward = SampleInput.WASD.y;
  57. if (forward > 0)
  58. {
  59. _Animancer.TryPlay(_Move);
  60. }
  61. else
  62. {
  63. _Animancer.TryPlay(_Idle);
  64. }
  65. }
  66. /************************************************************************************************************************/
  67. private void UpdateAction()
  68. {
  69. if (SampleInput.LeftMouseUp)
  70. {
  71. _CurrentState = State.Acting;
  72. AnimancerState state = _Animancer.TryPlay(_Action);
  73. state.Events(this).OnEnd ??= UpdateMovement;
  74. }
  75. }
  76. /************************************************************************************************************************/
  77. }
  78. }