繁体   English   中英

如何在后台运行 Unity 程序?

[英]How to run Unity program in background?

我开发了一个统一的 android 应用程序。 我想在应用程序处于前台和后台时执行一些操作。 现在我正试图以递归方式在几个时间间隔内打印日志。 我正在使用这种方法来调用我的倒数计时器。

void OnApplicationFocus(bool hasFocus){
        StartBattle();
        isPaused = !hasFocus;
        Debug.Log("isPaused " + isPaused);
    }

void OnApplicationPause(bool pauseStatus){
      isPaused = pauseStatus;
      StartBattle();
     }

并且这种方法以递归方式打印数据。

public void StartBattle(){
        StartCoroutine(BattleRecursive(0));
    }

public IEnumerator BattleRecursive(int depth){
        // Start Coroutine"

        yield return new WaitForSeconds(2f);

        if (depth == 0)
            yield return StartCoroutine(BattleRecursive(depth + 1));

        if (depth == 1)
            yield return StartCoroutine(BattleRecursive(depth + 1));

        Debug.Log("MyCoroutine is now finished at depth " + depth);
    }

当应用程序在前台时,日志打印得很好,但是当应用程序在后台时,它不打印任何东西。

当 Unity 退出时,您无法在后台执行 Unity C# 代码。 您要在后台运行的这段代码必须是用 Java 编写的。

用Java而不是C#编写要执行的代码,然后使用Android的Intent启动服务。 这要求您将当前 Unity 的ContextActivity发送到您的插件以启动服务。 您可以在此处此处找到有关如何执行操作的示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM