在使用Unity的GetMethod方法获取方法时,如果有多个重载,可以通过指定方法的参数类型来获取特定的重载方法。如果想获取没有参数的重载方法,可以使用Type.EmptyTypes作为参数类型的参数。以下是一个示例代码:

using UnityEngine;
using System;
using System.Reflection;

public class MyClass : MonoBehaviour
{
    private void MyMethod()
    {
        Debug.Log("No parameter method");
    }

    private void MyMethod(int value)
    {
        Debug.Log("Parameter method");
    }

    private void Start()
    {
        Type type = typeof(MyClass);
        MethodInfo method = type.GetMethod("MyMethod", Type.EmptyTypes);
        method.Invoke(this, null);
    }
}

在上面的示例中,MyClass类中有两个重载的MyMethod方法,一个没有参数,一个有一个int类型的参数。在Start方法中,使用Type.EmptyTypes作为参数类型的参数,可以获取没有参数的MyMethod方法。然后使用MethodInfo的Invoke方法调用该方法

unity 使用 GetMethod获取方法的时候 有多个重载想获取没有参数的那个

原文地址: https://www.cveoy.top/t/topic/iGAg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录