[Unity3D] How to use Unity to realize the function of exiting the game

1. When using the Unity compiler:

UnityEditor.EditorApplication.isPlaying = false;

2. After packaging:

Application.Quit();

Many times, we all know the Application.Quit() code, but when we debug the Unity compiler, we find that even the components or objects with this code cannot exit the compilation, because the code can only realize the exit function after packaging.

So we can write the two implementation codes into the script at the same time, so that the exit function can be realized in the compiler or after packaging.

code show as below:

  public void OnExitGame()//定义一个退出游戏的方法
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;//如果是在unity编译器中
#else
        Application.Quit();//否则在打包文件中
#endif
    }

Guess you like

Origin blog.csdn.net/dislike_carry/article/details/128006202