温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Unity3d 失去获取焦点,暂停

发布时间:2020-05-20 01:02:28 来源:网络 阅读:2370 作者:wo爱钱 栏目:编程语言

     目前在用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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI