简体   繁体   English

在Unity3D的构建设置中获取下一个场景的名称?

[英]Get name of next Scene in build settings in Unity3D?

So, what I want to do seems like it should be extraordinarily simple. 因此,我想做的事情似乎应该非常简单。 I need to grab the name (not load it) of the next scene, as listed in the build settings. 我需要获取下一个场景的名称 (而不是加载它),如构建设置中所列。

I thought I had a simple solution by doing something like this: 我以为我有一个简单的解决方案,可以这样做:

int currentIndex = SceneManager.GetActiveScene().buildIndex;
string nextSceneName = SceneManager.GetSceneAt(currentIndex + 1).name

But apparently that "GetSceneAt" doesn't apply to the scenes in the build settings, instead it's for some kind of internal list that the SceneManager has. 但是显然,“ GetSceneAt”不适用于构建设置中的场景,而是适用于SceneManager拥有的某种内部列表。

All my google searches bring up solutions based on a specific naming order for your scenes, or exporting the list of scenes to a file before building (a list that manually needs to be exported), etc. I can't imagine such a simple problem requires such a complex solution, which is why I'm asking for myself. 我所有的Google搜索都根据场景的特定命名顺序提出解决方案,或者在构建之前将场景列表导出到文件中(手动导出列表),等等。我无法想象这么简单的问题需要如此复杂的解决方案,这就是为什么我要问自己。

How can I just grab the name of the next scene in the build settings? 如何仅在构建设置中获取下一个场景的名称?

As of Unity 5.5 this should work, if you are on an earlier version see below. 从Unity 5.5开始,这应该可以工作,如果您使用的是较低版本,请参见下文。

The most straightforward approach most people would try first is to use SceneManager.GetSceneByBuildIndex(buildIndex) because it returns a Scene object with a name property. 大多数人首先尝试的最直接的方法是使用SceneManager.GetSceneByBuildIndex(buildIndex)因为它返回带有name属性的Scene对象。

However... the documentation for it mentions: 但是...文档中提到:

This method will return a valid Scene if a scene has been added to the build settings at the given build index AND the scene is loaded. 如果已将场景添加到给定的构建索引处的构建设置中并且已加载该场景,则此方法将返回有效的Scene。 If it has not been loaded yet the SceneManager cannot return a valid Scene. 如果尚未加载,则SceneManager无法返回有效的场景。

But all is not lost because there is also SceneUtility.GetScenePathByBuildIndex(buildIndex) which doesn't depend on the scene being already loaded. 但是,所有操作并不会丢失,因为还有SceneUtility.GetScenePathByBuildIndex(buildIndex) ,它不依赖于已加载的场景。 It returns a project relative path to the scene which we can extract a scene-name from. 它返回到场景的项目相对路径,我们可以从中提取场景名称。

public static class SceneUtilityEx
{
    public static string GetNextSceneName()
    {
        var nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;

        if (nextSceneIndex < SceneManager.sceneCountInBuildSettings)
        {
            return GetSceneNameByBuildIndex(nextSceneIndex);
        }

        return string.Empty;
    }

    public static string GetSceneNameByBuildIndex(int buildIndex)
    {
        return GetSceneNameFromScenePath(SceneUtility.GetScenePathByBuildIndex(buildIndex));
    }

    private static string GetSceneNameFromScenePath(string scenePath)
    {
        // Unity's asset paths always use '/' as a path separator
        var sceneNameStart = scenePath.LastIndexOf("/", StringComparison.Ordinal) + 1;
        var sceneNameEnd = scenePath.LastIndexOf(".", StringComparison.Ordinal);
        var sceneNameLength = sceneNameEnd - sceneNameStart;
        return scenePath.Substring(sceneNameStart, sceneNameLength);
    }
}

Edit: The section below is included for historical reasons, it is only applicable to Unity 5.4 and some earlier versions. 编辑:包括以下部分是出于历史原因,它仅适用于Unity 5.4和某些早期版本。

Yes, unfortunately you are right, but...You're in luck! 是的,不幸的是你是对的,但是...你很幸运!

I created a plugin/asset for that after reading your question, it's three small scripts and it should 'just work' and using it is literally just one line of code: 在阅读完您的问题后,我为此创建了一个插件/资产,它是三个小脚本,应该“可以正常工作”,并且使用它实际上只是一行代码:

var buildSceneRecords = BuildScenes.Records;

The records contain the name, path and index for the scenes in the editor build settings. 记录包含编辑器构建设置中场景的名称,路径和索引。

You can get it here: https://github.com/sindrijo/unity3d-runtime-buildscenes 您可以在这里获取它: https : //github.com/sindrijo/unity3d-runtime-buildscenes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM