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

2023-08-24 15:26:17 浏览数 (2)

小米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传递消息

代码语言:javascript复制
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. 再还原
代码语言:javascript复制
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中修改分辨率

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

代码语言:javascript复制
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 人点赞