unity 委托delegate的理解

unity中的委托类似于c/c++中的函数指针

直接上代码

public class delegateTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //用法1
        GoodMorning(GoodMorningChinese);

        //用法2
        m = GoodMorningEnglish;
        m();
    }

    delegate void MorningLanguage();//声明一个委托 (类似函数指针,指向参数为空,返回值为void的函数)
    MorningLanguage m; //实例化一个委托的对象
    void GoodMorning(MorningLanguage ml)//
    {
        ml();//参数是空的
    }
    void GoodMorningChinese()//某一个参数为空,返回值为void的函数
    {
        Debug.Log("早上好");
    }
    void GoodMorningEnglish()
    {
        Debug.Log("good morning");
    }
}
发布了12 篇原创文章 · 获赞 2 · 访问量 3329

猜你喜欢

转载自blog.csdn.net/qq_41642615/article/details/104043817