最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

unity自定義彈出框功能

 更新時(shí)間:2019年11月03日 08:36:35   作者:無名之士  
這篇文章主要為大家詳細(xì)介紹了unity自定義彈出框功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了unity自定義彈出框的具體方法,供大家參考,具體內(nèi)容如下

一、彈出框的搭建

布局如圖:Message為整個(gè)父物體,并且添加UiMessage代碼。panel為遮罩。

MessageBox為整個(gè)提示框,Panel為標(biāo)題,ok為確定按鈕,cancel為取消按鈕,retry為重試按鈕,Text為提示框的文字。

注意大小寫,后面代碼會(huì)根據(jù)名稱進(jìn)行獲取對(duì)應(yīng)組建。

效果如下:

二、MessageBox代碼

要說明的都在代碼中注釋了。仿照Windows的提示框功能,如果功能不足可自行添加。例如關(guān)閉按鈕、顯示圖標(biāo)等。

using System;

public enum DialogResult
{
  Ok,
  OKCancel,
  RetryCancel,
  YesNo,
  YesNoCancel
}

public static class MessageBox
{
  /// <summary>
  /// true表示模態(tài)框
  /// </summary>
  public static bool type;
  //三個(gè)委托,分別為三個(gè)按鈕的點(diǎn)擊運(yùn)行事件
  public static Action clickOk;
  public static Action clickRetry;
  public static Action clickCancel;
  public static DialogResult dialogResult;
  //標(biāo)題
  public static string headText;
  //文本
  public static string text;
  //狀態(tài)。用于顯示或隱藏彈出框
  public static bool state;

  /// <summary>
  ///重試按鈕點(diǎn)擊事件
  /// </summary>
  public static void onClickRetry()
  {
    state = false;
    clickRetry?.Invoke();
    clickRetry = null;
  }
  /// <summary>
  /// 取消按鈕點(diǎn)擊事件
  /// </summary>
  public static void onClickCancel()
  {
    state = false;
    clickCancel?.Invoke();
    clickCancel = null;
  }
  /// <summary>
  /// 確定按鈕點(diǎn)擊事件
  /// </summary>
  public static void onClickOk()
  {
    state = false;
    clickOk?.Invoke();
    clickOk = null;
  }

  /// <summary>
  /// 顯示
  /// </summary>
  /// <param name="_text">內(nèi)容</param>
  /// <param name="_head">標(biāo)題</param>
  /// <param name="dialog">樣式</param>
  /// <param name="type">模式</param>
  public static void Show(string _text,string _head,DialogResult _dialog, bool _type = true)
  {
    text = _text;
    headText = _head;
    dialogResult = _dialog;
    type = _type;
    state = true;
  }
  public static void Show(string _text,string _head,bool _type = true)
  {
    text = _text;
    headText = _head;
    dialogResult = DialogResult.Ok;
    type = _type;
    state = true;
  }
  public static void Show(string _text, bool _type = true)
  {
    text = _text;
    headText = "信息";
    dialogResult = DialogResult.Ok;
    type = _type;
    state = true;
  }

}

三、UiMessage代碼

添加到Message物體上。用于控制彈出框的顯示等功能。

using UnityEngine;
using UnityEngine.UI;

public class UiMessage : MonoBehaviour
{
  public Button ok;
  public Button cancel;
  public Button retry;
  /// <summary>
  /// 遮罩
  /// </summary>
  public GameObject panel;
  public Text headText;
  public Text text;
  /// <summary>
  /// 彈出框
  /// </summary>
  private GameObject messageBox;

  private void Awake()
  {
    messageBox = gameObject.transform.GetChild(1).gameObject;
    ok = messageBox.transform.Find("ok").GetComponent<Button>();
    cancel = messageBox.transform.Find("cancel").GetComponent<Button>();
    retry = messageBox.transform.Find("retry").GetComponent<Button>();
    panel = gameObject.transform.Find("panel").gameObject;
    text = messageBox.transform.Find("Text").GetComponent<Text>();
    headText = messageBox.transform.GetChild(0).Find("head").GetComponent<Text>();

    //將提示框居中顯示
    messageBox.transform.position = new Vector3(Screen.width / 2 - messageBox.GetComponent<RectTransform>().rect.width / 2,
        Screen.height / 2 + messageBox.GetComponent<RectTransform>().rect.height / 2, 0);
    init();
  }

  private void OnEnable()
  {
    init();
  }

  private void init()
  {
    ok.onClick.AddListener(MessageBox.onClickOk);
    cancel.onClick.AddListener(MessageBox.onClickCancel);
    retry.onClick.AddListener(MessageBox.onClickRetry);
    text.text = MessageBox.text;
    headText.text = MessageBox.headText;

    //根據(jù)傳遞的參數(shù),進(jìn)行樣式的顯示
    switch (MessageBox.dialogResult)
    {
      case DialogResult.Ok:
        ok.gameObject.SetActive(true);
        cancel.gameObject.SetActive(false);
        retry.gameObject.SetActive(false);
        break;
      case DialogResult.OKCancel:
        ok.gameObject.SetActive(true);
        cancel.gameObject.SetActive(true);
        retry.gameObject.SetActive(false);
        break;
      case DialogResult.RetryCancel:
        ok.gameObject.SetActive(true);
        cancel.gameObject.SetActive(true);
        retry.gameObject.SetActive(true);
        break;
      case DialogResult.YesNo:
        ok.transform.GetChild(0).GetComponent<Text>().text = "是";
        cancel.transform.GetChild(0).GetComponent<Text>().text = "否";
        ok.gameObject.SetActive(true);
        cancel.gameObject.SetActive(true);
        retry.gameObject.SetActive(false);
        break;
      case DialogResult.YesNoCancel:
        ok.transform.GetChild(0).GetComponent<Text>().text = "是";
        cancel.transform.GetChild(0).GetComponent<Text>().text = "否";
        ok.gameObject.SetActive(true);
        cancel.gameObject.SetActive(true);
        retry.gameObject.SetActive(true);
        break;
    }
  }

  private void Update()
  {
    panel.SetActive(MessageBox.type);
    gameObject.SetActive(MessageBox.state);
  }
}

四、顯示框的調(diào)用

此處調(diào)用可以自行設(shè)置一個(gè)按鈕,在其點(diǎn)擊事件中注冊(cè)調(diào)用即可。

筆者使用項(xiàng)目中的方式進(jìn)行演示。具體不做說明。調(diào)用方式已給出。

特別注意:由于UiMessage調(diào)用了MessageBox的方法,所以必須先初始化MessageBox的數(shù)據(jù)。使用什么就初始化什么。筆者使用了ok、cancel按鈕(默認(rèn)不初始化模式,即為模態(tài)框,不初始化DialogResult即為只顯示ok按鈕),所以注冊(cè)了相應(yīng)的點(diǎn)擊事件(委托)。最后顯示彈出框(整個(gè)包含遮罩和彈出框)。

五、運(yùn)行結(jié)果

六、彈出框可拖拽移動(dòng)

將DragManage添加到MessageBox物體上面。(如果你想讓ui物體可拖拽,對(duì)其添加DragManage即可實(shí)現(xiàn))

筆者就不做演示了

using UnityEngine;
using UnityEngine.EventSystems;

/// <summary>
/// 只是用來處理拖拽
/// </summary>
public class DragManage : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
  private Vector3 offect;

  public void OnBeginDrag(PointerEventData eventData)
  {
    offect = Input.mousePosition - transform.position;
  }

  public void OnDrag(PointerEventData eventData)
  {
    transform.position = Input.mousePosition - offect;
  }

  public void OnEndDrag(PointerEventData eventData)
  {
    transform.position = Input.mousePosition - offect;
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解析C#設(shè)計(jì)模式編程中適配器模式的實(shí)現(xiàn)

    解析C#設(shè)計(jì)模式編程中適配器模式的實(shí)現(xiàn)

    這篇文章主要介紹了C#設(shè)計(jì)模式編程中適配器模式的實(shí)現(xiàn),分別舉了類的對(duì)象適配器與對(duì)象的適配器模式的例子,需要的朋友可以參考下
    2016-02-02
  • C#實(shí)現(xiàn)將一個(gè)矩陣分解為對(duì)稱矩陣與反稱矩陣之和的方法

    C#實(shí)現(xiàn)將一個(gè)矩陣分解為對(duì)稱矩陣與反稱矩陣之和的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)將一個(gè)矩陣分解為對(duì)稱矩陣與反稱矩陣之和的方法,較為詳細(xì)的分析了矩陣分解運(yùn)算的原理與C#實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-08-08
  • Unity實(shí)現(xiàn)圓形Image組件

    Unity實(shí)現(xiàn)圓形Image組件

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)圓形Image組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C#中DrawCurve的用法小結(jié)

    C#中DrawCurve的用法小結(jié)

    本文主要介紹了C#中DrawCurve的用法小結(jié),通常用于繪制一條平滑的曲線通過一系列給定的點(diǎn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-04-04
  • C# WPF上位機(jī)實(shí)現(xiàn)和下位機(jī)TCP通訊的方法

    C# WPF上位機(jī)實(shí)現(xiàn)和下位機(jī)TCP通訊的方法

    這篇文章主要介紹了C# WPF上位機(jī)實(shí)現(xiàn)和下位機(jī)TCP通訊的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-03-03
  • WPF實(shí)現(xiàn)Interaction框架的Behavior擴(kuò)展

    WPF實(shí)現(xiàn)Interaction框架的Behavior擴(kuò)展

    這篇文章介紹了WPF實(shí)現(xiàn)Interaction框架Behavior擴(kuò)展的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Unity 讀取文件 TextAsset讀取配置文件方式

    Unity 讀取文件 TextAsset讀取配置文件方式

    這篇文章主要介紹了Unity 讀取文件 TextAsset讀取配置文件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 分享用于操作FTP的客戶端C#類

    分享用于操作FTP的客戶端C#類

    用.net自帶的FtpWebRequest做的ftp客戶端得程序,有一個(gè)功能實(shí)現(xiàn)起來會(huì)非??嚯y,就是移動(dòng)文件和文件夾的功能。所以后來又找了一個(gè)類,用socket實(shí)現(xiàn)的,發(fā)現(xiàn)比用ftpWebRequest功能要強(qiáng)?;镜膄tp客戶端得命令都實(shí)現(xiàn)了。
    2015-05-05
  • 最新評(píng)論

    曲沃县| 辰溪县| 醴陵市| 务川| 黄陵县| 荔波县| 姚安县| 贵港市| 申扎县| 东方市| 绥江县| 宝丰县| 长丰县| 临洮县| 威信县| 武宁县| 婺源县| 娱乐| 图木舒克市| 商都县| 于田县| 灵台县| 阿巴嘎旗| 北碚区| 普洱| 汽车| 霍山县| 安化县| 广元市| 石嘴山市| 东山县| 怀远县| 新邵县| 南华县| 阳东县| 宜阳县| 四平市| 枣强县| 南皮县| 遵化市| 银川市|