Making a generic modal window (Pt 2)

確認済のバージョン: 4.6

-

難易度: 中級

In this session we will be making a generic modal window (Yes, No, Maybeso, Cancel) where we can push content and actions to the window from anywhere in our game and have the events work when the buttons are pressed. (Part 2 of 3) Tutor - Adam Buckner

Making a generic modal window (Pt 2)

中級 User Interface (UI)

BringToFront

Code snippet

using UnityEngine;
using System.Collections;

public class BringToFront : MonoBehaviour {

    void OnEnable () {
        transform.SetAsLastSibling ();
    }
}

DeactivateMe

Code snippet

using UnityEngine;
using System.Collections;

public class DeactivateMe : MonoBehaviour {

    void Awake () {
        gameObject.SetActive(false);
    }
}

ModalPanel

Code snippet

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;


public class ModalPanel : MonoBehaviour {
    
    public Text question;
    public Image iconImage;
    public Button yesButton;
    public Button noButton;
    public Button cancelButton;
    public GameObject modalPanelObject;
    
    private static ModalPanel modalPanel;
    
    public static ModalPanel Instance () {
        if (!modalPanel) {
            modalPanel = FindObjectOfType(typeof (ModalPanel)) as ModalPanel;
            if (!modalPanel)
                Debug.LogError ("There needs to be one active ModalPanel script on a GameObject in your scene.");
        }
        
        return modalPanel;
    }
    
    //  Announcement: A string and Cancel event;
    public void Choice (string question, UnityAction cancelEvent) {
        modalPanelObject.SetActive (true);

        cancelButton.onClick.RemoveAllListeners();
        cancelButton.onClick.AddListener (cancelEvent);
        cancelButton.onClick.AddListener (ClosePanel);
        
        this.question.text = question;
        
        iconImage.gameObject.SetActive(false);
        yesButton.gameObject.SetActive(false);
        noButton.gameObject.SetActive(false);
        cancelButton.gameObject.SetActive(true);
    }
    
    //  Announcement with Image:  A string, a Sprite and Cancel event;
    public void Choice (string question, Sprite iconImage, UnityAction cancelEvent) {
        modalPanelObject.SetActive (true);
        
        cancelButton.onClick.RemoveAllListeners();
        cancelButton.onClick.AddListener (cancelEvent);
        cancelButton.onClick.AddListener (ClosePanel);
        
        this.question.text = question;
        this.iconImage.sprite = iconImage;
        
        this.iconImage.gameObject.SetActive(true);
        yesButton.gameObject.SetActive(false);
        noButton.gameObject.SetActive(false);
        cancelButton.gameObject.SetActive(true);
    }
    
    //  Yes/No: A string, a Yes event, a No event (No Cancel Button);
    public void Choice (string question, UnityAction yesEvent, UnityAction noEvent) {
        modalPanelObject.SetActive (true);

        yesButton.onClick.RemoveAllListeners();
        yesButton.onClick.AddListener (yesEvent);
        yesButton.onClick.AddListener (ClosePanel);

        noButton.onClick.RemoveAllListeners();
        noButton.onClick.AddListener (noEvent);
        noButton.onClick.AddListener (ClosePanel);

        this.question.text = question;
        
        this.iconImage.gameObject.SetActive(false);
        yesButton.gameObject.SetActive(true);
        noButton.gameObject.SetActive(true);
        cancelButton.gameObject.SetActive(false);
    }
    
    //  Yes/No/Cancel: A string, a Yes event, a No event and Cancel event;
    public void Choice (string question, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent) {
        modalPanelObject.SetActive (true);
        
        yesButton.onClick.RemoveAllListeners();
        yesButton.onClick.AddListener (yesEvent);
        yesButton.onClick.AddListener (ClosePanel);
        
        noButton.onClick.RemoveAllListeners();
        noButton.onClick.AddListener (noEvent);
        noButton.onClick.AddListener (ClosePanel);
        
        cancelButton.onClick.RemoveAllListeners();
        cancelButton.onClick.AddListener (cancelEvent);
        cancelButton.onClick.AddListener (ClosePanel);
        
        this.question.text = question;
        
        this.iconImage.gameObject.SetActive(false);
        yesButton.gameObject.SetActive(true);
        noButton.gameObject.SetActive(true);
        cancelButton.gameObject.SetActive(true);
    }
    
