构建的多场景启动,原样,从 Unity,怎么样?

Multi-scene launching of build, as is, from Unity, how?

幸运的是,Unity 中的多场景编辑允许启动(通过编辑器播放模式)处于当前分层状态的当前场景。

但是,构建和 运行 项目无法识别编辑器中的当前场景设置,而是从构建设置中设置的任何内容开始。

是否有某种方法可以让构建了解多场景编辑层次结构的当前编辑器状态,并构建和运行该设置?

我要做的是统一将某种脚本附加到启动场景,然后在游戏开始后触发加载其余所需场景。这将需要一些摆弄才能正确启动(例如,在尝试加载场景之前检测场景尚未加载的事实)。

如果您需要,我可能会用代码片段扩展答案以获得结果。

现在您可以在这里查看文档: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.GetSceneByName.html https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

基本思路是:

  1. 使用SceneManager.GetSceneByName获取必要的场景并 过滤掉所有已经加载的场景。
  2. 对于尚未加载的场景,调用 LoadSceneAsync 和 附上某种 检查加载进度的协程。
  3. 加载所有场景后,运行一个回调,以便其余的 游戏知道场景已加载并且 运行 依赖于正在加载的那些场景的其余必要操作。

如果你想在构建时保留当前层次结构(一组在编辑器中打开的场景),那么使用 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) 构建,但非常接近。

1。将编辑器场景引入构建设置

首先要收集设置,您可以使用编辑器脚本

1.a。单击菜单项更新

我把它作为菜单中的一个额外按钮,因为您可能不希望它总是自动出现。

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。在(取消)加载场景时自动更新

如果您希望它自动发生,您还可以将调用作为回调添加到 EditorSceneManager.sceneOpened and EditorSceneManager.sceneClosed using InitializeOnLoad 和静态构造函数以在重新编译或打开 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。 Enable/Disable 自动更新

如果您想要更多控制权,您还可以添加额外的菜单条目来启用和禁用自动更新,例如

// 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

#endif

2。从构建设置中加载所有场景

比后来 运行 第一个场景中的应用程序应该有一个脚本负责加载所有这些场景。比如

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

参考文献: