当前位置:网站首页>A simple example of delegate usage

A simple example of delegate usage

2022-07-07 10:48:00 HCC2017

public class TextDelegate2{


    public delegate void LogDelegate(string log);   // Definition The delegate name is LogDelegate, With a string Parametric Delegate type  


    public static LogDelegate LogEvent;             // Declare the delegate object , The delegate instance is LogEvent    


    public static void OnLogEvent(string log)    
    {
        if (LogEvent != null)
        {
            LogEvent(log);
        }
    }  


}


/// <summary>
/// Add call event , The delegate
/// </summary>
class TextDelegate : MonoBehaviour
{
    void Start()
    {


        TextDelegate2.LogEvent += Cat;
        TextDelegate2.LogEvent += Dog;


        TextDelegate2.OnLogEvent(" Feed the small animals ");// Send callback information to the function
    }


    void Cat(string log)
    {
        Debug.Log(" cat :" + log);
    }


    void Dog(string log)
    {
        Debug.Log(" Wang Wang Wang :" + log);
    }  
}
原网站

版权声明
本文为[HCC2017]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130622492791.html