简单的定时器

1.实现一个简单的功能,点击退出键,或者手机home键,出现一个提示标签,如果在两秒之内再次点击则退出游戏,2秒之后再次点击出现提示标签。

public class QuitGameHotFix : BasePanelCall
{
    private bool timer;

    private float _lastTime;
    private readonly float _waitTime = 2;
    private Transform _notice;

    public Transform notice;
    public override void Start(Transform t)
    {
        base.Start(t);
        _notice = t.Find("Notice");

    }

    public override void Update()
    {
        base.Update();

        if (timer)
        {
            _lastTime += Time.deltaTime;
            if (_lastTime > _waitTime)
            {
                timer = false;
            }
        }

        if (CheckIsQuitGame())
        {
            if (!timer)
            {
                timer = true;
                _lastTime = 0;
                ShowNotice();
            }
            else
            {
                Debug.Log("呼叫退出0000");
                Application.Quit();
            }
        }

    }

    private bool CheckIsQuitGame()
    {
        //Debug.Log("CheckIsQuitGame");

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Debug.Log("KeyCode.Escape");
            return true;
        }

/*        if (Application.platform == RuntimePlatform.Android && Input.GetKeyDown(KeyCode.Home))
        {
            return true;
        }*/

        return false;
    }


    private void ShowNotice()
    {
        if (notice == null)
        {
            notice = GameObject.Instantiate(_notice);
            Transform parent;
            if (SceneManager.GetActiveScene().name == SceneEnum.BattleScene.ToString())
            {
                parent = BattleUIHotFixManager.Instance.LegacyCanvas;
            }
            else if (SceneManager.GetActiveScene().name == SceneEnum.Chest.ToString())
            {
                parent = ChestManagerHotFix.Instance._bgCanvas;
            }
            else if (SceneManager.GetActiveScene().name == SceneEnum.MapScene.ToString())
            {
                parent = MapConfigManager.Instance.EqupimentCanvas.transform;
            }
            else if (SceneManager.GetActiveScene().name == SceneEnum.LoginScene.ToString())
            {
                parent = GameObject.Find("Canvas").transform;
            }
            else
            {
                parent = GameObject.Find("Canvas").transform;
            }

            notice.transform.SetParent(parent, false);
            notice.transform.localScale = Vector3.one;
            notice.transform.SetAsLastSibling();
            notice.transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 50);

            WaitForRunTool.Run(Destroy(notice.gameObject));
        }
    }


    private IEnumerator Destroy(GameObject go)
    {
        yield return new WaitForSeconds(2f);

        GameObject.Destroy(go);
    }

    public override void OnDestroy()
    {
        base.OnDestroy();

        _notice = null;
        notice = null;
    }
}

推荐阅读更多精彩内容

  • 我的实现方法是,创建一个按钮,并在按钮下方添加一个label,当点击按钮的时候,按钮隐藏,实时改变label的te...
    16哥哥阅读 436评论 0 0
  • 问题 做项目的时候经常会有这样的需求,在某个时刻开始执行某个任务,然后每隔一段时间都会执行该任务。 windows...
    Syfun阅读 10,912评论 0 52
  • 在项目开发中我们有的时候需要用到计时器,比如登录超时,scrollview的滚动等,那么就让我们自己手动的去创建一...
    Codepgq阅读 1,479评论 8 15
  • 经过第二课的深化,我更加确定自己能量晃荡最严重的卡点就是愿力部分的:不敢有钱。 小时候,爸妈做生意,赚很多钱,但家...
    曹燕六中阅读 214评论 2 0
  • 晨起感恩 感恩父母生养之恩,可以让我来到这么美好的时间,感受生活的心酸和幸福,感恩父亲对我无私的爱,感恩母亲什么都...
    黄巧珍阅读 70评论 0 1