鸟语天空
UGUI—对话框(DialogBox)
post by:追风剑情 2021-9-27 15:57

示例:对话框

工程截图

111.png

代码


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 对话框UI
/// </summary>
public class UIDialogBox : GameEventBehaviour
{
    public static UIDialogBox Instance { get; private set; }

    [SerializeField]
    private GameObject mask;
    [SerializeField]
    private GameObject okButton;
    [SerializeField]
    private GameObject cancelButton;
    [SerializeField]
    private Text m_Text;
    [SerializeField]
    private Text m_TextOk;
    [SerializeField]
    private Text m_TextCancel;
    private Action okCallback = null;
    private Action cancelCallback = null;
    private float freezeClickTime = -1f;

    public enum ButtonLayout
    {
        None,       //不显示操作按钮
        OK,         //只显示确认按钮
        OK_Cancel   //同时显示确认和取消按钮
    }

    protected override void OnAwake()
    {
        Instance = this;
    }

    protected override void OnUpdate()
    {
        if (freezeClickTime <= 0)
            return;

        freezeClickTime -= Time.deltaTime;
        if (freezeClickTime <= 0)
        {
            m_TextOk.text = Config.LAN_BUTTON_SURE;
        }
        else
        {
            int cd = Mathf.CeilToInt(freezeClickTime);
            m_TextOk.text = string.Format("{0}({1})", Config.LAN_BUTTON_SURE, cd);
        }
    }

    public void Close()
    {
        m_TextOk.text = string.Empty;
        m_TextCancel.text = string.Empty;
        mask.SetActive(false);
    }

    public void OnClickOK()
    {
        if (freezeClickTime > 0)
            return;

        Close();
        okCallback?.Invoke();
        okCallback = null;
        cancelCallback = null;
    }

    public void OnClickCancel()
    {
        Close();
        cancelCallback?.Invoke();
        cancelCallback = null;
        okCallback = null;
        freezeClickTime = -1;
    }

    public void SetCancelText(string text)
    {
        m_TextCancel.text = text;
    }

    public void SetOkText(string text)
    {
        m_TextOk.text = text;
    }

    public void Show(string text, ButtonLayout layout=ButtonLayout.OK, Action okCallback=null, float freezeClickTime = -1)
    {
        if (string.IsNullOrEmpty(m_TextOk.text))
            m_TextOk.text = "确定";
        if (string.IsNullOrEmpty(m_TextCancel.text))
            m_TextCancel.text = "取消";
        m_Text.text = text + "\n";
        m_TextOk.text = Config.LAN_BUTTON_SURE;
        mask.SetActive(true);
        okButton.SetActive(layout == ButtonLayout.OK || layout == ButtonLayout.OK_Cancel);
        cancelButton.SetActive(layout == ButtonLayout.OK_Cancel);

        RectTransform okRectTransform = okButton.GetComponent<RectTransform>();
        if (layout == ButtonLayout.OK_Cancel)
            SetAnchoredPositionX(okRectTransform, -121f);
        else
            SetAnchoredPositionX(okRectTransform, 0f);

        this.okCallback = okCallback;
        this.freezeClickTime = freezeClickTime;
    }

    private void SetAnchoredPositionX(RectTransform rectTransform, float x)
    {
        Vector2 anchoredPosition = rectTransform.anchoredPosition;
        anchoredPosition.x = x;
        rectTransform.anchoredPosition = anchoredPosition;
    }
}


效果

2222.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容