代码检查:构建设置中缺少索引
此检查会报告使用不在 Unity 构建设置场景列表范围内的数字构建索引进行场景加载的调用。
此检查适用于像 SceneManager.LoadScene 这样按场景索引加载场景的 API 以及旧的基于索引的加载方式。 如果索引过大,Unity 在运行时无法加载该场景。
示例
在此示例中,使用了索引 42 ,这在大多数项目中很可能不是有效的构建索引。
using UnityEngine.SceneManagement;
public class Example
{
public void OpenScene()
{
// Reported: 42 is likely an invalid build index
SceneManager.LoadScene(42);
}
}
using UnityEngine.SceneManagement;
public class Example
{
public void OpenScene()
{
// 1 is a valid build index
SceneManager.LoadScene(1);
}
}
快速修复
此检查不提供专用的快速修复。 请手动使用有效的构建索引修复调用,或切换为按场景名称加载。
2026年 5月 8日