模拟进入多个次级界面后的返回逻辑,主要是利用Stack<T>();
ps:MonoSingleton与CrudeElapsedTimer请搜索博客文章
public class AppMain : MonoSingleton<AppMain> {
public delegate void OnBackDelegate();
Stack<OnBackDelegate> _OnBackStack = new Stack<OnBackDelegate>();
CrudeElapsedTimer _KeyTimer = new CrudeElapsedTimer(0.5f);
protected void Update()
{
_KeyTimer.Advance(Time.deltaTime);
if (Input.GetKey(KeyCode.Escape))//PC端点击esc,手机端则是返回键
{
if (_KeyTimer.TimeOutCount > 0)
{
OnBack();
_KeyTimer.Reset();
}
}
}
public void PushOnBack(OnBackDelegate cb)
{
_OnBackStack.Push(cb); //向 Stack 的顶部添加一个对象
}
public void PopOnBack()
{
if(_OnBackStack.Count > 0)
{
_OnBackStack.Pop();//移除并返回在 Stack 的顶部的对象
}