Unity 游戏退出 之 编辑状态下的游戏退出

 时间:2024-10-21 07:59:17

Unity 游戏退出 之 编辑状态下的游戏退出。本节介绍,在Unity游戏开发中,在编辑状态下游戏的退出“Application.Quit();”并不起效,而必须使用其他的方法,本节将简单案例介绍,具体如下

工具/原料

Unity

一、知识要点

1、Application.Quit:1犬匮渝扮)功能简述public static voidQuit();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. ConsultApple Technical Page qa1561for further details.2)使用举例using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour { void Update() { if (Input.GetKey("escape")) Application.Quit(); }}

2、EditorApplication.isPlaying:1)功能简述public static bo泠贾高框olisPlaying;Is editor currently in play mode?Setting isPlaying delays the result until after all script code has completed for this frame.See Also:isPaused,isPlayingOrWillChangePlaymode.2)使用举例// Simple editor Script that lets you save a scene while in play mode.// WARNING: All Undo posibilities are lost after saving the scene.import UnityEditor;@MenuItem("Example/Save Scene while on play mode")static function EditorPlaying() { if(EditorApplication.isPlaying) { var sceneName : String = EditorApplication.currentScene; var path : String [] = sceneName.Split(char.Parse("/")); path[path.Length -1] = "Temp_" + path[path.Length-1]; var tempScene = String.Join("/",path); EditorApplication.SaveScene(tempScene); EditorApplication.isPaused = false; EditorApplication.isPlaying = false; FileUtil.DeleteFileOrDirectory(EditorApplication.currentScene); FileUtil.MoveFileOrDirectory(tempScene, sceneName); FileUtil.DeleteFileOrDirectory(tempScene); EditorApplication.OpenScene(sceneName); }}

二、游戏退出 之 编辑状态下的游戏退出

1、打开Unity,新建一个工程,具体如下图

Unity 游戏退出 之 编辑状态下的游戏退出

2、在场景中,新建一个“Cube”,具体如下图

Unity 游戏退出 之 编辑状态下的游戏退出

3、在工程中,新建一个脚本“QuitGame”,双击脚本或者右键“Open C# Project”打开脚本,具体如下图

Unity 游戏退出 之 编辑状态下的游戏退出

4、在“QuitGame”脚本上编辑代码,在Update函数中,当按下“Q”键时,如果是编辑状态使用“UnityEditor.EditorApplication.isPlaying=false;”退出,并打印;如果是正常下“Application.Quit();”退出,并打印,具体如下图

Unity 游戏退出 之 编辑状态下的游戏退出

5、“QuitGame”脚本的短铘辔嗟具体内容如下:usingUnityEngine;publicclassQuitGame:MonoBehaviour{ //Updateiscalledonceperframe voidUpdate(){if(Input.GetKeyDown(KeyCode.Q)){#ifUNITY_EDITORUnityEditor.EditorApplication.isPlaying=false; Debug.Log("编辑状态游戏退出"); #elseApplication.Quit(); Debug.Log("游戏退出"): #endif } }}

6、脚本编译正确,回到Unity界面,在场景中,新建一个“GameObject”,把脚本“QuitGame”赋给“GameObject”,具体如下图

Unity 游戏退出 之 编辑状态下的游戏退出

7、运行场景,在游戏视图中,按下“Q”键,在编辑状态下,游戏也可以正常退出,并且控制台Console打印对应信息,具体如下图

Unity 游戏退出 之 编辑状态下的游戏退出
Unity 游戏退出 之 编辑状态下的游戏退出

8、到此,《Unity 游戏退出 之 编辑状态下的游戏退出》讲解结束,谢谢

  • C4D中如何创建一个翻板动画
  • 怎样用AE减掉爱剪辑的片头片尾
  • AE如何添加小火苗特效
  • Axure RP8怎样分割图片?
  • AE制作空间网格效果
  • 热门搜索
    低碳生活手抄报资料 英语故事手抄报 感恩自然手抄报 反对邪教手抄报图片 保护动物的手抄报 感恩励志手抄报 长城手抄报图片 数学的手抄报 关于新年的手抄报图片 三节手抄报