unity中创建询问弹出窗口

在开发过程中进程会遇到需要弹出一个窗口询问用户是否进行的操作,今天就来制作一个这样弹出窗口,然后根据弹出窗口的选择内容不同进行不同的操作。本例中主要是为了删除一个数据,而在删除数据操作前需要得到用户的一个确认操作。这是需要创建的对象基本上就是两个按钮也就是一个确认一个取消按钮,再就是消息内容即两个text对象
这里面主要用到了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;


    }

最后效果见图所示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值