在开发过程中进程会遇到需要弹出一个窗口询问用户是否进行的操作,今天就来制作一个这样弹出窗口,然后根据弹出窗口的选择内容不同进行不同的操作。本例中主要是为了删除一个数据,而在删除数据操作前需要得到用户的一个确认操作。
这里面主要用到了NotificationCenter插件内容来进行消息传递,经过尝试发现消息传递的方式传递效率远高于携程方式。具体先看这窗口消息代码:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public struct Ask
{
public string sign;
public bool ASKBL;
}
public class AskWindow : MonoBehaviour
{
// Start is called before the first frame update
public TextMeshProUGUI Teile;
public TextMeshProUGUI MSGDATA;
public Button OK;
public Button cancel;
public Ask ask;
void Start()
{
ask = new Ask();
ask.ASKBL = false;
OK.onClick.AddListener(OnOK);
cancel.onClick.AddListener(Oncancel);
}
void Oncancel()
{
ask.ASKBL = false;
gameObject.SetActive(false);
NotificationCenter.DefaultCenter().PostNotification(this, "GetWindowsMSG", ask);
}
void OnOK()
{
ask.ASKBL = true;
gameObject.SetActive(false);
NotificationCenter.DefaultCenter().PostNotification(this, "GetWindowsMSG", ask);
}
// Update is called once per frame
void Update()
{
}
}
如上每个按钮中都增加了一个消息发送内容,在这里定义了一个结构Ask 的结构,用来记录窗口的内容以及传入的消息区分方式。
然后回到处理的代码中增加如下内容:
void GetWindowsMSG(Notification note) //建立处理消息
{
Ask st = (Ask)note.data; 获取传递过来的消息
if (st.sign== "Dtable"&&st.ASKBL)
{
...............
}
if (st.sign == "DELtablelie" && st.ASKBL)
{
....................
}
}
以及消息声明
NotificationCenter.DefaultCenter().AddObserver(this, "GetWindowsMSG"); //建立消息监听
这些都处理好了我们要做就是显示窗口就可能,如下所示。
/// <summary>
/// 显示循环消息完毕后通过ASKBL的状态判断选取对象
/// </summary>
/// <param name="tile"></param>
/// <param name="MSG"></param>
static public void ShowASK(string tile, string MSG,string sign)
{
GameObject root = GameObject.Find("Windows");
GameObject go = root.transform.Find("ASKWindows").gameObject;
go.SetActive(true);
go.GetComponent<AskWindow>().Teile.text = tile;
go.GetComponent<AskWindow>().MSGDATA.text = MSG;
go.GetComponent<AskWindow>().ask.sign = sign;
}