| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- 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 {
- public List<MenuBackInterface> views = new List<MenuBackInterface>();
- long lastPressExitTime = 0;
- public void Update()
- {
- if (Input.GetKeyDown(KeyCode.Escape)) {
- long lastLast = lastPressExitTime;
- lastPressExitTime = JC.CS.Utility.GetTimestamp();
- if (lastPressExitTime - lastLast < 10 * 1000) {
- OnTwiceBack();
- } else {
- OnOnceBack();
- }
- }
- }
- public 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")) {
- 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;
- }
- Debug.Log("菜单退出APP询问");
- PopupMgr.ins.ShowTip("再按一次退出APP");
- }
- public void OnTwiceBack() {
- Debug.Log("菜单退出APP");
- Application.Quit();
- }
- }
- public interface MenuBackInterface {
- public bool OnMenuBack();
- }
|