DirectionalBasics.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.DirectionalSprites
  5. {
  6. /// <summary>
  7. /// Animates a character to either stand idle or walk using animations defined in
  8. /// <see cref="DirectionalAnimationSet"/>s.
  9. /// </summary>
  10. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/directional-sprites/basics">Directional Basics</see></example>
  11. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.DirectionalSprites/DirectionalBasics
  12. ///
  13. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Directional Sprites - Directional Basics")]
  14. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(DirectionalSprites) + "/" + nameof(DirectionalBasics))]
  15. public sealed class DirectionalBasics : MonoBehaviour
  16. {
  17. /************************************************************************************************************************/
  18. [SerializeField] private AnimancerComponent _Animancer;
  19. [SerializeField] private DirectionalAnimationSet _Idles;
  20. [SerializeField] private DirectionalAnimationSet _Walks;
  21. [SerializeField] private Vector2 _Facing = Vector2.down;
  22. /************************************************************************************************************************/
  23. private void Update()
  24. {
  25. var input = ExampleInput.WASD;
  26. if (input != default)
  27. {
  28. _Facing = input;
  29. Play(_Walks);
  30. // Play could return the AnimancerState it gets from _Animancer.Play,
  31. // But we can also just access it using _Animancer.States.Current.
  32. var isRunning = ExampleInput.LeftShiftHold;
  33. _Animancer.States.Current.Speed = isRunning ? 2 : 1;
  34. }
  35. else
  36. {
  37. // When we are not moving, we still remember the direction we are facing
  38. // so we can continue using the correct idle animation for that direction.
  39. Play(_Idles);
  40. }
  41. }
  42. /************************************************************************************************************************/
  43. private void Play(DirectionalAnimationSet animations)
  44. {
  45. // Instead of only a single animation, we have a different one for each direction we can face.
  46. // So we get whichever is appropriate for that direction and play it.
  47. var clip = animations.GetClip(_Facing);
  48. _Animancer.Play(clip);
  49. // Or we could do that in one line:
  50. // _Animancer.Play(animations.GetClip(_Facing));
  51. }
  52. /************************************************************************************************************************/
  53. #if UNITY_EDITOR
  54. /************************************************************************************************************************/
  55. /// <summary>[Editor-Only]
  56. /// Sets the character's starting sprite in Edit Mode so you can see it while working in the scene.
  57. /// </summary>
  58. private void OnValidate()
  59. {
  60. if (_Idles != null)
  61. _Idles.GetClip(_Facing).EditModeSampleAnimation(_Animancer);
  62. }
  63. /************************************************************************************************************************/
  64. #endif
  65. /************************************************************************************************************************/
  66. }
  67. }