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后重新打開項目並添加上述腳本后方能正常退出游戲模式。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM