135 lines
5.9 KiB
C#
135 lines
5.9 KiB
C#
|
|
// WL_GameViewSize.cs — 게임뷰 해상도 전환 재사용 도구 (에디트 모드 · 프로젝트 에셋 미수정)
|
||
|
|
//
|
||
|
|
// 사용법:
|
||
|
|
// unity command run_script --file AgentScripts/WL_GameViewSize.cs --entry WL_GameViewSize.Set --args '[1080,1920]'
|
||
|
|
// unity command run_script --file AgentScripts/WL_GameViewSize.cs --entry WL_GameViewSize.Current
|
||
|
|
// unity command run_script --file AgentScripts/WL_GameViewSize.cs --entry WL_GameViewSize.List
|
||
|
|
//
|
||
|
|
// 왜 리플렉션인가: UnityEditor.GameViewSizes / GameViewSize / GameView 는 전부 internal 이라
|
||
|
|
// 공개 API 가 없다. 해상도별 UI 실측(CanvasScaler scaleFactor 는 Screen 크기에 의존)에는
|
||
|
|
// 게임뷰 해상도를 실제로 바꾸는 것 외에 방법이 없다.
|
||
|
|
//
|
||
|
|
// 주의: 게임뷰 크기는 에디터 상태이지 프로젝트 에셋이 아니다. 이 스크립트는 Assets 를 수정하지 않는다.
|
||
|
|
// 같은 (w,h) 로 다시 호출하면 커스텀 사이즈를 중복 생성하지 않고 기존 항목을 재사용한다.
|
||
|
|
using System;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Reflection;
|
||
|
|
using System.Text;
|
||
|
|
using UnityEditor;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public static class WL_GameViewSize
|
||
|
|
{
|
||
|
|
const string Tag = "WL"; // 이 도구가 만든 커스텀 사이즈 라벨 접두어
|
||
|
|
|
||
|
|
static Type T(string n) => typeof(Editor).Assembly.GetType(n);
|
||
|
|
|
||
|
|
static object SizesInstance()
|
||
|
|
{
|
||
|
|
var tSizes = T("UnityEditor.GameViewSizes");
|
||
|
|
var tSingle = typeof(ScriptableSingleton<>).Assembly
|
||
|
|
.GetType("UnityEditor.ScriptableSingleton`1").MakeGenericType(tSizes);
|
||
|
|
return tSingle.GetProperty("instance", BindingFlags.Public | BindingFlags.Static).GetValue(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
static object CurrentGroup()
|
||
|
|
{
|
||
|
|
var inst = SizesInstance();
|
||
|
|
var tSizes = T("UnityEditor.GameViewSizes");
|
||
|
|
var groupType = tSizes.GetProperty("currentGroupType", BindingFlags.Public | BindingFlags.Instance).GetValue(inst);
|
||
|
|
return tSizes.GetMethod("GetGroup", BindingFlags.Public | BindingFlags.Instance).Invoke(inst, new[] { groupType });
|
||
|
|
}
|
||
|
|
|
||
|
|
static int FindIndex(int w, int h)
|
||
|
|
{
|
||
|
|
var group = CurrentGroup();
|
||
|
|
var tGroup = group.GetType();
|
||
|
|
int total = (int)tGroup.GetMethod("GetTotalCount", BindingFlags.Public | BindingFlags.Instance).Invoke(group, null);
|
||
|
|
var getSize = tGroup.GetMethod("GetGameViewSize", BindingFlags.Public | BindingFlags.Instance);
|
||
|
|
for (int i = 0; i < total; i++)
|
||
|
|
{
|
||
|
|
var s = getSize.Invoke(group, new object[] { i });
|
||
|
|
var st = s.GetType();
|
||
|
|
int sw = (int)st.GetProperty("width").GetValue(s);
|
||
|
|
int sh = (int)st.GetProperty("height").GetValue(s);
|
||
|
|
var kind = st.GetProperty("sizeType").GetValue(s).ToString();
|
||
|
|
if (sw == w && sh == h && kind == "FixedResolution") return i;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int EnsureSize(int w, int h)
|
||
|
|
{
|
||
|
|
int idx = FindIndex(w, h);
|
||
|
|
if (idx >= 0) return idx;
|
||
|
|
|
||
|
|
var tSize = T("UnityEditor.GameViewSize");
|
||
|
|
var tSizeType = T("UnityEditor.GameViewSizeType");
|
||
|
|
var ctor = tSize.GetConstructor(new[] { tSizeType, typeof(int), typeof(int), typeof(string) });
|
||
|
|
var fixedRes = Enum.Parse(tSizeType, "FixedResolution");
|
||
|
|
var size = ctor.Invoke(new[] { fixedRes, (object)w, h, Tag + " " + w + "x" + h });
|
||
|
|
|
||
|
|
var group = CurrentGroup();
|
||
|
|
group.GetType().GetMethod("AddCustomSize", BindingFlags.Public | BindingFlags.Instance)
|
||
|
|
.Invoke(group, new[] { size });
|
||
|
|
SizesInstance().GetType().GetMethod("SaveToHDD", BindingFlags.Public | BindingFlags.Instance)
|
||
|
|
?.Invoke(SizesInstance(), null);
|
||
|
|
return FindIndex(w, h);
|
||
|
|
}
|
||
|
|
|
||
|
|
static EditorWindow GameViewWindow()
|
||
|
|
{
|
||
|
|
var tGV = T("UnityEditor.GameView");
|
||
|
|
var wins = Resources.FindObjectsOfTypeAll(tGV);
|
||
|
|
if (wins != null && wins.Length > 0) return (EditorWindow)wins[0];
|
||
|
|
return EditorWindow.GetWindow(tGV, false, "Game", false);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// 게임뷰를 지정 해상도(FixedResolution)로 전환하고 실제 Screen 크기를 돌려준다.
|
||
|
|
public static object Set(int w, int h)
|
||
|
|
{
|
||
|
|
int idx = EnsureSize(w, h);
|
||
|
|
if (idx < 0) return "ABORT: 사이즈 등록 실패 " + w + "x" + h;
|
||
|
|
|
||
|
|
var gv = GameViewWindow();
|
||
|
|
var tGV = gv.GetType();
|
||
|
|
|
||
|
|
var prop = tGV.GetProperty("selectedSizeIndex", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||
|
|
if (prop != null) prop.SetValue(gv, idx);
|
||
|
|
else
|
||
|
|
{
|
||
|
|
var cb = tGV.GetMethod("SizeSelectionCallback", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||
|
|
if (cb == null) return "ABORT: selectedSizeIndex / SizeSelectionCallback 둘 다 없음";
|
||
|
|
cb.Invoke(gv, new object[] { idx, null });
|
||
|
|
}
|
||
|
|
|
||
|
|
gv.Repaint();
|
||
|
|
// 캔버스 레이아웃을 새 해상도로 즉시 갱신 (실측 전 필수)
|
||
|
|
Canvas.ForceUpdateCanvases();
|
||
|
|
return "OK idx=" + idx + " 요청=" + w + "x" + h + " Screen=" + Screen.width + "x" + Screen.height;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static object Current()
|
||
|
|
{
|
||
|
|
return "Screen=" + Screen.width + "x" + Screen.height;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static object List()
|
||
|
|
{
|
||
|
|
var group = CurrentGroup();
|
||
|
|
var tGroup = group.GetType();
|
||
|
|
int total = (int)tGroup.GetMethod("GetTotalCount").Invoke(group, null);
|
||
|
|
var getSize = tGroup.GetMethod("GetGameViewSize");
|
||
|
|
var sb = new StringBuilder();
|
||
|
|
for (int i = 0; i < total; i++)
|
||
|
|
{
|
||
|
|
var s = getSize.Invoke(group, new object[] { i });
|
||
|
|
var st = s.GetType();
|
||
|
|
sb.AppendLine(i + ": " + st.GetProperty("sizeType").GetValue(s) + " "
|
||
|
|
+ st.GetProperty("width").GetValue(s) + "x" + st.GetProperty("height").GetValue(s)
|
||
|
|
+ " \"" + st.GetProperty("baseText").GetValue(s) + "\"");
|
||
|
|
}
|
||
|
|
return sb.ToString();
|
||
|
|
}
|
||
|
|
}
|