委托事件理解

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

public class DelegateTest : MonoBehaviour {

    public delegate void DelegatePlayer();//声明委托
    public delegate void CallNumDelegate(int num);

    public static event DelegatePlayer playerEvent;//声明事件
    public static event CallNumDelegate playerEvent2;

	// Use this for initialization
	void Start () {
        playerEvent();
        playerEvent2(4);
    }
	
	// Update is called once per frame
	void Update () {
		
	}

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DelegateTest2 : MonoBehaviour {

    void Awake() {
        DelegateTest.playerEvent += this.test1;//注册事件
        DelegateTest.playerEvent2 += this.test2;
    }
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    void test1() {
        Debug.Log(3);
    }

    void test2(int x) {

        Debug.Log(x);
    }
}

猜你喜欢

转载自blog.csdn.net/hemiaoyuan1989/article/details/54926873