Unity:You must exit play mode to save the scene问题解决

出现上述错误时,通常是处于游戏模式也就是play mode的状态下进行保存操作,此时无法完成保存指令。

通常遇到这个问题时已经进入游戏模式且没有添加退出功能,下面首先介绍一下游戏退出功能的实现:

我们通常习惯使用esc键退出应用,因此这里使用esc键触发退出游戏模式的事件。

首先添加一个空物体(Create empty),在空物体中添加component,新建一个script,打开编辑界面并输入以下代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QuitGame : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.Escape))

            {
                UnityEditor.EditorApplication.isPlaying = false;

                //Application.Quit();

            } 
    }
}

其中

UnityEditor.EditorApplication.isPlaying = false;

是在Unity编辑项目的过程中使用的,在完成后我们使用:

Application.Quit();

对于还没有添加上述功能的情况下,本次进入游戏模式后的编辑内容都无法保存,需要关闭unity后重新打开项目并添加上述脚本后方能正常退出游戏模式。

posted @ 2021-03-14 13:18  btc  阅读(2883)  评论(0编辑  收藏  举报