Door.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.FineControl
  5. {
  6. /// <summary>
  7. /// An <see cref="IInteractable"/> door which toggles between open and closed when something interacts with it.
  8. /// </summary>
  9. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/fine-control/doors">Doors</see></example>
  10. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.FineControl/Door
  11. ///
  12. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Fine Control - Door")]
  13. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(FineControl) + "/" + nameof(Door))]
  14. [SelectionBase]
  15. public sealed class Door : MonoBehaviour, IInteractable
  16. {
  17. /************************************************************************************************************************/
  18. [SerializeField] private AnimancerComponent _Animancer;
  19. [SerializeField] private AnimationClip _Open;
  20. [SerializeField, Range(0, 1)]
  21. private float _Openness;
  22. /************************************************************************************************************************/
  23. private void Awake()
  24. {
  25. // Apply the starting state and pause the graph.
  26. var state = _Animancer.Play(_Open);
  27. state.NormalizedTime = _Openness;
  28. _Animancer.Evaluate();
  29. _Animancer.Playable.PauseGraph();
  30. // And also pause it whenever the animation finishes to save performance.
  31. state.Events.OnEnd = _Animancer.Playable.PauseGraph;
  32. // Normally the OnEnd event would be cleared whenever we play a new animation, but since there is only one
  33. // animation in this example we just leave it playing and pause/unpause the graph instead.
  34. }
  35. /************************************************************************************************************************/
  36. /// <summary>[<see cref="IInteractable"/>] Toggles this door between open and closed.</summary>
  37. public void Interact()
  38. {
  39. // Get the state to set its speed (or we could have just kept the state from Awake).
  40. var state = _Animancer.States[_Open];
  41. // If currently near closed, play the animation forwards.
  42. if (_Openness < 0.5f)
  43. {
  44. state.Speed = 1;
  45. _Openness = 1;
  46. }
  47. else// Otherwise play it backwards.
  48. {
  49. state.Speed = -1;
  50. _Openness = 0;
  51. }
  52. // And make sure the graph is playing.
  53. _Animancer.Playable.UnpauseGraph();
  54. }
  55. /************************************************************************************************************************/
  56. #if UNITY_EDITOR
  57. /// <summary>[Editor-Only] Applies the starting openness value to the door in Edit Mode.</summary>
  58. /// <remarks>Called in Edit Mode whenever this script is loaded or a value is changed in the Inspector.</remarks>
  59. private void OnValidate()
  60. {
  61. AnimancerUtilities.EditModeSampleAnimation(_Open, this, _Openness * _Open.length);
  62. }
  63. #endif
  64. /************************************************************************************************************************/
  65. }
  66. }