unity editor模式下获取当前gameview Game窗口 游戏视图的模拟屏幕分辨率

这段代码展示了在Unity中如何通过反射获取编辑器中的Game视图尺寸,并在运行时获取屏幕分辨率。在编辑器中,它利用Unity的内部方法`GetMainPlayModeViewTargetSize`获取Game视图尺寸,而在实际设备上则使用`Screen.currentResolution`。此外,还提供了一种在多屏环境下获取对应Canvas的屏幕分辨率的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用这个脚本的最后其实都要下面这个方法

  public static Vector2 GetScreenResolution()
    {
        Vector2 gameViewSize;
        //使用宏编译主要是为了打包的时候不会报错
#if UNITY_EDITOR
        gameViewSize = GameViewSize();
#else
        gameViewSize = new Vector2(Screen.currentResolution.width,
            Screen.currentResolution.height) ;
#endif

        return gameViewSize;
    }

最简单

发现了一个比较直接的API

   int width = Display.displays[0].renderingWidth;
   int hieght = Display.displays[0].renderingWidth;
          

封装一下

float GetScreenWidth()
     {
          if (Application.platform == RuntimePlatform.WindowsEditor ||
              Application.platform == RuntimePlatform.OSXEditor)
          {
               return  Display.displays[0].renderingWidth;
          }

          return Screen.width;
     }
     
     float GetScreenHeight()
     {
          if (Application.platform == RuntimePlatform.WindowsEditor ||
              Application.platform == RuntimePlatform.OSXEditor)
          {
               return  Display.displays[0].renderingHeight;
          }

          return Screen.height;
     }

根据反射

using UnityEngine;


public class GetGameViewSize : MonoBehaviour
{
    private void OnEnable()
    {
        GameViewSize();
    }

#if UNITY_EDITOR
    private Vector2 GameViewSize()
    {
        var mouseOverWindow = UnityEditor.EditorWindow.mouseOverWindow;
        System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
        System.Type type = assembly.GetType("UnityEditor.PlayModeView");

        Vector2 size = (Vector2) type.GetMethod(
            "GetMainPlayModeViewTargetSize",
            System.Reflection.BindingFlags.NonPublic |
            System.Reflection.BindingFlags.Static
        ).Invoke(mouseOverWindow, null);

        return size;
    }

#endif
}

这个代码经过测试能在编辑器运行,试过几次都能正确输出Game视图的尺寸。

屏幕尺寸是1920*1080,则最大化Game视图的时候尺寸和这个基本一致。

根据UI

最后在有canvas的情况下,想了个方法获得屏幕的分辨率
在这里插入图片描述

#if UNITY_EDITOR
int canvasDisplay = RootCanvasTransform.GetComponent<Canvas>().targetDisplay;
            float editorWidth = Display.displays[canvasDisplay].renderingWidth;
            float editorHeight = Display.displays[canvasDisplay].renderingHeight;
#else        

RootCanvasTransform指定那个display才能获得对应的GameView窗口
这个做法在做多屏应用的时候 应该也能获得所需canvas对应的display的分辨率
所以一个全屏扩展图片的做法是

#if UNITY_EDITOR

        int canvasDisplay = RootCanvasTransform.GetComponent<Canvas>().targetDisplay;
        float screenWidth = Display.displays[canvasDisplay].renderingWidth;
        float screenHeight = Display.displays[canvasDisplay].renderingHeight;
#else
        float screenWidth = Screen.currentResolution.width;
        float screenHeight = Screen.currentResolution.height;
#endif
        
        
        ...
        hangerRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, screenWidth);
        hangerRectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, screenHeight);
        ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

染指流年丨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值