Unity 2023+中selectedSizeIndex替代方案及编辑器截图工具问询
Unity 2023+ 修改编辑器视图尺寸的替代方案及适配截图工具
一、替代selectedSizeIndex的代码实现
Unity 2023 Beta及后续版本中,GameView的selectedSizeIndex私有属性已被移除,需通过反射调用新的内部API来修改编辑器视图尺寸。以下是可行的实现代码:
using UnityEditor; using System.Reflection; public static class GameViewSizeHelper { /// <summary> /// 设置GameView为指定分辨率 /// </summary> /// <param name="width">目标宽度</param> /// <param name="height">目标高度</param> public static void SetTargetResolution(int width, int height) { // 获取GameView窗口实例 System.Type gameViewType = typeof(EditorWindow).Assembly.GetType("UnityEditor.GameView"); EditorWindow gameView = EditorWindow.GetWindow(gameViewType); // 获取当前尺寸组 PropertyInfo sizeGroupProp = gameViewType.GetProperty("sizeGroup", BindingFlags.Instance | BindingFlags.NonPublic); object sizeGroup = sizeGroupProp.GetValue(gameView); // 创建自定义分辨率尺寸 System.Type gameViewSizeType = typeof(EditorWindow).Assembly.GetType("UnityEditor.GameViewSize"); object customSize = System.Activator.CreateInstance(gameViewSizeType, new object[] { 1, width, height, $"Custom {width}x{height}" }); // 将自定义尺寸添加到尺寸组 MethodInfo addCustomSizeMethod = sizeGroup.GetType().GetMethod("AddCustomSize", BindingFlags.Instance | BindingFlags.Public); addCustomSizeMethod.Invoke(sizeGroup, new object[] { customSize }); // 获取尺寸组索引和新添加的尺寸索引 int groupIndex = (int)sizeGroup.GetType().GetProperty("index", BindingFlags.Instance | BindingFlags.Public).GetValue(sizeGroup); int sizeCount = (int)sizeGroup.GetType().GetMethod("GetDisplaySizeCount", BindingFlags.Instance | BindingFlags.Public).Invoke(sizeGroup, null); int targetSizeIndex = sizeCount - 1; // 设置GameView使用该尺寸 MethodInfo setSizeMethod = gameViewType.GetMethod("SetSize", BindingFlags.Instance | BindingFlags.NonPublic); setSizeMethod.Invoke(gameView, new object[] { groupIndex, targetSizeIndex }); } }
使用时直接调用GameViewSizeHelper.SetTargetResolution(1080, 1920);即可将GameView切换到指定分辨率,之后再执行截图逻辑即可。
二、适配Unity 2023+的应用商店截图工具
如果不想自行编写适配代码,可直接使用以下成熟工具:
- Unity Recorder:Unity官方内置工具,支持在编辑器中设置固定分辨率、指定截图格式,可一键导出符合应用商店要求的多尺寸截图,完全适配最新Unity版本。
- Screenshot Studio Pro:Asset Store中的付费工具,支持批量生成多分辨率截图、自动添加边框/水印,已适配Unity 2023+,操作流程简洁高效。
内容的提问来源于stack exchange,提问作者Heinrich Langehoven
相关产品推荐
相关产品推荐

