TheLost Lostit TheLost Lostit - 2 months ago 5
C# Question

Still can't make Escape key to quit game even in Standalone mode. How can i solve it?

I'm using unity 5.4.1f1 Personal on windows 10 64bit

I will explain all the steps i did and all the things i tried so far.

Firs time i created a new c# file with the original code:
Now when i press the Escape key it will go back to the editor but will not QUIT the game ! You can't stop the editor play button and quit the game when using [InitializeOnLoad]. To quit the game you can only do it with Build And Run in Standalone mode then you can make 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 ();
}
}
}


Then since this is not working as i wanted i tried something else.
I deleted the line: [InitializeOnLoad] but didn't change anything else in the rest of the script.

This time after deleting the line [InitializeOnLoad] i dragged the script into the ThirdPersonController.

Now if i will start the game as before form the editor and hit press the escape key it will back to the editor but will not quit the game !

But now if i make in the menu File > Build & Run i will get two errors:

Errors

The first error is:

Assets/MyScripts/FullScreenPlayMode.cs(1,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?

The second error:

Error building Player because scripts had compiler errors

So i went to this solution what suppose to be a solution in this link:

Not working solution

If i move the file script to the EDITOR directory then i can't drag the script to the ThirdPersonController it will throw a message say the script is editor script.

If i tried the second solution in the link:

#if UNITY_EDITOR
// Editor specific code here
#endif


So i did it in the script in the top:

#if UNITY_EDITOR
using UnityEditor;
#endif


And the script file is not in the Editor directory !
This time when i make Build & Run i'm getting a new another error:

Assets/MyScripts/FullScreenPlayMode.cs(34,16): error CS0246: The type or namespace name `EditorWindow' could not be found. Are you missing a using directive or an assembly reference?

Could not find what is this error with the EditorWindow. This error happen when i used the #if and #endif

So in all the ways i tried so far i got another problem.

Answer

I might be simplifying your problem but you could simply do this:

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

#else 
        Application.Quit();
#endif
        }