unity3d - SceneManager 没有按名称获取场景

标签 unity3d scene scene-manager

我的 Unity 项目中有 3 个场景,并试图通过其名称获取场景索引。我正在尝试使用 SceneManager但我不知道如何使用 SceneManager.GetSceneByName .使用

SceneManager.GetSceneByName("Scene1");

返回 null虽然我可以使用加载场景
SceneManager.LoadScene("Scene1",LoadSceneMode.Single);

还有 SceneManager.sceneCount返回 1虽然我有 3 个场景。

Unity 文档 GetSceneByName说:

Searches through the scenes added to the SceneManager for a scene with the given name.

The name has to be without the .unity extension. The name can be the last part of the name as displayed in the BuildSettings window in which case the first scene that matches will be returned. The name could also the be path as displayed in the Build Settings, still without the extension, in which case only the exact match will be returned. This is case insensitive.



这是否意味着我应该将所有场景添加到 SceneManager第一的?

我怎样才能做到这一点?

如果是这样,那为什么SceneManager.LoadScene()使用场景名称而不将其添加到 SceneManager ?

注:我已经将所有 3 个场景添加到 Build 设置中。

最佳答案

方法SceneManager.GetSceneByName()在已加载的场景列表中搜索场景。
就 SceneManager.sceneCount 而言,它的统一文档明确指出它返回:

number of currently loaded scenes



为了找出build设置中的场景数量,有一个单独的变量 SceneManager.sceneCountInBuildSettings .

如果您使用 SceneManager.GetSceneByName() , 以加法模式加载场景后,返回一个有效场景。

关于unity3d - SceneManager 没有按名称获取场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38801657/

相关文章:

unity3d - 检查对象是否完全通过另一个对象 Unity3d

swift - SpriteKit : Scaling Does Not Work Correctly

java - 完全删除AndEngine中的场景

c# - Unity场景切换花费太多时间

c# - 将 UnityScript 翻译成 C# : yield & transform. 位置

c# - 在 Unity 中简化使用语句

ios - 应用程序经过广泛测试,也得到了苹果的认可,但一旦进入 App Store,所有用户都会崩溃

三.js:如何统计场景中添加的对象

ios - 卸载 assetbundle 场景时 Unity iOS 崩溃

c# - 如何在场景之间保留对象的完整状态?