LayeredAnimationManager.cs 3.4 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 Animancer.Units;
  4. using UnityEngine;
  5. namespace Animancer.Examples.Layers
  6. {
  7. /// <summary>Demonstrates how to use layers to play multiple animations at once on different body parts.</summary>
  8. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/layers">Layers</see></example>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Layers/LayeredAnimationManager
  10. ///
  11. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Layers - Layered Animation Manager")]
  12. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(Layers) + "/" + nameof(LayeredAnimationManager))]
  13. public sealed class LayeredAnimationManager : MonoBehaviour
  14. {
  15. /************************************************************************************************************************/
  16. [SerializeField] private AnimancerComponent _Animancer;
  17. [SerializeField] private AvatarMask _ActionMask;
  18. [SerializeField, Seconds] private float _ActionFadeDuration = AnimancerPlayable.DefaultFadeDuration;
  19. private AnimancerLayer _BaseLayer;
  20. private AnimancerLayer _ActionLayer;
  21. private bool _CanPlayActionFullBody;
  22. /************************************************************************************************************************/
  23. private void Awake()
  24. {
  25. _BaseLayer = _Animancer.Layers[0];
  26. _ActionLayer = _Animancer.Layers[1];
  27. _ActionLayer.SetMask(_ActionMask);
  28. }
  29. /************************************************************************************************************************/
  30. public void PlayBase(ITransition transition, bool canPlayActionFullBody)
  31. {
  32. _CanPlayActionFullBody = canPlayActionFullBody;
  33. if (_CanPlayActionFullBody && _ActionLayer.TargetWeight > 0)
  34. {
  35. PlayActionFullBody(_ActionFadeDuration);
  36. }
  37. else
  38. {
  39. _BaseLayer.Play(transition);
  40. }
  41. }
  42. /************************************************************************************************************************/
  43. public void PlayAction(ITransition transition)
  44. {
  45. _ActionLayer.Play(transition);
  46. if (_CanPlayActionFullBody)
  47. PlayActionFullBody(transition.FadeDuration);
  48. }
  49. /************************************************************************************************************************/
  50. private void PlayActionFullBody(float fadeDuration)
  51. {
  52. var upperBodyState = _ActionLayer.CurrentState;
  53. var fullBodyClone = _BaseLayer.GetOrCreateState(upperBodyState, upperBodyState.Clip);
  54. _BaseLayer.Play(fullBodyClone, fadeDuration);
  55. fullBodyClone.NormalizedTime = upperBodyState.NormalizedTime;
  56. }
  57. /************************************************************************************************************************/
  58. public void FadeOutUpperBody()
  59. {
  60. _ActionLayer.StartFade(0, _ActionFadeDuration);
  61. }
  62. /************************************************************************************************************************/
  63. }
  64. }