    //  Yes/No with Image: A string, a Sprite, a Yes event, a No event (No Cancel Button);
    public void Choice (string question, Sprite iconImage, UnityAction yesEvent, UnityAction noEvent) {
        modalPanelObject.SetActive (true);
        
        yesButton.onClick.RemoveAllListeners();
        yesButton.onClick.AddListener (yesEvent);
        yesButton.onClick.AddListener (ClosePanel);
        
        noButton.onClick.RemoveAllListeners();
        noButton.onClick.AddListener (noEvent);
        noButton.onClick.AddListener (ClosePanel);
        
        this.question.text = question;
        this.iconImage.sprite = iconImage;
        
        this.iconImage.gameObject.SetActive(true);
        yesButton.gameObject.SetActive(true);
        noButton.gameObject.SetActive(true);
        cancelButton.gameObject.SetActive(false);
    }
    
    //  Yes/No/Cancel with Image: A string, a Sprite, a Yes event, a No event and Cancel event;
    public void Choice (string question, Sprite iconImage, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent) {
        modalPanelObject.SetActive (true);
        
        yesButton.onClick.RemoveAllListeners();
        yesButton.onClick.AddListener (yesEvent);
        yesButton.onClick.AddListener (ClosePanel);
        
        noButton.onClick.RemoveAllListeners();
        noButton.onClick.AddListener (noEvent);
        noButton.onClick.AddListener (ClosePanel);
        
        cancelButton.onClick.RemoveAllListeners();
        cancelButton.onClick.AddListener (cancelEvent);
        cancelButton.onClick.AddListener (ClosePanel);
        
        this.question.text = question;
        this.iconImage.sprite = iconImage;
        
        this.iconImage.gameObject.SetActive(true);
        yesButton.gameObject.SetActive(true);
        noButton.gameObject.SetActive(true);
        cancelButton.gameObject.SetActive(true);
    }
    
    void ClosePanel () {
        modalPanelObject.SetActive (false); 
    }
}

TestModalWindow

Code snippet

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;

public class TestModalPanel : MonoBehaviour {

    public Sprite icon;
    public Transform spawnPoint;
    public GameObject thingToSpawn;
    public DisplayManager displayManager;
    
    private ModalPanel modalPanel;
    // private UnityAction myYesAction;
    // private UnityAction myNoAction;
    // private UnityAction myCancelAction;

    void Awake () {
        modalPanel = ModalPanel.Instance();
        //  myYesAction = new UnityAction (TestYesFunction);
        //  myNoAction = new UnityAction (TestNoFunction);
        //  myCancelAction = new UnityAction(TestCancelFunction);
    }
    
    //  Send to the Modal Panel to set up the Buttons and functions to call
    public void TestC () {
        modalPanel.Choice ("This is an announcement!\nIf you don't like it, shove off!", TestCancelFunction);
        //  modalPanel.Choice ("This is an announcement!\nIf you don't like it, shove off!", myCancelAction);
    }
    public void TestCI () {
        modalPanel.Choice ("This is an announcement!\nIf you don't like it, shove off!", icon, TestCancelFunction);
        //  modalPanel.Choice ("This is an announcement!\nIf you don't like it, shove off!", myCancelAction);
    }
    
    public void TestYN () {
        modalPanel.Choice ("Cheese on your burger?", TestYesFunction, TestNoFunction);
    //  modalPanel.Choice ("Cheese on your burger?", myYesAction, myNoAction);
    }
    
    public void TestYNC () {
        modalPanel.Choice ("Would you like a poke in the eye?\nHow about with a sharp stick?", TestYesFunction, TestNoFunction, TestCancelFunction);
        //  modalPanel.Choice ("Would you like a poke in the eye?\nHow about with a sharp stick?", myYesAction, myNoAction, myCancelAction);
    }
    
