之前也参考过这篇文章 unity editor模式下获取当前gameview视图的分辨率
复杂一些 有时有用 有时没用
最后在有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);
...