繁体   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