unity 后台计时器实现

Posted 柯腾_wjf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity 后台计时器实现相关的知识,希望对你有一定的参考价值。

问题:最近在做一个倒计时的功能,发现倒计时在程序切换到后台是会停止计时,发现unity 安卓后台运行时,计时器不在运行,再次切回App时从切到后台的时间点计时,例如切到后台时位10,后台切回app还是从10开始

,原因是unity在安卓后台环境下,Update和协程不会执行。
解决:
1.在Update和协程中,计算计时器的时间用当前的Time.realtimeSinceStartup减去上一次计时的Time.realtimeSinceStartup,而不是直接减去固定数值,例如减去1
2.使用unity自带的方法void OnApplicationFocus(bool hasFocus),该方法会在App进入后台和进入程序的时候调用,当进入程序时hasFocus = true,否则hasFocus=false;(参考Start()方法)

3.使用unity自带的方法

void OnApplicationPause(bool pauseStatus)

该方法会在App暂停和停止暂停的时候调用,当暂停时pauseStatus= true,否则pauseStatus=false;

OnApplicationFocus is called when the application loses or gains focus. Alt-tabbing or Cmd-tabbing can take focus away from the Unity application to another desktop application. This causes the GameObjects to receive an OnApplicationFocus call with the argument set to false. When the user switches back to the Unity application, the GameObjects receive an OnApplicationFocus call with the argument set to true.
hasFocus True if the GameObjects have focus, else False.

 

代码如下:只需要监听OnApplicationEnterFocus和OnApplicationQuitFocus事件即可,FocusContinueTime为进入后台的总时间,计算倒计时时只要减掉这个数就可以了

public static float FocusContinueTime
        {
            get
            {
                return focusContinueTime;
            }
        }

        public static float PauseContinueTime
        {
            get
            {
                return pauseContinueTime;
            }
        }
        #endregion

        private static float preFocusTime = 0;
        private static float prePauseTime = 0;
        private static float focusContinueTime = 0;
        private static float pauseContinueTime = 0;

        void Awake()
        {
            this.gameObject.name = "ApplicationEvent";
            prePauseTime = Time.realtimeSinceStartup;
            preFocusTime = Time.realtimeSinceStartup;
            DontDestroyOnLoad(this);
        }
        public void Init()
        {
        }
        public void Release()
        {
        }
        void OnApplicationFocus(bool hasFocus)
        {
            if (hasFocus)
            {
                focusContinueTime = Time.realtimeSinceStartup - preFocusTime;
                if (OnApplicationEnterFocus != null)
                {
                    OnApplicationEnterFocus(); 
                }                         
            }
            else
            {                
                preFocusTime = Time.realtimeSinceStartup;
                if (OnApplicationQuitFocus != null)
                {
                    OnApplicationQuitFocus();
                }     
            }
        }

        void OnApplicationPause(bool pauseStatus)
        {
            if (pauseStatus)
            {
                prePauseTime = Time.realtimeSinceStartup;
                if (OnApplicationEnterPause != null)
                {
                    OnApplicationEnterPause();
                }   
            }
            else
            {
                pauseContinueTime = Time.realtimeSinceStartup - prePauseTime;
                if (OnApplicationQuitPause != null)
                {
                    OnApplicationQuitPause();
                }                                          
            }
        }

 

以上是关于unity 后台计时器实现的主要内容,如果未能解决你的问题,请参考以下文章

Unity中三种计时器实现方式

iOS开发简单的实现后台任务(诸如后台播放音乐,定时器,后台定位等)

Unity3D日常开发提高开发效率之定时器系统实现

Unity3D日常开发提高开发效率之定时器系统实现

unity工具类篇 unity 计时器

unity工具类篇 unity 计时器