代码检查:构建设置中缺少索引
此检查会报告在 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日