首页 文章

即使在独立模式下,仍然无法使Escape键退出游戏 . 我怎么解决呢?

提问于
浏览
-1

我在Windows 10 64bit上使用unity 5.4.1f1 Personal

我将解释我所做的所有步骤以及我迄今为止所做的所有事情 .

第一次我用原始代码创建了一个新的c#文件:现在,当我按下Escape键时,它将返回编辑器,但不会退出游戏!使用[InitializeOnLoad]时,无法停止编辑器播放按钮并退出游戏 . 要退出游戏,您只能在独立模式下使用Build And Run进行游戏,然后您可以创建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中 .

现在,如果我将像以前一样开始游戏,并按下转义键,它将返回编辑器,但不会退出游戏!

但是现在,如果我在菜单File> Build&Run中制作,我将收到两个错误:

Errors

第一个错误是:

Assets / MyScripts / FullScreenPlayMode.cs(1,7):错误CS0246:找不到类型或命名空间名称“UnityEditor” . 您是否缺少using指令或程序集引用?

第二个错误:

构建Player时出错,因为脚本存在编译错误

所以我去了这个解决方案,假设这个链接是一个解决方案:

Not working solution

如果我将文件脚本移动到EDITOR目录,那么我无法将脚本拖到ThirdPersonController,它会抛出一条消息,说脚本是编辑器脚本 .

如果我在链接中尝试了第二个解决方案:

#if UNITY_EDITOR
     // Editor specific code here
 #endif

所以我在顶部的脚本中做到了:

#if UNITY_EDITOR
using UnityEditor;
#endif

并且脚本文件不在Editor目录中!这次当我制作Build&Run时,我得到一个新的另一个错误:

Assets / MyScripts / FullScreenPlayMode.cs(34,16):错误CS0246:找不到类型或命名空间名称“EditorWindow” . 您是否缺少using指令或程序集引用?

无法使用EditorWindow找到此错误 . 当我使用#if和#endif时会发生此错误

所以到目前为止,我尝试过的所有方法都有另一个问题 .

1 回答

  • 2

    我可能会简化您的问题,但您可以这样做:

    void Update()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
     #if UNITY_EDITOR
            if (EditorApplication.isPlaying)
            {
                EditorApplication.isPlaying = false;
            }
    
    #else 
            Application.Quit();
    #endif
            }
    

相关问题