PanelAnimationController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace Google.Maps.Examples.Shared {
  4. /// <summary>
  5. /// This class controls the close and open panel animations.
  6. /// There are two animations provided based on the device orientation.
  7. /// In Portrait mode, we open the panel until the opposite edge,
  8. /// while in Landscape mode, we limit the opening to half of the screen.
  9. /// The reason is purely esthetic. In landscape mode we limit the amount of empty white space
  10. /// on the panel.
  11. /// Both animations are setup on the same controller. We just use different trigger keys.
  12. /// </summary>
  13. public class PanelAnimationController : MonoBehaviour {
  14. /// <summary>
  15. /// Reference to the close panel button
  16. /// </summary>
  17. public Button ClosePanelButton;
  18. /// <summary>
  19. /// Reference to the open panel button
  20. /// </summary>
  21. public Button OpenPanelButton;
  22. /// <summary>
  23. /// Reference to the animator component that drives the animation state machines for both
  24. /// vertical and horizontal screen orientations.
  25. /// </summary>
  26. public Animator Animator;
  27. /// <summary>
  28. /// Trigger key used in specific state machines configured in animator.
  29. /// </summary>
  30. private string TriggerKey = "closeV";
  31. /// <summary>
  32. /// Used to detect changes in screen orientation.
  33. /// When this happens, we close the panel to prevent confusion with the two state machines
  34. /// implemented in the associated animator. Closing the panel resets the current state machine
  35. /// to a common idle state.
  36. /// </summary>
  37. private ScreenOrientation CurrentScreenOrientation;
  38. /// <summary>
  39. /// On start, check the availability of the animator, open and close buttons.
  40. /// </summary>
  41. void Start() {
  42. Debug.Assert(Animator, "Missing close panel animation!");
  43. Debug.Assert(ClosePanelButton, "Missing close panel button!");
  44. Debug.Assert(OpenPanelButton, "Missing open panel button!");
  45. CurrentScreenOrientation = Screen.orientation;
  46. }
  47. /// <summary>
  48. /// On updates, apply the correct trigger key based on the screen orientation.
  49. /// </summary>
  50. void Update() {
  51. if (CurrentScreenOrientation != Screen.orientation) {
  52. OnPanelClose();
  53. CurrentScreenOrientation = Screen.orientation;
  54. } else {
  55. if (Screen.orientation == ScreenOrientation.Portrait) {
  56. TriggerKey = "closeV";
  57. } else {
  58. TriggerKey = "closeH";
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// Trigger the animation and update the open/close buttons.
  64. /// </summary>
  65. public void OnPanelOpen() {
  66. // Slide panel left
  67. Animator.SetBool(TriggerKey, false);
  68. ShowOpenPanelButton(false);
  69. }
  70. /// <summary>
  71. /// Trigger the animation and update the open/close buttons.
  72. /// </summary>
  73. public void OnPanelClose() {
  74. // Slide panel right
  75. Animator.SetBool(TriggerKey, true);
  76. ShowOpenPanelButton(true);
  77. }
  78. /// <summary>
  79. /// Hides/shows either close or open buttons.
  80. /// </summary>
  81. /// <param name="isOpened">Indicates if the panel should be opened or close.</param>
  82. public void ShowOpenPanelButton(bool isOpened) {
  83. ClosePanelButton.gameObject.SetActive(!isOpened);
  84. OpenPanelButton.gameObject.SetActive(isOpened);
  85. }
  86. }
  87. }