HybridBasics.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.AnimatorControllers
  5. {
  6. /// <summary>Demonstrates how to play Animator Controllers alongside Animancer.</summary>
  7. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/animator-controllers">Animator Controllers</see></example>
  8. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.AnimatorControllers/HybridBasics
  9. ///
  10. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Animator Controllers - Hybrid Basics")]
  11. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(AnimatorControllers) + "/" + nameof(HybridBasics))]
  12. public sealed class HybridBasics : MonoBehaviour
  13. {
  14. /************************************************************************************************************************/
  15. [SerializeField] private AnimancerComponent _Animancer;
  16. [SerializeField] private AnimationClip _SeparateAnimation;
  17. /************************************************************************************************************************/
  18. private void Awake()
  19. {
  20. // This example's documentation explains why this warning exists so we don't need it enabled.
  21. OptionalWarning.NativeControllerHumanoid.Disable();
  22. }
  23. /************************************************************************************************************************/
  24. private static readonly int MoveParameterID = Animator.StringToHash("Move");
  25. // Called by a UI Toggle.
  26. public void SetMove(bool move)
  27. {
  28. // Call SetBool on the HybridAnimancerComponent:
  29. if (_Animancer is HybridAnimancerComponent hybrid)
  30. hybrid.SetBool(MoveParameterID, move);
  31. else// Or on the Animator:
  32. _Animancer.Animator.SetBool(MoveParameterID, move);
  33. }
  34. /************************************************************************************************************************/
  35. // Called by a UI Button.
  36. public void PlaySeparateAnimation()
  37. {
  38. _Animancer.Play(_SeparateAnimation);
  39. }
  40. /************************************************************************************************************************/
  41. // Called by a UI Button.
  42. public void PlayAnimatorController()
  43. {
  44. // Play the Animator Controller on the HybridAnimancerComponent:
  45. if (_Animancer is HybridAnimancerComponent hybrid)
  46. hybrid.Play(hybrid.Controller, 0);
  47. else// Or Stop the AnimancerComponent to let the native Animator Controller resume control:
  48. _Animancer.Stop();
  49. }
  50. /************************************************************************************************************************/
  51. // Called by a UI Button.
  52. public void FadeSeparateAnimation()
  53. {
  54. _Animancer.Play(_SeparateAnimation, 0.25f);
  55. }
  56. /************************************************************************************************************************/
  57. // Called by a UI Button.
  58. public void FadeAnimatorController()
  59. {
  60. // Play the Animator Controller on the HybridAnimancerComponent:
  61. if (_Animancer is HybridAnimancerComponent hybrid)
  62. hybrid.PlayController();
  63. else// Or fade out the Animancer Layer to let the native Animator Controller resume control:
  64. _Animancer.Layers[0].StartFade(0, 0.25f);
  65. }
  66. /************************************************************************************************************************/
  67. }
  68. }