πŸͺŸUI Popup

UICanvasContoller inheritance for UI popups

What for

Sometimes you need popup windows, that appear for example to offer player reward or teach him controls and etc. UI Popup has basic functionality for this, it has buttons with responce types and events to handle it.

How to use

UIPopup.cs has PopupButton[] array, where you can add buttons and it's responce type in the inspector (by default here is Closed, Accepted, Denied responce types in PopupResults enum).

/// <summary>
/// User action callback, new callback will override previous one<br>
/// You can also use <see cref="OnPopupClose"/> on referenced UIPopup object that will fire events every time popup closes</br>
/// </summary>
public void SetResultCallback(UnityAction<PopupResults> callback);

By default on button click of types Closed, Accepted, Denied popup will be hiden. You can inherit UIPopup and override OnPlayerDenyClicked and OnPlayerAcceptClicked completely to avoid self close or add your custom logic.

Also UIPopup has "Show On Top Other UI Elements" field in the inspector, which works in canvas scope where popup located. It simply set last sibling index of transform.

If you use popup in separate Canvas outside GameCanvas you need to inscrease "Sort order" of canvas to be rendered on top of other. You don't need to use "Show On Top Other UI Elements" in UIPopup in that case.

Workflow

You can subscribe to OnPopupClose event on Awake if you have reference to popup window and take popup window Result property in callback function to handle player choice.

Or you can call SetResultCallback method on popup object before Show/EnableCanvas. This callback has Result argument in it and will remain till you call again SetResultCallback.

Example of reward offer popup

[SerializeField] private UIPopup _boostPopup;

private void Awake() {
    _boostPopup.OnPopupClose.AddListener(OnPopupClosed);
    // or if you need only accept result
    //_boostPopup.SetResultCallback((result) => ShowAdForBoost());

    // you can also get canvas from UI manager if it's already exist in scene
    var boostPopup = UIManager.instance.GetCanvas<UIPopup>("BoostPopup");
    Debug.Log("Hey! I found boost popup, click on me to show it in inspector", boostPopup.gameObject);
}

public void ShowBoostPopup() {
    _boostPopup.Show(); // inspector fade values for enable will be taken
    // or
    //_boostPopup.EnableCanvas(true, 1f);
}

public void HideBoostPopup() {
    _boostPopup.Hide(); // inspector fade values for disable will be taken
    // or
    //_boostPopup.EnableCanvas(false, 1f);
}

private void OnPopupClosed() {
    if (_boostPopup.Result == PopupResults.Accepted) {
        ShowAdForBoost();
    }

    // you can send analytics what result player choose here

    Debug.Log($"Player popup result: {_boostPopup.Result}");
}

private void ShowAdForBoost() {
    // call ad show
    // on ad watch complete you can activate boost and ets
    Debug.Log("Boost activate ad call!");
}

You can find UIPopup.cs example in: Assets\_Examples\UI System\UIPopup

Last updated