目前在用tolua进行项目开发,C#与lua交互的部分消息使用了注册监听处理,监听方法均放到Update方法里面进行处理,问题在于失去焦点后Update方法将不继续执行,这就导致了监听方法未能及时调用。

例如:AB玩家进行纸牌游戏,A接到电话并失去焦点,Update不执行导致消息未能及时通知服务器,此时B未能及时收到A的状态误以为A还在打牌状态。

此外Android Plugins中的UnityPlayer.UnitySendMessage,经测试在强制暂停时,OnPause、OnStop周期中UnitySendMessage无效。(个人未测)

故要特殊处理这两个方法,现改成C#直接调用lua方法,去掉监听推送这一步。

C#:///<summary>///当程序获得或者是去焦点时///</summary>///<paramname="focus"></param>publicvoidOnApplicationFocus(boolfocus){try{LuaManager.CallFunc_Void("MineMessageCenter.OnApplicationFocus",focus);}catch(System.Exception){throw;}//Debug.logger.Log("焦点:"+focus);//if(focus)//{//MessageCenter.GetInstance().PostEvent(EventNameDefine.GetFocusEvent);//}//else//{//MessageCenter.GetInstance().PostEvent(EventNameDefine.LostFocusEvent);//}}///<summary>///当程序暂停///</summary>///<paramname="focus"></param>publicvoidOnApplicationPause(boolisPause){try{LuaManager.CallFunc_Void("MineMessageCenter.OnApplicationPause",isPause);}catch(System.Exception){throw;}//Debug.logger.Log("暂停:"+isPause);//if(isPause)//{//MessageCenter.GetInstance().PostEvent(EventNameDefine.GetPauseEvent);//}//else//{//MessageCenter.GetInstance().PostEvent(EventNameDefine.LostPauseEvent);//}}

Lua:--当程序获得或者是去焦点时functionMineMessageCenter.OnApplicationFocus(focus)print("焦点:"..tostring(focus));end--当程序暂停functionMineMessageCenter.OnApplicationPause(isPause)--print("暂停:"..tostring(isPause));end