using UnityEngine; using UnityEngine.UI; namespace Google.Maps.Examples.Shared { /// /// This class controls the close and open panel animations. /// There are two animations provided based on the device orientation. /// In Portrait mode, we open the panel until the opposite edge, /// while in Landscape mode, we limit the opening to half of the screen. /// The reason is purely esthetic. In landscape mode we limit the amount of empty white space /// on the panel. /// Both animations are setup on the same controller. We just use different trigger keys. /// public class PanelAnimationController : MonoBehaviour { /// /// Reference to the close panel button /// public Button ClosePanelButton; /// /// Reference to the open panel button /// public Button OpenPanelButton; /// /// Reference to the animator component that drives the animation state machines for both /// vertical and horizontal screen orientations. /// public Animator Animator; /// /// Trigger key used in specific state machines configured in animator. /// private string TriggerKey = "closeV"; /// /// Used to detect changes in screen orientation. /// When this happens, we close the panel to prevent confusion with the two state machines /// implemented in the associated animator. Closing the panel resets the current state machine /// to a common idle state. /// private ScreenOrientation CurrentScreenOrientation; /// /// On start, check the availability of the animator, open and close buttons. /// void Start() { Debug.Assert(Animator, "Missing close panel animation!"); Debug.Assert(ClosePanelButton, "Missing close panel button!"); Debug.Assert(OpenPanelButton, "Missing open panel button!"); CurrentScreenOrientation = Screen.orientation; } /// /// On updates, apply the correct trigger key based on the screen orientation. /// void Update() { if (CurrentScreenOrientation != Screen.orientation) { OnPanelClose(); CurrentScreenOrientation = Screen.orientation; } else { if (Screen.orientation == ScreenOrientation.Portrait) { TriggerKey = "closeV"; } else { TriggerKey = "closeH"; } } } /// /// Trigger the animation and update the open/close buttons. /// public void OnPanelOpen() { // Slide panel left Animator.SetBool(TriggerKey, false); ShowOpenPanelButton(false); } /// /// Trigger the animation and update the open/close buttons. /// public void OnPanelClose() { // Slide panel right Animator.SetBool(TriggerKey, true); ShowOpenPanelButton(true); } /// /// Hides/shows either close or open buttons. /// /// Indicates if the panel should be opened or close. public void ShowOpenPanelButton(bool isOpened) { ClosePanelButton.gameObject.SetActive(!isOpened); OpenPanelButton.gameObject.SetActive(isOpened); } } }