其他分享
首页 > 其他分享> > Unity 快速切换 Game 视图分辨率

Unity 快速切换 Game 视图分辨率

作者:互联网

原因

Unity Game 视图是下拉切分辨率,在编辑 UI 时不方便进行切换查看各个分辨率的效果。

解决

分析 Editor 源码,将切换分辨率的功能移植到单独的窗口,这样切换分辨率的时候,只需要点击即可。

    protected void OnGUI()
    {
        var currentGroup = GameViewSizes.instance.currentGroup;
        int count = currentGroup.GetTotalCount();
        float curWidth = 0;
        float curX = 0, curY = 0;
        for (int i = currentGroup.GetBuiltinCount(); i < count; i++)
        {
            var viewSize = currentGroup.GetGameViewSize(i);
            var content = EditorGUIUtility.TempContent(viewSize.displayText);
            var size = EditorStyles.toolbarButton.CalcSize(content);
            size.x = Mathf.Min(size.x, Mathf.Max(minSize.x, position.width));
            if (size.x + curWidth > position.width)
            {
                curX = 0;
                curY += size.y;
                curWidth = 0;
                i--;
                continue;
            }

            curWidth += size.x;
            Rect rect = new Rect(curX, curY, size.x, size.y);
            curX += size.x;
            if (GUI.Button(rect, viewSize.displayText, EditorStyles.toolbarButton))
            {
                GameView.GetMainGameView().SizeSelectionCallback(i, viewSize);
            }
        }
    }


完整工程地址:https://github.com/akof1314/Unity-EditorInternalsVisibleDemo/tree/main/EditorInternalsVisibleDemo/PackagesCustom/com.wuhuan.internalsvisibledemo/Editor/PickViewSize

标签:curWidth,viewSize,Unity,视图,var,Game,curX,currentGroup,size
来源: https://blog.csdn.net/akof1314/article/details/113453793