0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Unity] GameWindowの解像度を切り替える

Last updated at Posted at 2025-04-05

ChatGPTもGeminiもCopilotも、何度か質問を繰り返してもリフレクションで何とかしようとしてうまくいかなかったのですが、
Editor,GameWindow,Resolutionでググったらすぐ出てきました。

#if UNITY_EDITOR
    PlayModeWindow.SetCustomRenderingResolution(1024, 1024, "1024x1024");
#else
    Screen.SetResolution(1024, 1024, false);
#endif

AIで行き詰ったらたまにはググれということですね。

ちなみに現時点(04/16/2025)でAIに聞くと、多くが下記のようなコードを教えてくれました。
推奨しないコードですが、困った人がググったときにこの記事にたどり着きやすいように、一応置いておきますね

DO_NOT_USE.cs
#if UNITY_EDITOR
using UnityEditor;

public class GameViewResolutionChanger
{
    public static void SetGameViewSize(int width, int height)
    {
        // GameViewのサイズを設定
        var group = UnityEditorInternal.GameViewSizes.instance.currentGroup;
        var index = FindSizeIndex(group, width, height);
        
        if (index == -1)
        {
            AddCustomSize(group, width, height);
            index = FindSizeIndex(group, width, height);
        }
        
        SetSize(index);
    }

    private static int FindSizeIndex(UnityEditorInternal.GameViewSizeGroup group, int width, int height)
    {
        for (int i = 0; i < group.GetBuiltinCount() + group.GetCustomCount(); i++)
        {
            var size = group.GetGameViewSize(i);
            if (size.width == width && size.height == height)
                return i;
        }
        return -1;
    }

    private static void AddCustomSize(UnityEditorInternal.GameViewSizeGroup group, int width, int height)
    {
        group.AddCustomSize(new UnityEditorInternal.GameViewSize(UnityEditorInternal.GameViewSizeType.FixedResolution, width, height));
    }

    private static void SetSize(int index)
    {
        var gameViewType = typeof(EditorWindow).Assembly.GetType("UnityEditor.GameView");
        var gameView = EditorWindow.GetWindow(gameViewType);
        var sizeField = gameViewType.GetProperty("selectedSizeIndex", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
        sizeField.SetValue(gameView, index);
    }
}
#endif
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?