123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using UnityEngine.UI;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using GameFramework.Event;
- /// <summary>
- /// 对话框类型
- /// </summary>
- public enum E_DialogType
- {
- /// <summary>
- /// 一个按钮
- /// </summary>
- OneButton = 1,
- /// <summary>
- /// 两个按钮
- /// </summary>
- TwoBntton = 2,
- }
- /// <summary>
- /// 提示级别
- /// </summary>
- public enum E_CueLevel
- {
- /// <summary>
- /// 低级(提示作用,只有一个确定)
- /// </summary>
- Low = 0,
- /// <summary>
- /// 中级(重新更新)
- /// </summary>
- Mid = 1,
- /// <summary>
- /// 高级(重新登陆)
- /// </summary>
- High = 2,
- /// <summary>
- /// 超级(重新更新 重新登陆)
- /// </summary>
- Super = 3,
- }
- /// <summary>
- /// 对话框
- /// </summary>
- public class UI_CueDialog : MonoBehaviour
- {
- /// <summary>
- /// 提示框单键
- /// </summary>
- private static UI_CueDialog mInstance = null;
- /// <summary>
- /// 提示框主体
- /// </summary>
- private GameObject mCueObj = null;
- /// <summary>
- /// 背景,有碰撞,遮挡其他 UI
- /// </summary>
- private GameObject mBG = null;
- /// <summary>
- /// 出提示框对象
- /// </summary>
- private GameObject mTipsObj = null;
- /// <summary>
- /// 出提示框的时间间隔
- /// </summary>
- private int mInterval = 0;
- /// <summary>
- /// 确定按钮
- /// </summary>
- private Button mButtonConfirm = null;
- /// <summary>
- /// 确定按钮上的文字
- /// </summary>
- private Text mConfirmText = null;
- /// <summary>
- /// 取消按钮
- /// </summary>
- private Button mButtonCancel = null;
- /// <summary>
- /// 取消按钮上的文字
- /// </summary>
- private Text mCancelText = null;
- /// <summary>
- /// 标题
- /// </summary>
- private Text mTitle = null;
- /// <summary>
- /// 内容
- /// </summary>
- private Text mContent = null;
- /// <summary>
- /// 提示内容
- /// </summary>
- private Text mTipsContent = null;
- /// <summary>
- /// 确定回调
- /// </summary>
- private Action mAc_Confirm = null;
- /// <summary>
- /// 取消回调
- /// </summary>
- private Action mAc_Cancel = null;
- /// <summary>
- /// 按钮布局:左
- /// </summary>
- private Transform mLeft = null;
- /// <summary>
- /// 按钮布局:中
- /// </summary>
- private Transform mMid = null;
- /// <summary>
- /// 按钮布局:右
- /// </summary>
- private Transform mRight = null;
- /// <summary>
- /// 获取单键
- /// </summary>
- /// <returns> 返回提示框实例 </returns>
- public static UI_CueDialog Instance()
- {
- return mInstance;
- }
- /// <summary>
- /// 初始化
- /// </summary>
- private void Awake()
- {
- mInstance = this;
-
- mCueObj = transform.Find("GameObject").gameObject;
- mBG = transform.Find("GameObject/Panel/bg").gameObject;
- mTipsObj = transform.Find("TipsPanel").gameObject;
- mTitle = transform.Find("GameObject/Panel/title").gameObject.GetComponent<Text>();
- mContent = transform.Find("GameObject/Panel/content").gameObject.GetComponent<Text>();
- mTipsContent = transform.Find("TipsPanel/Panel/ContentText").gameObject.GetComponent<Text>();
- mButtonConfirm = transform.Find("GameObject/Panel/ButtonConfirm").GetComponent<Button>();
- EventTriggerListenerDialog.Get(mButtonConfirm.gameObject).onClick = ((_btn) =>
- {
- OkEvent();
- });
- mConfirmText = transform.Find("GameObject/Panel/ButtonConfirm/Text").gameObject.GetComponent<Text>();
- mButtonCancel = transform.Find("GameObject/Panel/ButtonCancel").GetComponent<Button>();
- EventTriggerListenerDialog.Get(mButtonCancel.gameObject).onClick = ((_btn) =>
- {
- CancelEvent();
- });
- mCancelText = transform.Find("GameObject/Panel/ButtonCancel/Text").gameObject.GetComponent<Text>();
- mLeft = transform.Find("GameObject/Panel/left");
- mMid = transform.Find("GameObject/Panel/mid");
- mRight = transform.Find("GameObject/Panel/right");
- //mTitle.font = FontManager.Instance().CurFont;
- //mContent.font = FontManager.Instance().CurFont;
- //mConfirmText.font = FontManager.Instance().CurFont;
- //mCancelText.font = FontManager.Instance().CurFont;
- }
- /// <summary>
- /// 如果当前有打开的对话框,替换文字显示。(应用场景有限)
- /// </summary>
- /// <param name="strContent"></param>
- /// <param name="strTitle"></param>
- /// <param name="type"></param>
- /// <param name="okFun"></param>
- /// <param name="cancelFun"></param>
- /// <param name="leftBtnName"></param>
- /// <param name="rightBtnName"></param>
- public void OpenAndReplace(string strContent, string strTitle = "", E_DialogType type = E_DialogType.OneButton, Action okFun = null, Action cancelFun = null, string leftBtnName = "确定", string rightBtnName = "取消")
- {
- Canvas canvas = this.gameObject.GetComponent<Canvas>();
- if (canvas.worldCamera == null)
- {
- SetFont(this.gameObject);
- SetCamera(this.gameObject, string.Empty);
- }
- mButtonConfirm.gameObject.SetActive(true);
- mCueObj.SetActive(true);
- mBG.SetActive(true); // 开启背景,遮挡其他 UI
- // 设置标题和内容
- mTitle.text = strTitle;
- mContent.text = strContent;
- // 设置按钮上的文字
- mConfirmText.text = leftBtnName;
- mCancelText.text = rightBtnName;
- // 记录回调
- mAc_Confirm = okFun;
- mAc_Cancel = cancelFun;
- // 显示一个按钮
- if (E_DialogType.OneButton == type)
- {
- mButtonConfirm.transform.localPosition = mMid.localPosition;
- mButtonCancel.gameObject.SetActive(false);
- // 点击空白处等同于点中间按钮
- EventTriggerListenerDialog.Get(mBG).onClick = ((_btn) =>
- {
- OkEvent();
- });
- }
- else if (E_DialogType.TwoBntton == type)
- {
- // 显示两个按钮
- mButtonConfirm.transform.localPosition = mLeft.localPosition;
- mButtonCancel.gameObject.SetActive(true);
- mButtonCancel.transform.localPosition = mRight.localPosition;
- // 点空白处无响应
- mBG.GetComponent<Button>().onClick = null;
- }
- }
- /// <summary>
- /// 打开提示框(同时只会存在一个)
- /// </summary>
- /// <param name="strContent"> 文字内容 </param>
- /// <param name="strTitle"> 文字标题 </param>
- /// <param name="type"> 按钮布局类型 </param>
- /// <param name="okFun"> 确定回调 </param>
- /// <param name="cancelFun"> 取消回调 </param>
- /// <param name="leftBtnName"> 左按钮上的文字 </param>
- /// <param name="rightBtnName"> 右按钮上的文字 </param>
- public void Open(string strContent, string strTitle = "", E_DialogType type = E_DialogType.OneButton, Action okFun = null, Action cancelFun = null, string leftBtnName = "确定", string rightBtnName = "取消")
- {
- // Debug.LogError(" 打开提示框(同时只会存在一个)");
- if (!mCueObj.activeSelf)
- {
- Canvas canvas = this.gameObject.GetComponent<Canvas>();
- if (canvas.worldCamera == null)
- {
- SetFont(this.gameObject);
- SetCamera(this.gameObject, string.Empty);
- }
- mButtonConfirm.gameObject.SetActive(true);
- mCueObj.SetActive(true);
- mBG.SetActive(true); // 开启背景,遮挡其他 UI
- // 设置标题和内容
- mTitle.text = strTitle;
- mContent.text = strContent;
- // 设置按钮上的文字
- mConfirmText.text = leftBtnName;
- mCancelText.text = rightBtnName;
- // 记录回调
- mAc_Confirm = okFun;
- mAc_Cancel = cancelFun;
- // 显示一个按钮
- if (E_DialogType.OneButton == type)
- {
- mButtonConfirm.transform.localPosition = mMid.localPosition;
- mButtonCancel.gameObject.SetActive(false);
- // 点击空白处等同于点中间按钮
- EventTriggerListenerDialog.Get(mBG).onClick = ((_btn) =>
- {
- OkEvent();
- });
- }
- else if (E_DialogType.TwoBntton == type)
- {
- // 显示两个按钮
- mButtonConfirm.transform.localPosition = mLeft.localPosition;
- mButtonCancel.gameObject.SetActive(true);
- mButtonCancel.transform.localPosition = mRight.localPosition;
- // 点空白处无响应
- mBG.GetComponent<Button>().onClick = null;
- }
- }
- }
- /// <summary>
- /// 打开提示框ios占用框(不能用用户自行关闭)
- /// </summary>
- /// <param name="strContent"> 文字内容 </param>
- /// <param name="strTitle"> 文字标题 </param>
- /// <param name="type"> 按钮布局类型 </param>
- /// <param name="okFun"> 确定回调 </param>
- /// <param name="cancelFun"> 取消回调 </param>
- /// <param name="leftBtnName"> 左按钮上的文字 </param>
- /// <param name="rightBtnName"> 右按钮上的文字 </param>
- public void OpenIOSTIPS(string strContent, string strTitle = "")
- {
- // Debug.LogError(" 打开ios提示框(用户无法自行关闭)");
- Canvas canvas = this.gameObject.GetComponent<Canvas>();
- if (canvas.worldCamera == null)
- {
- SetFont(this.gameObject);
- SetCamera(this.gameObject, string.Empty);
- }
- mCueObj.SetActive(true);
- mBG.SetActive(true); // 开启背景,遮挡其他 UI
- // 设置标题和内容
- mTitle.text = strTitle;
- mContent.text = strContent;
- mButtonCancel.gameObject.SetActive(false);
- mButtonConfirm.gameObject.SetActive(false);
- }
- /// <summary>
- /// 打开提示栏不能遮挡后面ui,3秒后自动消失
- /// </summary>
- /// <param name="strContent"></param>
- public void OpenTips(string strContent)
- {
- mInterval = 0;
- mTipsObj.SetActive(false);
- mTipsObj.SetActive(true);
- mTipsContent.text = strContent;
- // Invoke("AutoClose",3f);
- }
- /// <summary>
- /// 设置 UI 的字体
- /// </summary>
- /// <param name="uiObj"> UI对象 </param>
- public void SetFont(GameObject uiObj)
- {
- if (null == uiObj)
- {
- Debug.LogError("PanelHelper 设置字体,UI 为空,中断");
- return;
- }
- // 获取所有的 Text
- Text[] allText = uiObj.GetComponentsInChildren<Text>(true);
- // 设置字体
- //for (int i = 0; i < allText.Length; i++)
- //{
- // allText[i].font = FontManager.Instance().CurFont;
- // allText[i].fontStyle = FontStyle.Normal;
- //}
- }
- /// <summary>
- /// 设置 UI 的摄像机
- /// </summary>
- /// <param name="uiObj"> UI对象 </param>
- /// <param name="camPath">指定相机位置</param>
- public void SetCamera(GameObject uiObj, string camPath)
- {
- return;
- if (null == uiObj)
- {
- Debug.LogError("PanelHelper 设置摄像机,UI 为空,中断");
- return;
- }
- GameObject cameraObj = null;
- if (camPath.Trim().Length > 0)
- {
- cameraObj = GameObject.Find(camPath);
- }
- else
- {
- cameraObj = GameObject.Find("/UICamera");
- }
- if (null == cameraObj)
- {
- Debug.LogError("PanelHelper 设置摄像机,找不到摄像机,中断");
- return;
- }
- Canvas canvas = uiObj.GetComponent<Canvas>();
- if (null == canvas)
- {
- Debug.LogError("PanelHelper 设置摄像机,获取画布组件失败,中断");
- return;
- }
- canvas.worldCamera = cameraObj.GetComponent<Camera>();
- }
- /// <summary>
- /// 用于网络异常提示
- /// </summary>
- /// <param name="strContent"> 文字字符串 </param>
- /// <param name="level"> 提示级别 </param>
- public void NetCue(string strContent, E_CueLevel level = E_CueLevel.Low)
- {
- // 关闭网络 Loading ,暂时屏蔽
- ////Loading.Instance().CloseNetLoading();
- switch (level)
- {
- case E_CueLevel.Low:
- UI_CueDialog.Instance().Open(strContent);
- break;
- case E_CueLevel.Mid:
- UI_CueDialog.Instance().Open(strContent, string.Empty, E_DialogType.TwoBntton, ReUpdate, ExitGame, "重试", "退出游戏");
- break;
- case E_CueLevel.High:
- UI_CueDialog.Instance().Open(strContent, string.Empty, E_DialogType.TwoBntton, ReLogin, ExitGame, "返回登陆", "退出游戏");
- break;
- case E_CueLevel.Super:
- UI_CueDialog.Instance().Open(strContent, string.Empty, E_DialogType.TwoBntton, ReUpdate, ExitGame, "重新链接", "退出游戏");
- break;
- default:
- UI_CueDialog.Instance().Open("客户端异常", string.Empty, E_DialogType.OneButton, ExitGame);
- break;
- }
- }
- public void OnDestroy()
- {
- Debug.Log("UI_CueDialog 被销毁!");
- }
- /// <summary>
- /// 退出游戏提示
- /// </summary>
- public void ExitGameCue()
- {
- UI_CueDialog.Instance().Open("您确定要退出游戏?", "退出", E_DialogType.TwoBntton, ExitGame);
- }
- private void ExitCalback(LoyalSoftSDK.LoyalGameCallbackData data)
- {
- Debug.Log("退出时输出:" + data.action + " " + data.msg + " " + data.data);
- if (data.action.Equals("0"))
- {
- // GameInfo.GetPlayerInfo("exitServer");
- ExitGame();
- }
- else if (data.action.Equals("999"))
- {
- ExitGameCue();
- }
- }
- /// <summary>
- /// 更新
- /// </summary>
- private void Update()
- {
- if (Input.GetKeyUp(KeyCode.Escape))
- {
- if (Application.platform == RuntimePlatform.Android && GlobalConfig.is_SDK) ////如果在接入sdk的情况下调用sdk接口
- {
- //LoyalSoftSDK.LoyalGameSDK.Instance.ExitGame(GameInfo.GetPlayerInfo("exitServer"));
- //if (!LoyalSoftSDK.LoyalGameSDK.Instance.SDKExit(GameInfo.GetPlayerInfo("exitServer")))
- //{
- // ExitGameCue();
- //}
- //LoyalSoftSDK.LoyalGameSDK.Instance.OnSDKExitCallback = delegate(LoyalSoftSDK.LoyalGameCallbackData data)
- //{
- // ExitCalback(data);
- //};
- // 退出
- if (AndroidInteractive.CallJavaFunction("OnExitGame") == false)
- {
- ExitGameCue();
- }
- }
- else
- {
- ExitGameCue();
- }
- }
- if (mInterval > 150)
- {
- mTipsObj.SetActive(false);
- }
- else
- {
- mInterval++;
- }
- }
- /// <summary>
- /// 重新更新
- /// </summary>
- private void ReUpdate()
- {
- ////LevelManager.Instance().LoadLevel(E_LevelInfo.ResUpdate);
- }
- /// <summary>
- /// 重新登陆
- /// </summary>
- private void ReLogin()
- {
- ////LevelManager.Instance().LoadLevel(E_LevelInfo.Login);
- }
- /// <summary>
- /// 退出游戏
- /// </summary>
- private void ExitGame()
- {
- // LoyalSoft. LoyalGameSDK.Instance.FoceExit();
- Application.Quit();
- }
- /// <summary>
- /// 确定事件
- /// </summary>
- /// <param name="obj"> 按钮对象 </param>
- private void OkEvent()
- {
- ////AudioManager.Instance.PlaySound(SoundName.normal);
- //// 先关闭提示窗口,再执行回调,就可以连续打开 CueDialog
- Close();
- if (null != mAc_Confirm)
- {
- mAc_Confirm();
- }
- }
- /// <summary>
- /// 取消事件
- /// </summary>
- /// <param name="obj"> 按钮对象 </param>
- private void CancelEvent()
- {
- ////AudioManager.Instance.PlaySound(SoundName.normal);
- //// 先关闭提示窗口,再执行回调,就可以连续打开 CueDialog
- Close();
- if (null != mAc_Cancel)
- {
- mAc_Cancel();
- }
- }
- /// <summary>
- /// 关闭本窗口
- /// </summary>
- public void Close()
- {
- mCueObj.SetActive(false);
- mBG.SetActive(false);
- }
- /// <summary>
- /// 关闭提示窗口
- /// </summary>
- public void TipsClose()
- {
- mTipsObj.SetActive(false);
- }
- }
|