LayeredAnimationManager.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 Animancer.Units;
  4. using UnityEngine;
  5. namespace Animancer.Samples.Layers
  6. {
  7. /// <summary>
  8. /// Demonstrates how to use layers to play multiple
  9. /// independent animations at the same time on different body parts.
  10. /// </summary>
  11. ///
  12. /// <remarks>
  13. /// <strong>Sample:</strong>
  14. /// <see href="https://kybernetik.com.au/animancer/docs/samples/layers/dynamic">
  15. /// Dynamic Layers</see>
  16. /// </remarks>
  17. ///
  18. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Layers/LayeredAnimationManager
  19. ///
  20. [AddComponentMenu(Strings.SamplesMenuPrefix + "Layers - Layered Animation Manager")]
  21. [AnimancerHelpUrl(typeof(LayeredAnimationManager))]
  22. public class LayeredAnimationManager : MonoBehaviour
  23. {
  24. /************************************************************************************************************************/
  25. [SerializeField] private AnimancerComponent _Animancer;
  26. [SerializeField] private AvatarMask _ActionMask;
  27. [SerializeField, Seconds] private float _ActionFadeDuration = AnimancerGraph.DefaultFadeDuration;
  28. private AnimancerLayer _BaseLayer;
  29. private AnimancerLayer _ActionLayer;
  30. private bool _CanPlayActionFullBody;
  31. /************************************************************************************************************************/
  32. protected virtual void Awake()
  33. {
  34. _BaseLayer = _Animancer.Layers[0];
  35. _ActionLayer = _Animancer.Layers[1];
  36. _ActionLayer.Mask = _ActionMask;
  37. _ActionLayer.SetDebugName("Action Layer");
  38. }
  39. /************************************************************************************************************************/
  40. public void PlayBase(ITransition transition, bool canPlayActionFullBody)
  41. {
  42. _CanPlayActionFullBody = canPlayActionFullBody;
  43. if (_CanPlayActionFullBody && _ActionLayer.TargetWeight > 0)
  44. {
  45. PlayActionFullBody(_ActionFadeDuration);
  46. }
  47. else
  48. {
  49. _BaseLayer.Play(transition);
  50. }
  51. }
  52. /************************************************************************************************************************/
  53. public void PlayAction(ITransition transition)
  54. {
  55. _ActionLayer.Play(transition);
  56. if (_CanPlayActionFullBody)
  57. PlayActionFullBody(transition.FadeDuration);
  58. }
  59. /************************************************************************************************************************/
  60. private void PlayActionFullBody(float fadeDuration)
  61. {
  62. AnimancerState actionState = _ActionLayer.CurrentState;
  63. AnimancerState baseState = _BaseLayer.Play(actionState, fadeDuration);
  64. baseState.Time = actionState.Time;
  65. }
  66. /************************************************************************************************************************/
  67. public void FadeOutAction()
  68. {
  69. _ActionLayer.StartFade(0, _ActionFadeDuration);
  70. }
  71. /************************************************************************************************************************/
  72. }
  73. }