    public void TestYNI () {
        modalPanel.Choice ("Do you like this icon?", icon, TestYesFunction, TestNoFunction, TestCancelFunction);
        //  modalPanel.Choice ("Do you like this icon?", icon, myYesAction, myNoAction, myCancelAction);
    }
    
    public void TestYNCI () {
        modalPanel.Choice ("Do you want to use this icon?", icon, TestYesFunction, TestNoFunction, TestCancelFunction);
        //  modalPanel.Choice ("Do you like this icon?", icon, myYesAction, myNoAction, myCancelAction);
    }
    
    public void TestLambda () {
        modalPanel.Choice ("Do you want to create this sphere?", () => { InstantiateObject(thingToSpawn); }, TestNoFunction);
        //  modalPanel.Choice ("Do you want to create this sphere?", () => { InstantiateObject(thingToSpawn); }, myNoAction);
    }
    
    public void TestLambda2 () {
        modalPanel.Choice ("Do you want to create two spheres?", () => { InstantiateObject(thingToSpawn, thingToSpawn); }, TestNoFunction);
        //  modalPanel.Choice ("Do you want to create two spheres?", () => { InstantiateObject(thingToSpawn, thingToSpawn); }, myNoAction);
    }
    
    public void TestLambda3 () {
        modalPanel.Choice ("Do you want to create three spheres?", () => { InstantiateObject(thingToSpawn); InstantiateObject(thingToSpawn, thingToSpawn); }, TestNoFunction);
        //  modalPanel.Choice ("Do you want to create three spheres?", () => { InstantiateObject(thingToSpawn); InstantiateObject(thingToSpawn, thingToSpawn); }, myNoAction);
    }
    
    //  The function to call when the button is clicked
    //  These are wrapped up in a UnityAction during Awake
    void TestYesFunction () {
        displayManager.DisplayMessage ("Heck, yeah!");
    }
    
    void TestNoFunction () {
        displayManager.DisplayMessage ("No way, Jose!");
    }
    
    void TestCancelFunction () {
        displayManager.DisplayMessage ("I give up!");
    }
    
    void InstantiateObject (GameObject thingToInstantiate) {
        displayManager.DisplayMessage ("Here you go!");
        Instantiate (thingToInstantiate, spawnPoint.position, spawnPoint.rotation);
    }
    
    void InstantiateObject (GameObject thingToInstantiate, GameObject thingToInstantiate2) {
        displayManager.DisplayMessage ("Here you go!");
        Instantiate (thingToInstantiate, spawnPoint.position -  Vector3.one, spawnPoint.rotation);
        Instantiate (thingToInstantiate2, spawnPoint.position + Vector3.one, spawnPoint.rotation);
    }
}

DisplayManager

Code snippet

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class DisplayManager : MonoBehaviour {

    public Text displayText;
    public float displayTime;
    public float fadeTime;
    
    private IEnumerator fadeAlpha;
    
    private static DisplayManager displayManager;
    
    public static DisplayManager Instance () {
        if (!displayManager) {
            displayManager = FindObjectOfType(typeof (DisplayManager)) as DisplayManager;
            if (!displayManager)
                Debug.LogError ("There needs to be one active DisplayManager script on a GameObject in your scene.");
        }
        
        return displayManager;
    }

    public void DisplayMessage (string message) {
        displayText.text = message;
        SetAlpha ();
    }
    
    void SetAlpha () {
        if (fadeAlpha != null) {
            StopCoroutine (fadeAlpha);
        }
        fadeAlpha = FadeAlpha ();
        StartCoroutine (fadeAlpha);
    }
    
    IEnumerator FadeAlpha () {
        Color resetColor = displayText.color;
        resetColor.a = 1;
        displayText.color = resetColor;
        
        yield return new WaitForSeconds (displayTime);
        
        while (displayText.color.a > 0) {
            Color displayColor = displayText.color;
            displayColor.a -= Time.deltaTime / fadeTime;
            displayText.color = displayColor;
            yield return null;
        }
        yield return null;
    }
}

関連するチュートリアル

関連ドキュメント