管理手机返回键功能的系统,在跳转页面的时候注册返回事件为关关闭自身,自身关闭或回调生效时注销事件,还包括左右滑动事件
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetMobileKeyManager : MonoBehaviour
{
private float clickTime;
public static GetMobileKeyManager Main;
private Vector2 clockPos;
private void Awake()
{
Main = this;
DontDestroyOnLoad(gameObject);
LeftSlide = () => {};
RightSlide = () => {};
}
public Action LeftSlide,RightSlide;
public bool leftSliderActive = true, rightSliderActive = true;
private Dictionary<string, Action> BackKeyDownEvents = new Dictionary<string, Action>();
private List<string> EventsName = new List<string>();
public void AddBackKeyEvent(string eventName,Action Event)
{
if(EventsName.Contains(eventName)){return;}
EventsName.Add(eventName);
BackKeyDownEvents.Add(eventName,Event);
}
public void RemoveBackKeyEvent(string eventName)
{
if(!EventsName.Contains(eventName)){return;}
BackKeyDownEvents.Remove(eventName);
EventsName.Remove(eventName);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if(EventsName.Count==0){return;}
Debug.Log(EventsName[EventsName.Count - 1]);
BackKeyDownEvents[EventsName[EventsName.Count - 1]]();
// RemoveBackKeyEvent(EventsName[EventsName.Count - 1]);
}
if (GameManager.Main.GetClick())
{
clockPos = GameManager.Main.GetPos();
clickTime = Time.time;
}
if (GameManager.Main.GetClickUp())
{
if (Time.time - clickTime < 1 && GameManager.Main.GetPos().y < 600 && clockPos.y < 600)
{
if (GameManager.Main.GetPos().x > clockPos.x+100)
{
if(leftSliderActive){LeftSlide();}
}
else if (GameManager.Main.GetPos().x < clockPos.x-100)
{
if(rightSliderActive){RightSlide();}
}
}
}
}
}