[英]Unity create scenes and add content to it in the editor
What I want to do:我想做的事:
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.