简体   繁体   English

Unity 在编辑器中创建场景并添加内容

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

What I want to do:我想做的事:

  1. I have a list of Sector gameObjects on my scene我的场景中有一个扇区游戏对象列表
  2. I want to iterate through and create a new scene for it, scene_sector_xxx我想遍历并为它创建一个新场景,scene_sector_xxx
  3. the scene's content will be the gameObject only at that position场景的内容将是仅在 position 处的游戏对象
  4. add this scene to runtime将此场景添加到运行时

Why I want this?我为什么要这个?

I need to create an async/additive scene loader at runtime to speed up everything.我需要在运行时创建一个异步/附加场景加载器来加速一切。 When the player is near by a sector then just want to load that sector piece immediatelly.当玩家靠近某个扇区时,只想立即加载该扇区。

Is it possible?可能吗? In editor maybe?也许在编辑器中?

Update: my code:更新:我的代码:

    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);   
    }

   

Your issue is probably in EditorSceneManager.NewScene .您的问题可能在EditorSceneManager.NewScene中。

As mode you are passing in NewSceneMode.Single作为模式,您传入NewSceneMode.Single

All current open Scenes are closed and the newly created Scene are opened.所有当前打开的场景都关闭,新创建的场景打开。

What you rather want to use is NewSceneMode.Additive您更愿意使用的是NewSceneMode.Additive

The newly created Scene is added to the current open Scenes.新创建的场景被添加到当前打开的场景中。

like eg像例如

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);   

Then I don't think this should be a Coroutine.那么我认为这不应该是协程。 I don't see any need for/use of doing我认为没有必要/使用做

yield return null;

in this use case在这个用例中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM