统一中的多场景编辑,幸运的是,允许启动(通过编辑器播放模式)当前场景的当前分层状态。
但是,构建和运行该项目并不识别编辑器中当前的场景设置,而是从构建设置中设置的任何内容开始。
是否有办法使生成了解多场景编辑层次结构的当前编辑器状态,并构建和运行该设置?
发布于 2019-02-11 10:46:46
1.将编辑器场景放到构建设置中。
首先,在收集设置时,可以使用编辑器脚本
loaded场景,所以用isLoaded = true列出一个只包含场景的列表1.a.在MenuItem上单击更新
我在菜单上做了一个额外的按钮,因为你可能不想让它总是自动的。
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
public static class UpdateBuildSettigns
{
[MenuItem("Example/UpdateBuildSettings")]
public static void UpdateSettings()
{
// get current editor setup
SceneSetup[] editorScenes = EditorSceneManager.GetSceneManagerSetup();
// filter list e.g. get only scenes with isActive true
var activeEditorScenes = editorScenes.Where(scene => scene.isLoaded);
// set those scenes as the buildsettings
List<EditorBuildSettingsScene> editorBuildSettingsScenes = new List<EditorBuildSettingsScene>();
foreach (var sceneAsset in activeEditorScenes)
{
string scenePath = sceneAsset.path;
// ignore unsaved scenes
if (!string.IsNullOrEmpty(scenePath)) continue;
editorBuildSettingsScenes.Add(new EditorBuildSettingsScene(scenePath, true));
}
// Set the Build Settings window Scene list
EditorBuildSettings.scenes = editorBuildSettingsScenes.ToArray();
}
}在菜单按钮上更新

1.b.自动更新(Un)加载场景
如果希望它自动发生,还可以使用EditorSceneManager.sceneOpened和静态构造函数将回调作为回调添加到InitializeOnLoad和EditorSceneManager.sceneClosed中,以便在重新编译或打开UnityEditor后添加回调,如
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
[InitializeOnLoad]
public static class UpdateBuildSettigns
{
// ofcourse you still can also call it via menu item
[MenuItem("Example/UpdateBuildSettings")]
public static void UpdateSettings()
{
//...
}
static UpdateBuildSettigns()
{
// it is always save to remove callbacks even if they are not there
// makes sure they are always only added once
//
// this is a static constructor so actually there should be no
// callbacks yet ... but .. you never know ;)
EditorSceneManager.sceneOpened -= OnSceneLoaded;
EditorSceneManager.sceneClosed -= OnSceneUnloaded;
EditorSceneManager.sceneOpened += OnSceneLoaded;
EditorSceneManager.sceneClosed += OnSceneUnloaded;
}
private static void OnSceneUnloaded(Scene current)
{
UpdateSettings();
}
private static void OnSceneLoaded(Scene current, OpenSceneMode mode)
{
UpdateSettings();
}
}使用自动更新

1.c.启用/禁用自动更新
如果需要更多的控件,还可以添加额外的菜单项,以启用和禁用以下自动更新
// flag to check if auto-updates are currently enabled
private static bool isEnabled;
// disable the "EnableAutoUpdate" button if already enabled
[MenuItem("Example/EnableAutoUpdate", true)]
private static bool CanEnable()
{
return !isEnabled;
}
// disable the "DisableAutoUpdate" button if already disabled
[MenuItem("Example/DisableAutoUpdate", true)]
private static bool CanDisable()
{
return isEnabled;
}
// add callbacks
[MenuItem("Example/EnableAutoUpdate")]
private static void EnableAutoUpdate()
{
// it is always save to remove callbacks even if they are not there
// makes sure they are always only added once
EditorSceneManager.sceneOpened -= OnSceneLoaded;
EditorSceneManager.sceneClosed -= OnSceneUnloaded;
EditorSceneManager.sceneOpened += OnSceneLoaded;
EditorSceneManager.sceneClosed += OnSceneUnloaded;
isEnabled = true;
}
// remove callbacks
[MenuItem("Example/DisableAutoUpdate")]
private static void DisableAutoUpdate()
{
EditorSceneManager.sceneOpened -= OnSceneLoaded;
EditorSceneManager.sceneClosed -= OnSceneUnloaded;
isEnabled = false;
}注意,由于这使用了UnityEditor命名空间,所以您应该将此脚本放置在Editor文件夹中,或者使用适当的预处理器(如
#if UNITY_EDITOR
// above code here
#endif2.从构建设置加载所有场景
在第一个场景中运行应用程序时,应该有一个脚本负责加载所有这些场景。类似的东西。
// making it a component to make sure it is inside of one scene
public class SceneLoader : MonoBehaviour
{
private void Start()
{
var thisScene = SceneManager.GetActiveScene();
// load all scenes
for(int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
{
// skip if is current scene since we don't want it twice
if(thisScene.buildIndex == i) continue;
// Skip if scene is already loaded
if(SceneManager.GetSceneByBuildIndex(i).IsValid()) continue;
SceneManager.LoadScene(i, LoadSceneMode.Additive);
// or depending on your usecase
SceneManager.LoadSceneAsync(i, LoadSceneMode.Additive);
}
}
}参考文献:
发布于 2019-02-05 11:55:29
我要做的是将某种脚本附加到启动场景中,然后在游戏开始后触发其余所需场景的加载。这需要一些修改才能正常启动(例如,在尝试加载场景之前,要检测场景尚未加载的事实)。
如果您需要的话,我可以使用代码片段扩展这个答案,以实现这个结果。
现在,您可以看看这里的文档:https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.GetSceneByName.html https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html
基本想法是:
如果您希望在构建时保留当前层次结构(在编辑器中打开的一组场景),则可以使用BuildPipeline:https://docs.unity3d.com/Manual/BuildPlayerPipeline.html实现它。
有一种方法可以使用可编程的场景列表进行构建:
// Get filename.
string path = EditorUtility.SaveFolderPanel("Choose Location of Built Game", "", "");
string[] levels = new string[] {"Assets/Scene1.unity", "Assets/Scene2.unity"}; // You'd have to assemble this list yourself.
// Build player.
BuildPipeline.BuildPlayer(levels, path + "/BuiltGame.exe", BuildTarget.StandaloneWindows, BuildOptions.None);(在运行构建时,您可以根据当前加载的场景确定该场景)。这将不是一个标准(Cmd + b)构建,但相当接近。
https://stackoverflow.com/questions/54499028
复制相似问题