代码检查:字符串字面量中引用的方法没有预期的签名
此检查会报告字符串形式引用 Unity 方法但其签名不正确的情况。
通常发生在如 Invoke、 InvokeRepeating、 StartCoroutine、 CancelInvoke 及类似需要以字符串形式传递方法名的 Unity API 中。 该方法存在,但其参数、返回类型或静态/实例用法与 Unity 要求的不符。
示例
在此示例中, InvokeRepeating 期望一个无参数的实例方法,但 LaunchProjectile 声明时带有 int 参数。
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
InvokeRepeating("LaunchProjectile", 1f, 1f);
}
// Reported: InvokeRepeating expects a parameterless instance method.
private void LaunchProjectile(int count)
{
}
}
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
InvokeRepeating("LaunchProjectile", 1f, 1f);
}
private void LaunchProjectile()
{
}
}
快速修复
此检查不会直接为字符串字面量提供专用快速修复。 需要通过修改被引用方法的签名以符合 Unity 的要求来修正代码。
2026年 5月 8日