Customizing new scene creation
Customize the new scene creation process with scripts.
阅读时间2 分钟最后更新于 1 个月前
To run custom code when Unity instantiates a new scene from a template, create a Scene Template Pipeline script and connect it to the template. Each time you create a new scene from the template, Unity creates a new instance of the pipeline script as well.
To connect the script to a template:
- Inspect the template to edit its properties.
- Set the Scene Template Pipeline property to point to your Scene Template Pipeline script.
You can also use the method to connect the script to the template via C#.
SceneTemplateAsset.templatePipelineA Scene Template Pipeline script must derive from the interface or . It should implement the events you want to react to; for example, or in the code below.
ISceneTemplatePipelineSceneTemplatePipelineAdapterBeforeTemplateInstantiationAfterTemplateInstantiationExample:
using UnityEditor.SceneTemplate;using UnityEngine;using UnityEngine.SceneManagement;public class DummySceneTemplatePipeline : ISceneTemplatePipeline{ public void BeforeTemplateInstantiation(SceneTemplateAsset sceneTemplateAsset, bool isAdditive, string sceneName) { if (sceneTemplateAsset) { Debug.Log($"Before Template Pipeline {sceneTemplateAsset.name} isAdditive: {isAdditive} sceneName: {sceneName}"); } } public void AfterTemplateInstantiation(SceneTemplateAsset sceneTemplateAsset, Scene scene, bool isAdditive, string sceneName) { if (sceneTemplateAsset) { Debug.Log($"After Template Pipeline {sceneTemplateAsset.name} scene: {scene} isAdditive: {isAdditive} sceneName: {sceneName}"); } }}
Scene Template instantiation sequence
When you create a new scene from a template with cloneable dependencies, Unity performs several file operations. Most of these operations trigger Unity events that you can listen for, and react to, in scripts.
After you create a new scene, Unity performs the following actions:
-
Creates a scene template asset, a template scene associated with the template, and a new instance of the template scene.
-
Triggers theevent for the template asset, and binds the asset to a
ISceneTemplatePipeline.BeforeTemplateInstantiationscript that it triggers.ISceneTemplatePipeline -
Triggers theevent.
SceneTemplate.NewTemplateInstantiating -
Creates a new scene that's a copy of the template scene.
-
Creates a folder with the same name as the new scene, and copies all cloneable dependencies into that folder.
-
Opens the new scene in memory, and triggers the following events:
EditorSceneManager.sceneOpening- (on all GameObjects that implement it)
MonoBehavior.OnValidate EditorSceneManager.sceneOpened
-
Remaps references to all cloneable assets, so the new scene points to the clones.
-
Saves the new scene, and triggers the following events:
-
Triggers thefor the template asset, and binds the asset to a
ISceneTemplatePipeline.AfterTemplateInstantiationscript that it triggers.ISceneTemplatePipeline -
Triggers theevent.
SceneTemplate.NewTemplateInstantiated