Unity3d+Android:分屏小窗还原分辨率触摸异常

小米OPPO手机小窗或者分屏再恢复全屏,游戏的分辨率触摸异常

小窗事件打印

01-07 11:32:48.604: E/SmallWindowStateHelper(3005): updateSmallWindowPackageName:sSmallWindowPackageName=null sLastValidSmallWindowPackageName=com.test.game
01-07 11:32:48.642: E/AppBackListener(22163): onTopAppChanged newApp=com.test.game,oldApp=com.miui.home,mPersistentApp=null,innerApp=false
01-07 11:32:50.147: E/Layer(991): [Bounds for - com.test.game/com.test.game.GameActivity#0] No local sync point found

Android监听onGlobalLayout

监听到布局改变时,向unity传递消息

private void addGlobalLayoutListener()
{
   if(mUnityPlayer != null)
   {
      Log.i("GameActivity", "addGlobalLayoutListener mUnityPlayer != null");
       mUnityPlayer.getView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
         public void onGlobalLayout() {
            resetTouchInputOnLayoutChange();
         }
      });
   }
   else
   {
      Log.e("GameActivity", "addGlobalLayoutListener mUnityPlayer is null!!");
   }
}

Unity得到Android消息重置分辨率

  1. nScreenWidth + 1,触发update中的调整分辨率
  2. 再还原
public void ResetResolutionOnLayoutChange()
		{
			CancelInvoke("FixResolutionCorutine");
			Invoke("FixResolutionCorutine", 2.0f);//2秒后再触发,避免闪屏
		}

		private void FixResolutionCorutine()
		{
			nScreenHeight = Screen.currentResolution.height;
			nScreenWidth = Screen.currentResolution.width;

			StartCoroutine(FixResolutionWhenReload());
		}

		static int nScreenWidth = 0;
		static int nScreenHeight = 0;
		IEnumerator FixResolutionWhenReload()
		{
			yield return new WaitForSeconds(0.1f);
#if UNITY_ANDROID
			Screen.SetResolution(nScreenWidth + 1, nScreenHeight, true);
			yield return new WaitForSeconds(3f);

			Screen.SetResolution(nScreenWidth, nScreenHeight, true);
#endif
		}

Unity Update中修改分辨率

当前分辨率与上次设置不一致时修改

void Update()
{
          
            //分辨率调整
            if (adjustWidth != Screen.currentResolution.width || adjustHeight != Screen.currentResolution.height)
            {
                ResolutionAdjust();
            }
}

private static int adjustWidth = 0;
        private static int adjustHeight = 0;

        private static void ResolutionAdjust()
        {
            adjustWidth = Screen.currentResolution.width;
            adjustHeight = Screen.currentResolution.height;
            // 对于屏幕有悬浮菜单的手机进行特殊处理,目前只关注1080P的分辨率。
            if ((adjustWidth >= 1720 && adjustWidth < 1920) || (adjustHeight >= 880 && adjustHeight < 1080))
            {
                float aspect = (float)adjustWidth / (float)adjustHeight;
                adjustHeight = 720;
                adjustWidth = (int)(aspect * adjustHeight);

                Screen.SetResolution(adjustWidth, adjustHeight, true);
                return;
            }
            // 对分辨率大于1080P分辨率的手机,强制降到1080P。
            if (adjustWidth >= 1920 && adjustHeight >= 1080)
            {
                if (adjustWidth > 1920 || adjustHeight > 1080)
                {
                    float fRatio = (float)adjustWidth / (float)adjustHeight;
                    adjustHeight = 1080;
                    adjustWidth = (int)(fRatio * adjustHeight);
                    Screen.SetResolution(adjustWidth, adjustHeight, true);
                }
            }
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值