2022년 3월 25일 금요일

[Unity} How to pop up multiple window with error message.

 1. I would like to pop error message window up. 

     Before I just use one designated 'Error Pop Up Window' and 'SetActive(true)' command. 

2. But now I call multiple function with Delegate Chain. If I use only one pop up window and there are multiple error message in miltiple function, that will give me error in my life. So I figured out this "Hmm. Will I instantiate Pop Up window as prefab? " I did but Pop Up window instantiate where I can't see.

    public void PopErrInstantiate()

    {

        GameObject childObject = Instantiate(popErrorPref) as GameObject;

     }

3.Then let's make Pop Up window instantiate & let this be the child of main Canvas. 

    public void PopErrInstantiate(s)

    {

        GameObject childObject = Instantiate(popErrorPref) as GameObject;

        childObject.transform.parent = Ui_ControlUi.BigCanvas.transform;

    }

4. OK. Let's destroy this pop up before my game will be stacked withPop Up window object. I put destroy in the close funtion that was attached in Close button of Pop Up window. 

        public void Close()

        {

            GameObject one = canvas.transform.parent.transform.parent.gameObject;

            Destroy(one);

        } 

 

Full Code is below


I made a prefab with UI_test level hierarchy. DialogueUI script is attached in [Dialog UI Test]. 

Below is the code of Dialouge UI 


namespace EasyUI.Dialogue
{
    public class Dialogue
    {
        public string Message = "Message goes here.";
    }

    public class DialogueUI : MonoBehaviour
    {
        [SerializeField] GameObject canvas; 
        [SerializeField] Text messageUIText;
        [SerializeField] Button closeButton;

        Dialogue dialogue = new Dialogue();

        //public Control_UIs ConUDialog;
        public static DialogueUI Instance;

        private void Awake()
        {
            Instance = this;
            closeButton.onClick.RemoveAllListeners();
            closeButton.onClick.AddListener(Close);
        }

        public DialogueUI SetMessage(string message)
        {
            dialogue.Message = message;
            return Instance;
        }

        public void Show()
        {
            messageUIText.text = dialogue.Message;
            canvas.SetActive(true);
        }

        public void Close()
        {
            canvas.SetActive(false);
            GameObject one = canvas.transform.parent.transform.parent.gameObject;
            Destroy(one);
        }
    }
}

That code is from Youtube tutorial 


And I call that function like this. 

        else
        {
            string message;
            message = "Band members should be less than 9"; 
            ControlUi_casting.PopErrInstantiate(message); //생성해서 띄우는 팝업창 열기
        } 






댓글 없음:

댓글 쓰기