[Unity冷知识] 手机平台切换后台或者其他操作导致抬起事件监听不到问题

问题描述:

我们经常遇到问题,在手机平台切换后台或者其他什么操作,导致Unity进程的抬起事件监听不到,造成比如滑条乱跑,按住特效不消失,按钮一直处于按下状态等等问题。

解决思路:

参考文章:https://www.jianshu.com/p/3a7989d1ff26

Unity有两个接口函数

OnApplicationPause

OnApplicationFocus

官方解释:

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 OnApplicationFocuscall 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.

OnApplicationFocus can be a co-routine; to do this, use the yield statement in the function. Implemented this way, it is evaluated twice during the initial frame: first as an early notification, and secondly during the normal co-routine update step.

On Android, when the on-screen keyboard is enabled, it causes an OnApplicationFocus( false ) event. Additionally, if you press Home at the moment the keyboard is enabled, the OnApplicationFocus() event is not called, butOnApplicationPause() is called instead.

简单翻译:

PC上 Alt-tabbing 或者 Cmd-tabbing应用切换后台或者切回来时时,OnApplicationFocus 会被调用

Andriod上输入法出来的时候,OnApplicationFocus会调用,按下Home切换后台时,OnApplicationFocus不会调用,反而调用的是butOnApplicationPause

你可能感兴趣的:(unity)