我在 Windows 10 64 位上使用 unity 5.4.1f1 Personal
我将解释我所做的所有步骤以及迄今为止我尝试过的所有事情。
我第一次使用原始代码创建了一个新的 c# 文件:
现在,当我按 Escape 键时,它将返回编辑器,但不会退出游戏!使用[InitializeOnLoad]时,您无法停止编辑器播放按钮并退出游戏。要退出游戏,您只能在独立模式下使用“构建并运行”来完成,然后您可以使用 Application.Quit();
using UnityEditor;
using UnityEngine;
using System.Collections;
[InitializeOnLoad]
public class FullscreenPlayMode : MonoBehaviour {
//The size of the toolbar above the game view, excluding the OS border.
private static int tabHeight = 22;
static FullscreenPlayMode()
{
EditorApplication.playmodeStateChanged -= CheckPlayModeState;
EditorApplication.playmodeStateChanged += CheckPlayModeState;
EditorApplication.update += Update;
}
static void CheckPlayModeState()
{
if (EditorApplication.isPlaying)
{
FullScreenGameWindow();
}
else
{
CloseGameWindow();
}
}
static EditorWindow GetMainGameView(){
EditorApplication.ExecuteMenuItem("Window/Game");
System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
System.Object Res = GetMainGameView.Invoke(null,null);
return (EditorWindow)Res;
}
static void FullScreenGameWindow(){
EditorWindow gameView = GetMainGameView();
Rect newPos = new Rect(0, 0 - tabHeight, Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);
gameView.position = newPos;
gameView.minSize = new Vector2(Screen.currentResolution.width, Screen.currentResolution.height + tabHeight);
gameView.maxSize = gameView.minSize;
gameView.position = newPos;
}
static void CloseGameWindow(){
EditorWindow gameView = GetMainGameView();
gameView.Close();
}
static void Update()
{
if (Input.GetKey (KeyCode.Escape))
{
CloseGameWindow ();
}
}
}
然后,由于这不能按我想要的方式工作,所以我尝试了其他方法。
我删除了该行: [InitializeOnLoad] 但没有更改脚本其余部分中的任何其他内容。
这次删除行 [InitializeOnLoad] 后,我将脚本拖到 ThirdPersonController 中。
现在,如果我像以前一样从编辑器开始游戏并按转义键,它将返回编辑器但不会退出游戏!
但现在如果我在菜单“文件”>“构建并运行”中进行创建,我将收到两个错误:
第一个错误是:
Assets/MyScripts/FullScreenPlayMode.cs(1,7):错误CS0246:找不到类型或命名空间名称“UnityEditor”。您是否缺少 using 指令或程序集引用?
第二个错误:
由于脚本存在编译器错误,构建 Player 时出错
所以我找到了这个解决方案,这个链接中的解决方案应该是:
不起作用的解决方案
如果我将文件脚本移动到 EDITOR 目录,那么我无法将脚本拖动到 ThirdPersonController,它将抛出一条消息,表示该脚本是编辑器脚本。
如果我尝试了链接中的第二个解决方案:
#if UNITY_EDITOR
// Editor specific code here
#endif
所以我在顶部的脚本中做到了:
#if UNITY_EDITOR
using UnityEditor;
#endif
而且脚本文件不在Editor目录下!
这次当我进行“构建并运行”时,我收到了另一个新错误:
Assets/MyScripts/FullScreenPlayMode.cs(34,16):错误CS0246:找不到类型或命名空间名称“EditorWindow”。您是否缺少 using 指令或程序集引用?
无法找到 EditorWindow 的错误是什么。当我使用 #if 和 #endif 时会发生此错误
所以到目前为止我尝试过的所有方法都遇到了另一个问题。