Unity
Application.Quit:1)功能简述public static void Quit();Quits the player application.Quit is ignored in the editor. IMPORTANT: In most cases termination of application under iOS should be left at the user discretion. Consult Apple Technical Page qa1561 for further details.2)使用举例 using UnityEngine; using System.Collections;public class ExampleClass : MonoBehaviour { void Update() { if (Input.GetKey('escape')) Application.Quit(); } }
打开Unity,新建一个工程,具体如下图
在场景中,新建一个“Cube”,具体如下图
在工程中,新建一个脚本“QuitGame”,双击脚本或者右键“Open C# Project”打开脚本,具体如下图
在“QuitGame”脚本上编辑代码,在Update函数中,当按下“Q”键时,如果是编辑状态使用“UnityEditor.EditorApplication.isPlaying = false;”退出,并打印;如果是正常下“Application.Quit();”退出,并打印,具体如下图
“QuitGame”脚本的具体内容如下:using UnityEngine;public class QuitGame : MonoBehaviour { // Update is called once per frame void Update () { if(Input.GetKeyDown (KeyCode.Q)) { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; Debug.Log ('编辑状态游戏退出'); #else Application.Quit(); Debug.Log ('游戏退出'): #endif } }}
脚本编译正确,回到Unity界面,在场景中,新建一个“GameObject”,把脚本“QuitGame”赋给“GameObject”,具体如下图
运行场景,在游戏视图中,按下“Q”键,在编辑状态下,游戏也可以正常退出,并且控制台Console打印对应信息,具体如下图
到此,《Unity 游戏退出 之 编辑状态下的游戏退出》讲解结束,谢谢
您的支持,是我们不断坚持知识分享的动力,若帮到您,还请帮忙投票有得;若有疑问,请留言