下面进入实战操作,先说一下需求,项目组有个关卡编辑器,是用mono脚本写的,放在一个名为NewLevelEditor的场景里,并且需要设置分辨率为FreeAspect,而项目分辨率为1080X1920,启动场景为GameFramework。所以每次要去改一个场景的配置再回来着效果,又要切场景,又要改分辨率很是费劲,如何做一个工具菜单直接进入,退出的时候又能还原到之前的编辑状态呢。代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;
public class NewLevelEditorMenu
{
private static string m_EditorScenePath = "Assets/GameMain/Scenes/NewLevelEditor.unity";
[MenuItem("GameLevelEditor/打开关卡编辑器")]
static void OpenLevelEditor()
{
string lastScenePath = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().path;
EditorPrefs.SetString("lastSelectActiveScenePath", lastScenePath);
UnityEditor.SceneManagement.EditorSceneManager.OpenScene(m_EditorScenePath);
Type gameViewType = GetGameViewType();
var gameView = GetGameViewWindows(gameViewType);
var info = GetProperty_selectedSizeIndex(gameViewType);
int lastSelectResolutionIndex = (int)info.GetValue(gameView, new object[0] { });
EditorPrefs.SetInt("lastSelectResolutionIndex", lastSelectResolutionIndex);
info.SetValue(gameView, 0);
gameView.maximized = true;
gameView.Focus();
UnityEditor.EditorApplication.isPlaying = true;
}
[MenuItem("GameLevelEditor/打开关卡编辑器",true)]
static bool OpenLevelEditorVisable()
{
string curScenePath = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().path;
return m_EditorScenePath != curScenePath;
}
[MenuItem("GameLevelEditor/关闭关卡编辑器")]
static void CloseLevelEditor()
{
UnityEditor.EditorApplication.isPlaying = false;
EditorApplication.update += Update;
}
static void ExecuteCloseLevelEditorLogic()
{
Type gameViewType = GetGameViewType();
var gameView = GetGameViewWindows(gameViewType);
var info = GetProperty_selectedSizeIndex(gameViewType);
int lastSelectResolutionIndex = EditorPrefs.GetInt("lastSelectResolutionIndex");
info.SetValue(gameView, lastSelectResolutionIndex);
gameView.maximized = false;
string lastScenePath = EditorPrefs.GetString("lastSelectActiveScenePath");
UnityEditor.SceneManagement.EditorSceneManager.OpenScene(lastScenePath);
}
[MenuItem("GameLevelEditor/关闭关卡编辑器",true)]
static bool CloseLevelEditorVisable()
{
string curScenePath = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().path;
return m_EditorScenePath == curScenePath;
}
public static void Update()
{
if (!UnityEditor.EditorApplication.isPlaying)
{
EditorApplication.update -= Update;
ExecuteCloseLevelEditorLogic();
}
}
static EditorWindow GetGameViewWindows(Type gameViewType)
{
System.Reflection.MethodInfo GetMainGameView = gameViewType.GetMethod("GetMainGameView", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
System.Object Res = GetMainGameView.Invoke(null, null);
var gameView = (UnityEditor.EditorWindow)Res;
return gameView;
}
static PropertyInfo GetProperty_selectedSizeIndex(Type gameViewType)
{
var info = gameViewType.GetProperty("selectedSizeIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
return info;
}
static Type GetGameViewType()
{
Assembly assembly = typeof(EditorWindow).Assembly;
Type[] types = assembly.GetTypes();
for (int i = 0; i < types.Length; i++)
{
if (types[i].Name == "GameView")
{
return types[i];
}
}
return null;
}
}
效果:

点击以后,直接进入关卡编辑器场景,最大化窗口,最大化分辨率,自动运行游戏,激活关闭编译器按钮。点击退出后还原之前的用户操作环境,直接运行就能看效果
本文介绍如何通过反射技术实现在Unity中快速切换并设置关卡编辑器场景,包括调整分辨率、全屏显示和自动运行游戏。点击退出后,能够恢复到先前的用户工作环境,简化了开发过程中的场景切换操作。
1171

被折叠的 条评论
为什么被折叠?



