簡體   English   中英

Unity 在編輯器中創建場景並添加內容

[英]Unity create scenes and add content to it in the editor

我想做的事:

  1. 我的場景中有一個扇區游戲對象列表
  2. 我想遍歷並為它創建一個新場景,scene_sector_xxx
  3. 場景的內容將是僅在 position 處的游戲對象
  4. 將此場景添加到運行時

我為什么要這個?

我需要在運行時創建一個異步/附加場景加載器來加速一切。 當玩家靠近某個扇區時,只想立即加載該扇區。

可能嗎? 也許在編輯器中?

更新:我的代碼:

    private static IEnumerator SaveSectorToSceneCoroutine(Sector sector)
    {
        
        //TODO save this go to a new scene
        var go = Object.Instantiate(sector.gameObject);
        //Object.DontDestroyOnLoad(go); //this is not working from editor
        //or need a prefab?

      

        var newScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); //maybe better from calling this in the background, how?
        SceneManager.MoveGameObjectToScene(go, newScene); //this not working, "go" became null

     

        string[] path = EditorSceneManager.GetActiveScene().path.Split(char.Parse("/"));
        path[path.Length - 1] = "_SCN_SECTOR_" + go.name + path[path.Length - 1];
        EditorSceneManager.SaveScene(newScene, string.Join("/", path), true);
      
        Debug.Log("Saved Scene " + path);   
    }

   

您的問題可能在EditorSceneManager.NewScene中。

作為模式,您傳入NewSceneMode.Single

所有當前打開的場景都關閉,新創建的場景打開。

您更願意使用的是NewSceneMode.Additive

新創建的場景被添加到當前打開的場景中。

像例如

var go = Object.Instantiate(sector.gameObject);

var newScene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive); 

SceneManager.MoveGameObjectToScene(go, newScene); 

string[] path = EditorSceneManager.GetActiveScene().path.Split('//'));
path[path.Length - 1] = "_SCN_SECTOR_" + go.name + path[path.Length - 1];
EditorSceneManager.SaveScene(newScene, string.Join('//', path), true);

EditorSceneManager.CloseScene(newScene, true);
  
Debug.Log("Saved Scene " + path);   

那么我認為這不應該是協程。 我認為沒有必要/使用做

yield return null;

在這個用例中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM