<aside> <img src="/icons/reorder_gray.svg" alt="/icons/reorder_gray.svg" width="40px" />
Navigation
</aside>
<aside>
💡 A page is a canvas with a UIPage subclass. Add it to the UI Main Canvas of the Game scene and UIController finds it.
</aside>
Create the script. Inherit from UIPage and implement Init, OnShow and OnHide. Call NotifyOpened() and NotifyClosed() when the animations end.
using UnityEngine;
using UnityEngine.UI;
namespace Watermelon
{
public class UIShop : UIPage
{
private const float FadeDuration = 0.25f;
[SerializeField] RectTransform safeAreaRectTransform;
[SerializeField] Button closeButton;
private UIFadeAnimation fadeAnimation;
public static void Show()
{
UIController.ShowPage<UIShop>();
}
public override void Init()
{
fadeAnimation = new UIFadeAnimation(gameObject);
SafeAreaAdapter.RegisterRectTransform(safeAreaRectTransform);
closeButton.onClick.AddListener(OnCloseButtonClicked);
}
protected override void OnShow()
{
fadeAnimation.Show(FadeDuration, onCompleted: NotifyOpened);
}
protected override void OnHide()
{
fadeAnimation.Hide(FadeDuration, onCompleted: NotifyClosed);
}
private void OnCloseButtonClicked()
{
AudioController.PlaySound(AudioController.GetClip("button_sound"));
UIController.HidePage<UIShop>();
}
}
}
Create the prefab. Create a canvas object. The UIPage adds a Canvas and a GraphicRaycaster. Add a child object with a Safe Area rect transform and build the layout inside it.
Add it to the scene. Place the prefab as a child of the UI Main Canvas in the Game scene. UIController discovers all UIPage components under its object.
Save the scene. The page stores the list of its IUIPageElement components when the scene is saved.
Open the page. Call UIController.ShowPage<UIShop>().
Override IsPopup to return true. The controller tracks open popups and UIController.IsPopupOpened reports them. To pause the game while the popup is open, also override IsPausePopup, and use unscaled time in the animations.
public override bool IsPopup => true;
public override bool IsPausePopup => true;
A component that must follow the page lifecycle implements IUIPageElement. The page finds it with GetComponentsInChildren when the scene is saved. The upgrade slots and the active booster HUD are page elements.
using UnityEngine;
namespace Watermelon
{
public class ShopItem : MonoBehaviour, IUIPageElement
{
public void Init(UIPage page)
{
// Cache references and subscribe to events
}
public void OnPageStateChanged(bool state)
{
// The page canvas was enabled or disabled
}
}
}
UIController has a Cached Pages list. A page prefab in that list is instantiated on demand when the page is shown for the first time, so the page does not have to exist in the scene at start. An entry can also destroy the page when it closes (Auto Destroy).
UIController.HidePage<T>(). Do not disable the canvas yourself.NotifyOpened() and NotifyClosed(). The controller waits for them.AudioController.PlaySound(AudioController.GetClip("button_sound")).Checkpoint.Log("Page opened", gameObject) to find the page in the logs.