C#事件

C#编程 · 01-02 · 251 人浏览

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class weituoevent : MonoBehaviour
{

public UnityEvent myevent1;
public UnityEvent<int> myevent2;
public void dowork1()
{
    print("无参事件");
}

public void dowork2(int a)
{
    print("有参事件" + a);
}


// Start is called before the first frame update
void Start()
{
   
    myevent1.AddListener(dowork1); //添加监听
    myevent1?.Invoke(); //有就调用
    myevent2.AddListener(dowork2);
    myevent2?.Invoke(10);
}

}

Theme Jasmine by Kent Liao