如何实现unity在后台运行

Posted

tags:

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

参考技术A 在 Build Settings 面板里有 Run in Background 的选项,选上了游戏才会在后台运行,否则游戏窗口转到后台时是不更新游戏逻辑的。
在程序中用 Application.runInBackground (bool 类型)来判断是否在后台运行。

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设备上后台运行问题

unity发布的android游戏有办法让它在后台运行时主线程不停止吗

Unity3D 灵巧小知识点 ☀️ | 让Unity支持后台运行

Unity3D用继承EditorUpdater类来实现Editor模式下的后台处理

Unity3D Application模拟进入前后台及暂停

如何在 iOS 10 的后台运行 xmpp?