当前位置: 代码迷 >> 综合 >> unity 委托delegate的理解
  详细解决方案

unity 委托delegate的理解

热度:44   发布时间:2024-01-26 08:35:52.0

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

直接上代码

public class delegateTest : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){//用法1GoodMorning(GoodMorningChinese);//用法2m = GoodMorningEnglish;m();}delegate void MorningLanguage();//声明一个委托 (类似函数指针,指向参数为空,返回值为void的函数)MorningLanguage m; //实例化一个委托的对象void GoodMorning(MorningLanguage ml)//{ml();//参数是空的}void GoodMorningChinese()//某一个参数为空,返回值为void的函数{Debug.Log("早上好");}void GoodMorningEnglish(){Debug.Log("good morning");}
}

 

  相关解决方案