本文執行個體為大家分享了unity自定義彈出框的具體方法,供大家參考,具體内容如下
一、彈出框的搭建
布局如圖:Message為整個父物體,并且添加UiMessage代碼。panel為遮罩。
MessageBox為整個提示框,Panel為标題,ok為确定按鈕,cancel為取消按鈕,retry為重試按鈕,Text為提示框的文字。
注意大小寫,後面代碼會根據名稱進行擷取對應組建。
效果如下:
二、MessageBox代碼
要說明的都在代碼中注釋了。仿照Windows的提示框功能,如果功能不足可自行添加。例如關閉按鈕、顯示圖示等。
using System;
public enum DialogResult
{
Ok,
OKCancel,
RetryCancel,
YesNo,
YesNoCancel
}
public static class MessageBox
{
///
/// true表示模态框
///
public static bool type;
//三個委托,分别為三個按鈕的點選運作事件
public static Action clickOk;
public static Action clickRetry;
public static Action clickCancel;
public static DialogResult dialogResult;
//标題
public static string headText;
//文本
public static string text;
//狀态。用于顯示或隐藏彈出框
public static bool state;
///
///重試按鈕點選事件
///
public static void onClickRetry()
{
state = false;
clickRetry?.Invoke();
clickRetry = null;
}
///
/// 取消按鈕點選事件
///
public static void onClickCancel()
{
state = false;
clickCancel?.Invoke();
clickCancel = null;
}
///
/// 确定按鈕點選事件
///
public static void onClickOk()
{
state = false;
clickOk?.Invoke();
clickOk = null;
}
///
/// 顯示
///
/// 内容
/// 标題
/// 樣式
/// 模式
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;
///
/// 遮罩
///
public GameObject panel;
public Text headText;
public Text text;
///
/// 彈出框
///
private GameObject messageBox;
private void Awake()
{
messageBox = gameObject.transform.GetChild(1).gameObject;
ok = messageBox.transform.Find("ok").GetComponent();
cancel = messageBox.transform.Find("cancel").GetComponent();
retry = messageBox.transform.Find("retry").GetComponent();
panel = gameObject.transform.Find("panel").gameObject;
text = messageBox.transform.Find("Text").GetComponent();
headText = messageBox.transform.GetChild(0).Find("head").GetComponent();
//将提示框居中顯示
messageBox.transform.position = new Vector3(Screen.width / 2 - messageBox.GetComponent().rect.width / 2,
Screen.height / 2 + messageBox.GetComponent().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;
//根據傳遞的參數,進行樣式的顯示
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 = "是";
cancel.transform.GetChild(0).GetComponent().text = "否";
ok.gameObject.SetActive(true);
cancel.gameObject.SetActive(true);
retry.gameObject.SetActive(false);
break;
case DialogResult.YesNoCancel:
ok.transform.GetChild(0).GetComponent().text = "是";
cancel.transform.GetChild(0).GetComponent().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);
}
}
四、顯示框的調用
此處調用可以自行設定一個按鈕,在其點選事件中注冊調用即可。
筆者使用項目中的方式進行示範。具體不做說明。調用方式已給出。
特别注意:由于UiMessage調用了MessageBox的方法,是以必須先初始化MessageBox的資料。使用什麼就初始化什麼。筆者使用了ok、cancel按鈕(預設不初始化模式,即為模态框,不初始化DialogResult即為隻顯示ok按鈕),是以注冊了相應的點選事件(委托)。最後顯示彈出框(整個包含遮罩和彈出框)。
五、運作結果
六、彈出框可拖拽移動
将DragManage添加到MessageBox物體上面。(如果你想讓ui物體可拖拽,對其添加DragManage即可實作)
筆者就不做示範了
using UnityEngine;
using UnityEngine.EventSystems;
///
/// 隻是用來處理拖拽
///
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;
}
}
以上就是本文的全部内容,希望對大家的學習有所幫助,也希望大家多多支援腳本之家。