您的位置:首页 > 移动开发 > Unity3D

Unity3d 失去获取焦点,暂停

2017-10-28 18:07 405 查看
目前在用tolua进行项目开发,C#与lua交互的部分消息使用了注册监听处理,监听方法均放到Update方法里面进行处理,问题在于失去焦点后Update方法将不继续执行,这就导致了监听方法未能及时调用。
例如:AB玩家进行纸牌游戏,A接到电话并失去焦点,Update不执行导致消息未能及时通知服务器,此时B未能及时收到A的状态误以为A还在打牌状态。
此外Android Plugins中的UnityPlayer.UnitySendMessage,经测试在强制暂停时,OnPause、OnStop周期中UnitySendMessage无效。(个人未测)
故要特殊处理这两个方法,现改成C#直接调用lua方法,去掉监听推送这一步。

C#:
/// <summary>
/// 当程序获得或者是去焦点时
/// </summary>
/// <param name="focus"></param>
public void OnApplicationFocus(bool focus)
{
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>
/// <param name="focus"></param>
public void OnApplicationPause(bool isPause)
{
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:
--当程序获得或者是去焦点时
function MineMessageCenter.OnApplicationFocus(focus)
print("焦点:"..tostring(focus));
end

--当程序暂停
function MineMessageCenter.OnApplicationPause(isPause)
--print("暂停:"..tostring(isPause));
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  焦点