PersistenHandler.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. //持久化的处理脚本,在app启动时就应开始常驻
  6. public class PersistenHandler : MonoBehaviour
  7. {
  8. public MenuBackController menuBackCtr = new MenuBackController();
  9. public static PersistenHandler ins;
  10. public static void Init() {
  11. if (ins) return;
  12. GameObject obj = new GameObject("PersistenHandler");
  13. ins = obj.AddComponent<PersistenHandler>();
  14. DontDestroyOnLoad(obj);
  15. }
  16. void Update()
  17. {
  18. menuBackCtr.Update();
  19. }
  20. }
  21. public class MenuBackController {
  22. public List<MenuBackInterface> views = new List<MenuBackInterface>();
  23. long lastPressExitTime = 0;
  24. public void Update()
  25. {
  26. if (Input.GetKeyDown(KeyCode.Escape)) {
  27. long lastLast = lastPressExitTime;
  28. lastPressExitTime = JC.CS.Utility.GetTimestamp();
  29. if (lastPressExitTime - lastLast < 10 * 1000) {
  30. OnTwiceBack();
  31. } else {
  32. OnOnceBack();
  33. }
  34. }
  35. }
  36. public void BanTwice() {
  37. lastPressExitTime = 0;
  38. }
  39. public void OnOnceBack() {
  40. var sceneName = SceneManager.GetActiveScene().name;
  41. if (sceneName == "Entry") {
  42. BanTwice();
  43. Debug.Log("菜单退出: " + sceneName);
  44. Application.Quit();
  45. return;
  46. }
  47. if (sceneName.StartsWith("Game")) {
  48. BanTwice();
  49. Debug.Log("菜单返回主页");
  50. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  51. return;
  52. }
  53. if (views.Count > 0) {
  54. BanTwice();
  55. MenuBackInterface i = views[views.Count - 1];
  56. if (i.OnMenuBack()) {
  57. views.Remove(i);
  58. Debug.Log("菜单返回成功: " + i);
  59. } else {
  60. Debug.Log("菜单返回失败: " + i);
  61. }
  62. return;
  63. }
  64. Debug.Log("菜单退出APP询问");
  65. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("tip_phone-menu-back_quit-app"));
  66. }
  67. public void OnTwiceBack() {
  68. Debug.Log("菜单退出APP");
  69. Application.Quit();
  70. }
  71. }
  72. public interface MenuBackInterface {
  73. public bool OnMenuBack();
  74. }