PersistenHandler.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. //持久化的处理脚本,在app启动时就应开始常驻
  7. public class PersistenHandler : MonoBehaviour
  8. {
  9. public MenuBackController menuBackCtr = new MenuBackController();
  10. public static PersistenHandler ins;
  11. public static void Init() {
  12. if (ins) return;
  13. GameObject obj = new GameObject("PersistenHandler");
  14. ins = obj.AddComponent<PersistenHandler>();
  15. DontDestroyOnLoad(obj);
  16. }
  17. void Update()
  18. {
  19. menuBackCtr.Update();
  20. }
  21. }
  22. public class MenuBackController {
  23. static List<string> NeedBackGameScenes = new List<string>(new string[] { "DuckHunter", "WildAttack", "FruitMaster" });
  24. public List<MenuBackInterface> views = new List<MenuBackInterface>();
  25. long lastPressExitTime = 0;
  26. public void Update()
  27. {
  28. if (Input.GetKeyDown(KeyCode.Escape) || CommonConfig.StandaloneMode && Input.GetKeyDown(KeyCode.Space))
  29. {
  30. HandleKeyDownEscape();
  31. }
  32. if (CommonConfig.StandaloneMode)
  33. {
  34. //暴力取消导航
  35. foreach (var item in Object.FindObjectsOfType<Button>())
  36. {
  37. var nv = item.navigation;
  38. nv.mode = Navigation.Mode.None;
  39. item.navigation = nv;
  40. }
  41. }
  42. }
  43. public void HandleKeyDownEscape()
  44. {
  45. long lastLast = lastPressExitTime;
  46. lastPressExitTime = JCUnityLib.TimeUtils.GetTimestamp();
  47. if (lastPressExitTime - lastLast < 10 * 1000 && !CommonConfig.StandaloneMode)
  48. {
  49. OnTwiceBack();
  50. }
  51. else
  52. {
  53. if (CommonConfig.StandaloneMode) OnOnceBackStandAlone();
  54. else OnOnceBack();
  55. }
  56. }
  57. private void BanTwice() {
  58. lastPressExitTime = 0;
  59. }
  60. public void OnOnceBack() {
  61. var sceneName = SceneManager.GetActiveScene().name;
  62. if (sceneName == "Entry") {
  63. BanTwice();
  64. Debug.Log("菜单退出: " + sceneName);
  65. Application.Quit();
  66. return;
  67. }
  68. if (sceneName.StartsWith("Game") || NeedBackGameScenes.Contains(sceneName)) {
  69. BanTwice();
  70. Debug.Log("菜单返回主页");
  71. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  72. return;
  73. }
  74. if (views.Count > 0) {
  75. BanTwice();
  76. MenuBackInterface i = views[views.Count - 1];
  77. if (i.OnMenuBack()) {
  78. views.Remove(i);
  79. Debug.Log("菜单返回成功: " + i);
  80. } else {
  81. Debug.Log("菜单返回失败: " + i);
  82. }
  83. return;
  84. }
  85. if (!CommonConfig.StandaloneMode)
  86. {
  87. Debug.Log("菜单退出APP询问");
  88. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("tip_phone-menu-back_quit-app"));
  89. }
  90. }
  91. public void OnOnceBackStandAlone()
  92. {
  93. var sceneName = SceneManager.GetActiveScene().name;
  94. if (sceneName == "Entry")
  95. {
  96. BanTwice();
  97. Debug.Log("菜单退出: " + sceneName);
  98. Application.Quit();
  99. return;
  100. }
  101. if (sceneName.StartsWith("Game") || NeedBackGameScenes.Contains(sceneName))
  102. {
  103. BanTwice();
  104. Debug.Log("菜单返回主页");
  105. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  106. return;
  107. }
  108. if (views.Count > 0)
  109. {
  110. BanTwice();
  111. MenuBackInterface i = views[views.Count - 1];
  112. if (i.GetType().Name == "GameStartView") return;
  113. if (i.OnMenuBack())
  114. {
  115. views.Remove(i);
  116. Debug.Log("菜单返回成功: " + i);
  117. }
  118. else
  119. {
  120. Debug.Log("菜单返回失败: " + i);
  121. }
  122. return;
  123. }
  124. if (!CommonConfig.StandaloneMode)
  125. {
  126. Debug.Log("菜单退出APP询问");
  127. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("tip_phone-menu-back_quit-app"));
  128. }
  129. }
  130. private void OnTwiceBack() {
  131. Debug.Log("菜单退出APP");
  132. Application.Quit();
  133. }
  134. }
  135. public interface MenuBackInterface {
  136. public bool OnMenuBack();
  137. }