| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- //持久化的处理脚本,在app启动时就应开始常驻
- public class PersistenHandler : MonoBehaviour
- {
- public MenuBackController menuBackCtr = new MenuBackController();
- public static PersistenHandler ins;
- public static void Init() {
- if (ins) return;
- GameObject obj = new GameObject("PersistenHandler");
- ins = obj.AddComponent<PersistenHandler>();
- DontDestroyOnLoad(obj);
- }
- void Update()
- {
- menuBackCtr.Update();
- }
- }
- public class MenuBackController {
- static List<string> NeedBackGameScenes = new List<string>(new string[] { "DuckHunter", "WildAttack", "FruitMaster" });
- public List<MenuBackInterface> views = new List<MenuBackInterface>();
- long lastPressExitTime = 0;
- public void Update()
- {
- if (Input.GetKeyDown(KeyCode.Escape) || CommonConfig.StandaloneMode && Input.GetKeyDown(KeyCode.Space))
- {
- HandleKeyDownEscape();
- }
- if (CommonConfig.StandaloneMode)
- {
- //暴力取消导航
- foreach (var item in Object.FindObjectsOfType<Button>())
- {
- var nv = item.navigation;
- nv.mode = Navigation.Mode.None;
- item.navigation = nv;
- }
- }
- }
- public void HandleKeyDownEscape()
- {
- long lastLast = lastPressExitTime;
- lastPressExitTime = JCUnityLib.TimeUtils.GetTimestamp();
- if (lastPressExitTime - lastLast < 10 * 1000 && !CommonConfig.StandaloneMode)
- {
- OnTwiceBack();
- }
- else
- {
- if (CommonConfig.StandaloneMode) OnOnceBackStandAlone();
- else OnOnceBack();
- }
- }
- private void BanTwice() {
- lastPressExitTime = 0;
- }
- public void OnOnceBack() {
- var sceneName = SceneManager.GetActiveScene().name;
- if (sceneName == "Entry") {
- BanTwice();
- Debug.Log("菜单退出: " + sceneName);
- Application.Quit();
- return;
- }
- if (sceneName.StartsWith("Game") || NeedBackGameScenes.Contains(sceneName)) {
- BanTwice();
- Debug.Log("菜单返回主页");
- SceneManager.LoadScene("Home", LoadSceneMode.Single);
- return;
- }
- if (views.Count > 0) {
- BanTwice();
- MenuBackInterface i = views[views.Count - 1];
- if (i.OnMenuBack()) {
- views.Remove(i);
- Debug.Log("菜单返回成功: " + i);
- } else {
- Debug.Log("菜单返回失败: " + i);
- }
- return;
- }
- if (!CommonConfig.StandaloneMode)
- {
- Debug.Log("菜单退出APP询问");
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("tip_phone-menu-back_quit-app"));
- }
- }
- public void OnOnceBackStandAlone()
- {
- var sceneName = SceneManager.GetActiveScene().name;
- if (sceneName == "Entry")
- {
- BanTwice();
- Debug.Log("菜单退出: " + sceneName);
- Application.Quit();
- return;
- }
- if (sceneName.StartsWith("Game") || NeedBackGameScenes.Contains(sceneName))
- {
- BanTwice();
- Debug.Log("菜单返回主页");
- SceneManager.LoadScene("Home", LoadSceneMode.Single);
- return;
- }
- if (views.Count > 0)
- {
- BanTwice();
- MenuBackInterface i = views[views.Count - 1];
- if (i.GetType().Name == "GameStartView") return;
- if (i.OnMenuBack())
- {
- views.Remove(i);
- Debug.Log("菜单返回成功: " + i);
- }
- else
- {
- Debug.Log("菜单返回失败: " + i);
- }
- return;
- }
- if (!CommonConfig.StandaloneMode)
- {
- Debug.Log("菜单退出APP询问");
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("tip_phone-menu-back_quit-app"));
- }
- }
- private void OnTwiceBack() {
- Debug.Log("菜单退出APP");
- Application.Quit();
- }
- }
- public interface MenuBackInterface {
- public bool OnMenuBack();
- }
|