VSClone/Assets/Editor/GameViewPortrait.cs

65 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace VS.EditorTools
{
// GUI 에디터를 열면 Game 뷰를 세로 720×1280 으로 맞춘다(내부 API 리플렉션 · 실패 시 경고만). LDClone 도구와 같은 방식.
[InitializeOnLoad]
static class GameViewPortrait
{
const string Label = "VS Portrait 720x1280";
const int Wd = 720, Ht = 1280;
static GameViewPortrait()
{
if (Application.isBatchMode) return;
EditorApplication.delayCall += () => Apply(false);
}
[MenuItem("VS/Game 뷰 세로 720x1280")]
static void ApplyMenu() => Apply(true);
static void Apply(bool verbose)
{
try
{
var asm = typeof(Editor).Assembly;
var sizesType = asm.GetType("UnityEditor.GameViewSizes");
var single = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
var instance = single.GetProperty("instance").GetValue(null);
var groupType = sizesType.GetProperty("currentGroupType").GetValue(instance);
var group = sizesType.GetMethod("GetGroup").Invoke(instance, new[] { groupType });
var gt = group.GetType();
int total = (int)gt.GetMethod("GetTotalCount").Invoke(group, null);
int idx = -1;
for (int i = 0; i < total; i++)
{
var sz = gt.GetMethod("GetGameViewSize").Invoke(group, new object[] { i });
if ((string)sz.GetType().GetProperty("baseText").GetValue(sz) == Label) { idx = i; break; }
}
if (idx < 0)
{
var sizeT = asm.GetType("UnityEditor.GameViewSize");
var kindT = asm.GetType("UnityEditor.GameViewSizeType");
var ctor = sizeT.GetConstructor(new[] { kindT, typeof(int), typeof(int), typeof(string) });
var size = ctor.Invoke(new object[] { Enum.Parse(kindT, "FixedResolution"), Wd, Ht, Label });
gt.GetMethod("AddCustomSize").Invoke(group, new[] { size });
idx = (int)gt.GetMethod("GetTotalCount").Invoke(group, null) - 1;
}
var gvType = asm.GetType("UnityEditor.GameView");
foreach (var w in Resources.FindObjectsOfTypeAll(gvType))
{
var prop = gvType.GetProperty("selectedSizeIndex", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (prop != null && prop.CanWrite) prop.SetValue(w, idx);
else gvType.GetMethod("SizeSelectionCallback", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.Invoke(w, new object[] { idx, null });
((EditorWindow)w).Repaint();
}
if (verbose) Debug.Log("[VS] Game 뷰 = " + Label);
}
catch (Exception e) { Debug.LogWarning("[VS] Game 뷰 세로 설정 실패(수동으로 720x1280 선택): " + e.Message); }
}
}
}