UI_CueDialog.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using UnityEngine.UI;
  6. using UnityEngine.Events;
  7. using UnityEngine.EventSystems;
  8. using GameFramework.Event;
  9. /// <summary>
  10. /// 对话框类型
  11. /// </summary>
  12. public enum E_DialogType
  13. {
  14. /// <summary>
  15. /// 一个按钮
  16. /// </summary>
  17. OneButton = 1,
  18. /// <summary>
  19. /// 两个按钮
  20. /// </summary>
  21. TwoBntton = 2,
  22. }
  23. /// <summary>
  24. /// 提示级别
  25. /// </summary>
  26. public enum E_CueLevel
  27. {
  28. /// <summary>
  29. /// 低级(提示作用,只有一个确定)
  30. /// </summary>
  31. Low = 0,
  32. /// <summary>
  33. /// 中级(重新更新)
  34. /// </summary>
  35. Mid = 1,
  36. /// <summary>
  37. /// 高级(重新登陆)
  38. /// </summary>
  39. High = 2,
  40. /// <summary>
  41. /// 超级(重新更新 重新登陆)
  42. /// </summary>
  43. Super = 3,
  44. }
  45. /// <summary>
  46. /// 对话框
  47. /// </summary>
  48. public class UI_CueDialog : MonoBehaviour
  49. {
  50. /// <summary>
  51. /// 提示框单键
  52. /// </summary>
  53. private static UI_CueDialog mInstance = null;
  54. /// <summary>
  55. /// 提示框主体
  56. /// </summary>
  57. private GameObject mCueObj = null;
  58. /// <summary>
  59. /// 背景,有碰撞,遮挡其他 UI
  60. /// </summary>
  61. private GameObject mBG = null;
  62. /// <summary>
  63. /// 出提示框对象
  64. /// </summary>
  65. private GameObject mTipsObj = null;
  66. /// <summary>
  67. /// 出提示框的时间间隔
  68. /// </summary>
  69. private int mInterval = 0;
  70. /// <summary>
  71. /// 确定按钮
  72. /// </summary>
  73. private Button mButtonConfirm = null;
  74. /// <summary>
  75. /// 确定按钮上的文字
  76. /// </summary>
  77. private Text mConfirmText = null;
  78. /// <summary>
  79. /// 取消按钮
  80. /// </summary>
  81. private Button mButtonCancel = null;
  82. /// <summary>
  83. /// 取消按钮上的文字
  84. /// </summary>
  85. private Text mCancelText = null;
  86. /// <summary>
  87. /// 标题
  88. /// </summary>
  89. private Text mTitle = null;
  90. /// <summary>
  91. /// 内容
  92. /// </summary>
  93. private Text mContent = null;
  94. /// <summary>
  95. /// 提示内容
  96. /// </summary>
  97. private Text mTipsContent = null;
  98. /// <summary>
  99. /// 确定回调
  100. /// </summary>
  101. private Action mAc_Confirm = null;
  102. /// <summary>
  103. /// 取消回调
  104. /// </summary>
  105. private Action mAc_Cancel = null;
  106. /// <summary>
  107. /// 按钮布局:左
  108. /// </summary>
  109. private Transform mLeft = null;
  110. /// <summary>
  111. /// 按钮布局:中
  112. /// </summary>
  113. private Transform mMid = null;
  114. /// <summary>
  115. /// 按钮布局:右
  116. /// </summary>
  117. private Transform mRight = null;
  118. /// <summary>
  119. /// 获取单键
  120. /// </summary>
  121. /// <returns> 返回提示框实例 </returns>
  122. public static UI_CueDialog Instance()
  123. {
  124. return mInstance;
  125. }
  126. /// <summary>
  127. /// 初始化
  128. /// </summary>
  129. private void Awake()
  130. {
  131. mInstance = this;
  132. mCueObj = transform.Find("GameObject").gameObject;
  133. mBG = transform.Find("GameObject/Panel/bg").gameObject;
  134. mTipsObj = transform.Find("TipsPanel").gameObject;
  135. mTitle = transform.Find("GameObject/Panel/title").gameObject.GetComponent<Text>();
  136. mContent = transform.Find("GameObject/Panel/content").gameObject.GetComponent<Text>();
  137. mTipsContent = transform.Find("TipsPanel/Panel/ContentText").gameObject.GetComponent<Text>();
  138. mButtonConfirm = transform.Find("GameObject/Panel/ButtonConfirm").GetComponent<Button>();
  139. EventTriggerListenerDialog.Get(mButtonConfirm.gameObject).onClick = ((_btn) =>
  140. {
  141. OkEvent();
  142. });
  143. mConfirmText = transform.Find("GameObject/Panel/ButtonConfirm/Text").gameObject.GetComponent<Text>();
  144. mButtonCancel = transform.Find("GameObject/Panel/ButtonCancel").GetComponent<Button>();
  145. EventTriggerListenerDialog.Get(mButtonCancel.gameObject).onClick = ((_btn) =>
  146. {
  147. CancelEvent();
  148. });
  149. mCancelText = transform.Find("GameObject/Panel/ButtonCancel/Text").gameObject.GetComponent<Text>();
  150. mLeft = transform.Find("GameObject/Panel/left");
  151. mMid = transform.Find("GameObject/Panel/mid");
  152. mRight = transform.Find("GameObject/Panel/right");
  153. //mTitle.font = FontManager.Instance().CurFont;
  154. //mContent.font = FontManager.Instance().CurFont;
  155. //mConfirmText.font = FontManager.Instance().CurFont;
  156. //mCancelText.font = FontManager.Instance().CurFont;
  157. }
  158. /// <summary>
  159. /// 如果当前有打开的对话框,替换文字显示。(应用场景有限)
  160. /// </summary>
  161. /// <param name="strContent"></param>
  162. /// <param name="strTitle"></param>
  163. /// <param name="type"></param>
  164. /// <param name="okFun"></param>
  165. /// <param name="cancelFun"></param>
  166. /// <param name="leftBtnName"></param>
  167. /// <param name="rightBtnName"></param>
  168. public void OpenAndReplace(string strContent, string strTitle = "", E_DialogType type = E_DialogType.OneButton, Action okFun = null, Action cancelFun = null, string leftBtnName = "确定", string rightBtnName = "取消")
  169. {
  170. Canvas canvas = this.gameObject.GetComponent<Canvas>();
  171. if (canvas.worldCamera == null)
  172. {
  173. SetFont(this.gameObject);
  174. SetCamera(this.gameObject, string.Empty);
  175. }
  176. mButtonConfirm.gameObject.SetActive(true);
  177. mCueObj.SetActive(true);
  178. mBG.SetActive(true); // 开启背景,遮挡其他 UI
  179. // 设置标题和内容
  180. mTitle.text = strTitle;
  181. mContent.text = strContent;
  182. // 设置按钮上的文字
  183. mConfirmText.text = leftBtnName;
  184. mCancelText.text = rightBtnName;
  185. // 记录回调
  186. mAc_Confirm = okFun;
  187. mAc_Cancel = cancelFun;
  188. // 显示一个按钮
  189. if (E_DialogType.OneButton == type)
  190. {
  191. mButtonConfirm.transform.localPosition = mMid.localPosition;
  192. mButtonCancel.gameObject.SetActive(false);
  193. // 点击空白处等同于点中间按钮
  194. EventTriggerListenerDialog.Get(mBG).onClick = ((_btn) =>
  195. {
  196. OkEvent();
  197. });
  198. }
  199. else if (E_DialogType.TwoBntton == type)
  200. {
  201. // 显示两个按钮
  202. mButtonConfirm.transform.localPosition = mLeft.localPosition;
  203. mButtonCancel.gameObject.SetActive(true);
  204. mButtonCancel.transform.localPosition = mRight.localPosition;
  205. // 点空白处无响应
  206. mBG.GetComponent<Button>().onClick = null;
  207. }
  208. }
  209. /// <summary>
  210. /// 打开提示框(同时只会存在一个)
  211. /// </summary>
  212. /// <param name="strContent"> 文字内容 </param>
  213. /// <param name="strTitle"> 文字标题 </param>
  214. /// <param name="type"> 按钮布局类型 </param>
  215. /// <param name="okFun"> 确定回调 </param>
  216. /// <param name="cancelFun"> 取消回调 </param>
  217. /// <param name="leftBtnName"> 左按钮上的文字 </param>
  218. /// <param name="rightBtnName"> 右按钮上的文字 </param>
  219. public void Open(string strContent, string strTitle = "", E_DialogType type = E_DialogType.OneButton, Action okFun = null, Action cancelFun = null, string leftBtnName = "确定", string rightBtnName = "取消")
  220. {
  221. // Debug.LogError(" 打开提示框(同时只会存在一个)");
  222. if (!mCueObj.activeSelf)
  223. {
  224. Canvas canvas = this.gameObject.GetComponent<Canvas>();
  225. if (canvas.worldCamera == null)
  226. {
  227. SetFont(this.gameObject);
  228. SetCamera(this.gameObject, string.Empty);
  229. }
  230. mButtonConfirm.gameObject.SetActive(true);
  231. mCueObj.SetActive(true);
  232. mBG.SetActive(true); // 开启背景,遮挡其他 UI
  233. // 设置标题和内容
  234. mTitle.text = strTitle;
  235. mContent.text = strContent;
  236. // 设置按钮上的文字
  237. mConfirmText.text = leftBtnName;
  238. mCancelText.text = rightBtnName;
  239. // 记录回调
  240. mAc_Confirm = okFun;
  241. mAc_Cancel = cancelFun;
  242. // 显示一个按钮
  243. if (E_DialogType.OneButton == type)
  244. {
  245. mButtonConfirm.transform.localPosition = mMid.localPosition;
  246. mButtonCancel.gameObject.SetActive(false);
  247. // 点击空白处等同于点中间按钮
  248. EventTriggerListenerDialog.Get(mBG).onClick = ((_btn) =>
  249. {
  250. OkEvent();
  251. });
  252. }
  253. else if (E_DialogType.TwoBntton == type)
  254. {
  255. // 显示两个按钮
  256. mButtonConfirm.transform.localPosition = mLeft.localPosition;
  257. mButtonCancel.gameObject.SetActive(true);
  258. mButtonCancel.transform.localPosition = mRight.localPosition;
  259. // 点空白处无响应
  260. mBG.GetComponent<Button>().onClick = null;
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// 打开提示框ios占用框(不能用用户自行关闭)
  266. /// </summary>
  267. /// <param name="strContent"> 文字内容 </param>
  268. /// <param name="strTitle"> 文字标题 </param>
  269. /// <param name="type"> 按钮布局类型 </param>
  270. /// <param name="okFun"> 确定回调 </param>
  271. /// <param name="cancelFun"> 取消回调 </param>
  272. /// <param name="leftBtnName"> 左按钮上的文字 </param>
  273. /// <param name="rightBtnName"> 右按钮上的文字 </param>
  274. public void OpenIOSTIPS(string strContent, string strTitle = "")
  275. {
  276. // Debug.LogError(" 打开ios提示框(用户无法自行关闭)");
  277. Canvas canvas = this.gameObject.GetComponent<Canvas>();
  278. if (canvas.worldCamera == null)
  279. {
  280. SetFont(this.gameObject);
  281. SetCamera(this.gameObject, string.Empty);
  282. }
  283. mCueObj.SetActive(true);
  284. mBG.SetActive(true); // 开启背景,遮挡其他 UI
  285. // 设置标题和内容
  286. mTitle.text = strTitle;
  287. mContent.text = strContent;
  288. mButtonCancel.gameObject.SetActive(false);
  289. mButtonConfirm.gameObject.SetActive(false);
  290. }
  291. /// <summary>
  292. /// 打开提示栏不能遮挡后面ui,3秒后自动消失
  293. /// </summary>
  294. /// <param name="strContent"></param>
  295. public void OpenTips(string strContent)
  296. {
  297. mInterval = 0;
  298. mTipsObj.SetActive(false);
  299. mTipsObj.SetActive(true);
  300. mTipsContent.text = strContent;
  301. // Invoke("AutoClose",3f);
  302. }
  303. /// <summary>
  304. /// 设置 UI 的字体
  305. /// </summary>
  306. /// <param name="uiObj"> UI对象 </param>
  307. public void SetFont(GameObject uiObj)
  308. {
  309. if (null == uiObj)
  310. {
  311. Debug.LogError("PanelHelper 设置字体,UI 为空,中断");
  312. return;
  313. }
  314. // 获取所有的 Text
  315. Text[] allText = uiObj.GetComponentsInChildren<Text>(true);
  316. // 设置字体
  317. //for (int i = 0; i < allText.Length; i++)
  318. //{
  319. // allText[i].font = FontManager.Instance().CurFont;
  320. // allText[i].fontStyle = FontStyle.Normal;
  321. //}
  322. }
  323. /// <summary>
  324. /// 设置 UI 的摄像机
  325. /// </summary>
  326. /// <param name="uiObj"> UI对象 </param>
  327. /// <param name="camPath">指定相机位置</param>
  328. public void SetCamera(GameObject uiObj, string camPath)
  329. {
  330. return;
  331. if (null == uiObj)
  332. {
  333. Debug.LogError("PanelHelper 设置摄像机,UI 为空,中断");
  334. return;
  335. }
  336. GameObject cameraObj = null;
  337. if (camPath.Trim().Length > 0)
  338. {
  339. cameraObj = GameObject.Find(camPath);
  340. }
  341. else
  342. {
  343. cameraObj = GameObject.Find("/UICamera");
  344. }
  345. if (null == cameraObj)
  346. {
  347. Debug.LogError("PanelHelper 设置摄像机,找不到摄像机,中断");
  348. return;
  349. }
  350. Canvas canvas = uiObj.GetComponent<Canvas>();
  351. if (null == canvas)
  352. {
  353. Debug.LogError("PanelHelper 设置摄像机,获取画布组件失败,中断");
  354. return;
  355. }
  356. canvas.worldCamera = cameraObj.GetComponent<Camera>();
  357. }
  358. /// <summary>
  359. /// 用于网络异常提示
  360. /// </summary>
  361. /// <param name="strContent"> 文字字符串 </param>
  362. /// <param name="level"> 提示级别 </param>
  363. public void NetCue(string strContent, E_CueLevel level = E_CueLevel.Low)
  364. {
  365. // 关闭网络 Loading ,暂时屏蔽
  366. ////Loading.Instance().CloseNetLoading();
  367. switch (level)
  368. {
  369. case E_CueLevel.Low:
  370. UI_CueDialog.Instance().Open(strContent);
  371. break;
  372. case E_CueLevel.Mid:
  373. UI_CueDialog.Instance().Open(strContent, string.Empty, E_DialogType.TwoBntton, ReUpdate, ExitGame, "重试", "退出游戏");
  374. break;
  375. case E_CueLevel.High:
  376. UI_CueDialog.Instance().Open(strContent, string.Empty, E_DialogType.TwoBntton, ReLogin, ExitGame, "返回登陆", "退出游戏");
  377. break;
  378. case E_CueLevel.Super:
  379. UI_CueDialog.Instance().Open(strContent, string.Empty, E_DialogType.TwoBntton, ReUpdate, ExitGame, "重新链接", "退出游戏");
  380. break;
  381. default:
  382. UI_CueDialog.Instance().Open("客户端异常", string.Empty, E_DialogType.OneButton, ExitGame);
  383. break;
  384. }
  385. }
  386. public void OnDestroy()
  387. {
  388. Debug.Log("UI_CueDialog 被销毁!");
  389. }
  390. /// <summary>
  391. /// 退出游戏提示
  392. /// </summary>
  393. public void ExitGameCue()
  394. {
  395. UI_CueDialog.Instance().Open("您确定要退出游戏?", "退出", E_DialogType.TwoBntton, ExitGame);
  396. }
  397. private void ExitCalback(LoyalSoftSDK.LoyalGameCallbackData data)
  398. {
  399. Debug.Log("退出时输出:" + data.action + " " + data.msg + " " + data.data);
  400. if (data.action.Equals("0"))
  401. {
  402. // GameInfo.GetPlayerInfo("exitServer");
  403. ExitGame();
  404. }
  405. else if (data.action.Equals("999"))
  406. {
  407. ExitGameCue();
  408. }
  409. }
  410. /// <summary>
  411. /// 更新
  412. /// </summary>
  413. private void Update()
  414. {
  415. if (Input.GetKeyUp(KeyCode.Escape))
  416. {
  417. if (Application.platform == RuntimePlatform.Android && GlobalConfig.is_SDK) ////如果在接入sdk的情况下调用sdk接口
  418. {
  419. //LoyalSoftSDK.LoyalGameSDK.Instance.ExitGame(GameInfo.GetPlayerInfo("exitServer"));
  420. //if (!LoyalSoftSDK.LoyalGameSDK.Instance.SDKExit(GameInfo.GetPlayerInfo("exitServer")))
  421. //{
  422. // ExitGameCue();
  423. //}
  424. //LoyalSoftSDK.LoyalGameSDK.Instance.OnSDKExitCallback = delegate(LoyalSoftSDK.LoyalGameCallbackData data)
  425. //{
  426. // ExitCalback(data);
  427. //};
  428. // 退出
  429. if (AndroidInteractive.CallJavaFunction("OnExitGame") == false)
  430. {
  431. ExitGameCue();
  432. }
  433. }
  434. else
  435. {
  436. ExitGameCue();
  437. }
  438. }
  439. if (mInterval > 150)
  440. {
  441. mTipsObj.SetActive(false);
  442. }
  443. else
  444. {
  445. mInterval++;
  446. }
  447. }
  448. /// <summary>
  449. /// 重新更新
  450. /// </summary>
  451. private void ReUpdate()
  452. {
  453. ////LevelManager.Instance().LoadLevel(E_LevelInfo.ResUpdate);
  454. }
  455. /// <summary>
  456. /// 重新登陆
  457. /// </summary>
  458. private void ReLogin()
  459. {
  460. ////LevelManager.Instance().LoadLevel(E_LevelInfo.Login);
  461. }
  462. /// <summary>
  463. /// 退出游戏
  464. /// </summary>
  465. private void ExitGame()
  466. {
  467. // LoyalSoft. LoyalGameSDK.Instance.FoceExit();
  468. Application.Quit();
  469. }
  470. /// <summary>
  471. /// 确定事件
  472. /// </summary>
  473. /// <param name="obj"> 按钮对象 </param>
  474. private void OkEvent()
  475. {
  476. ////AudioManager.Instance.PlaySound(SoundName.normal);
  477. //// 先关闭提示窗口,再执行回调,就可以连续打开 CueDialog
  478. Close();
  479. if (null != mAc_Confirm)
  480. {
  481. mAc_Confirm();
  482. }
  483. }
  484. /// <summary>
  485. /// 取消事件
  486. /// </summary>
  487. /// <param name="obj"> 按钮对象 </param>
  488. private void CancelEvent()
  489. {
  490. ////AudioManager.Instance.PlaySound(SoundName.normal);
  491. //// 先关闭提示窗口,再执行回调,就可以连续打开 CueDialog
  492. Close();
  493. if (null != mAc_Cancel)
  494. {
  495. mAc_Cancel();
  496. }
  497. }
  498. /// <summary>
  499. /// 关闭本窗口
  500. /// </summary>
  501. public void Close()
  502. {
  503. mCueObj.SetActive(false);
  504. mBG.SetActive(false);
  505. }
  506. /// <summary>
  507. /// 关闭提示窗口
  508. /// </summary>
  509. public void TipsClose()
  510. {
  511. mTipsObj.SetActive(false);
  512. }
  513. }