栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

unity中创建询问弹出窗口

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unity中创建询问弹出窗口

在开发过程中进程会遇到需要弹出一个窗口询问用户是否进行的操作,今天就来制作一个这样弹出窗口,然后根据弹出窗口的选择内容不同进行不同的操作。本例中主要是为了删除一个数据,而在删除数据操作前需要得到用户的一个确认操作。
这里面主要用到了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");   //建立消息监听

这些都处理好了我们要做就是显示窗口就可能,如下所示。

/// 
    /// 显示循环消息完毕后通过ASKBL的状态判断选取对象
    /// 
    /// 
    /// 
    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().Teile.text = tile;
        go.GetComponent().MSGDATA.text = MSG;
        go.GetComponent().ask.sign = sign;


    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901902.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号