unity UGUI之共有彈框類

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class GlobalTipUIView : LuaBehaviour
{
    public enum MessageType
    {
        OK,               //ok
        OK_CANCEL,        //不帶參
        OK_CANCEL_Param,  //帶參
    }
    public delegate void Callback();
    public delegate void CallbackParam(object obj);
    private string strTitle;
    private string strContent;
    private MessageType type;
    private Callback okCall;
    private Callback cancelCall;
    private Callback callFun;

    private CallbackParam okCallParam;
    private CallbackParam cancelCallParam;
    private CallbackParam callFunParam;
    private object okpram;
    private object cancelparam;
    private object callparam;

    private Text txtTitle;
    private Text txtContent;
    private Button Btn_ok_one;
    private Button Btn_ok;
    private Button Btn_cancel;

    private void Start()
    {
        txtTitle = this.transform.Find("Bg/di/Title/Text").GetComponent<Text>();
        txtContent = this.transform.Find("Bg/di/Content").GetComponent<Text>();
        if (!string.IsNullOrEmpty(strTitle))
            txtTitle.text = strTitle;
            txtTitle.gameObject.SetActive(true);
        if (!string.IsNullOrEmpty(strContent))
        {
            txtContent.text = strContent.Replace(" ", "\u00A0");
            txtContent.gameObject.SetActive(true);
        }
        GameObject ok = this.transform.Find("Bg/di/ok").gameObject;
        GameObject ok_one = this.transform.Find("Bg/di/ok_one").gameObject;
        GameObject cancel = this.transform.Find("Bg/di/cancel").gameObject;
        Btn_ok_one = ok_one.GetComponent<Button>();
        Btn_ok = ok.GetComponent<Button>();
        Btn_cancel = cancel.GetComponent<Button>();
        switch (type)
        {
            case MessageType.OK_CANCEL:
            case MessageType.OK_CANCEL_Param:
                ok_one.SetActive(false);
                ok.SetActive(true);
                cancel.SetActive(true);
                break;
            default:
                ok_one.SetActive(true);
                ok.SetActive(false);
                cancel.SetActive(false);
                break;
        }
        Btn_ok_one.onClick.AddListener(OnOkClick);
        Btn_ok.onClick.AddListener(OnOkClick);
        Btn_cancel.onClick.AddListener(OnCancelClick);
    }
    private void OnOkClick()
    {
        callFun = okCall;
        callFunParam = okCallParam;
        callparam = okpram;
        Close();
    }
    private void OnCancelClick()
    {
        callFun = cancelCall;
        callFunParam = cancelCallParam;
        callparam = cancelparam;
        Close();
    }

    private void Close()
    {
        GameEntry.UIManager.Hide<GlobalTipUIView>();
        switch (type)
        {
            case MessageType.OK:
                if (okCall != null)
                    okCall();
                break;

            case MessageType.OK_CANCEL:
                if (callFun != null)
                    callFun();
                else if (cancelCall != null)
                    cancelCall();
                break;

            case MessageType.OK_CANCEL_Param:
                if (callFunParam != null)
                    callFunParam(callparam);
                else if (cancelCallParam != null)
                    cancelCallParam(cancelparam);
                break;
        }
    }

    public static void Show(string title,string content ,MessageType type = MessageType.OK, Callback okclick = null,
                            Callback cancelclick = null)
    {
        GlobalTipUIView globalTipUIView = GameEntry.UIManager.Show<GlobalTipUIView>();
        if (globalTipUIView == null)
            return;
        globalTipUIView.strTitle = title;
        globalTipUIView.strContent = content;
        globalTipUIView.type = type;
        globalTipUIView.okCall = okclick;
        globalTipUIView.cancelCall = cancelclick;
    }
    public static void Show(string title, string content, MessageType type, CallbackParam okCallParam, object okpram,
                        CallbackParam cancelCallParam = null, object cancelparam = null)
    {
        GlobalTipUIView globalTipUIView = GameEntry.UIManager.Show<GlobalTipUIView>();
        if (globalTipUIView == null)
            return;

        globalTipUIView.strTitle = title;
        globalTipUIView.strContent = content;
        globalTipUIView.type = type;
        globalTipUIView.okCallParam = okCallParam;
        globalTipUIView.cancelCallParam = cancelCallParam;
        globalTipUIView.okpram = okpram;
        globalTipUIView.cancelparam = cancelparam;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章