y中检测到applicationDidBecomeActive时

y中检测到applicationDidBecomeActive时

本文介绍了在Unity中检测到applicationDidBecomeActive时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试检测应用程序在进入后台模式后何时再次激活,我们希望能够在发生这种情况时触发一种方法.这个想法很简单:甚至有一个MonoBehavour方法OnApplicationPause,可在应用程序进入后台模式时使用,但不适用于"OnApplicationResume".

We are trying to detect when the app becomes active again after being in background mode, we want to be able to fire a method when this occurs. It's such a simple idea: there's even a MonoBehavour method OnApplicationPause which can be used as the app enters background mode, but non for "OnApplicationResume".

推荐答案

只需尝试 OnApplicationFocus ,效果很好.

bool initialFocus;


void OnApplicationFocus(bool focusStatus)
{
    if (focusStatus)
    {
        //App became active, will fire on application first focus
    }

    if (focusStatus && initialFocus)
    {
        //App became active, after it's been inactive at least once
    }

    initialFocus = initialFocus || focusStatus;
}

此外,在您的问题中,您询问的是OnApplicationPause

Further, in your question you ask about OnApplicationPause

请注意,实际上OnApplicationPause以相同的方式检测暂停和取消暂停-它会以布尔值作为参数.

Note that in fact OnApplicationPause detects both pause and unpause in the same way - it gets a bool as an argument.

  void OnApplicationPause(bool p)
  {
      Debug.Log("The app just " + (p ? "paused" : "unpaused"));
  }

还要注意,OnApplicationQuit在移动平台上毫无意义.

Note too that OnApplicationQuit is meaningless on the mobile platforms.

这篇关于在Unity中检测到applicationDidBecomeActive时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 15:03