管理手机返回键功能的系统,在跳转页面的时候注册返回事件为关关闭自身,自身关闭或回调生效时注销事件,还包括左右滑动事件
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();}
}
}
}
}
}
原文地址:https://blog.csdn.net/qq_39332021/article/details/113780806
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。