[英]Why is unity OnApplicationPause called twice on android?
I have a simple game object (the main UI object) with OnApplicationPause
that prints out the Pause
status (true/false)
in the debug log.我有一个带有
OnApplicationPause
的简单游戏对象(主 UI 对象),它在调试日志中打印出Pause
状态(true/false)
。 When I run
it on android, and press the Home Button
to leave the app, I see that the OnApplicationPause()
is called twice
in a row, with the same pauseStatus
(true).当我在 android 上
run
它并按下主页Button
离开应用程序时,我看到OnApplicationPause()
被连续调用twice
,具有相同的pauseStatus
(true)。 The same happens when I reopen the app: the OnApplicationPause
is called twice with Pause
status false.当我重新打开应用程序时
OnApplicationPause
发生同样的情况: OnApplicationPause
被调用两次, Pause
状态为 false。
Why is it being called twice and can I avoid it?为什么它被调用两次,我可以避免它吗? Am I doing anything wrong?
我做错了什么吗?
I also tried to create a separate GameObject, that includes the same debug code.我还尝试创建一个单独的 GameObject,其中包含相同的调试代码。 The issue doesn't occur there - only in my main UI object.
问题不会发生在那里 - 仅在我的主 UI 对象中。
Are you 100% sure that your script containing the OnApplicationPause()
method is not present twice in the Scene when Pause occurs?您是否 100% 确定包含
OnApplicationPause()
方法的脚本在发生暂停时不会在场景中出现两次? or present on a previous Scene on a DontDestroyOnLoad
GameObject?或出现在
DontDestroyOnLoad
游戏对象的前一个场景中?
OnApplicationPause is called on each active MonoBehaviour
when pause occurs.发生暂停时,会在每个活动的
MonoBehaviour
上调用OnApplicationPause 。
So having two Debug.Log
means two scripts instances running if you don't call it manually yourself.因此,如果您不自己手动调用它,那么拥有两个
Debug.Log
意味着运行两个脚本实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.