【Unity3D自学记录】Unity3D代理委托模式

using UnityEngine;
using System.Collections;

public class DJH_Delegate : MonoBehaviour
{

    public delegate void Delegate1();
    public Delegate1 OutTest1;

    public delegate void Delegate2(int index);
    public Delegate2 OutTest2;

    int index = 12345;
    void Start()
    {
        OutTest1 = DebugLog1;
        OutTest2 = DebugLog2;
    }
    void OnGUI()
    {
        //开始按钮  
        if (GUI.Button(new Rect(0, 10, 100, 30), "Button1"))
        {
            if (OutTest1 != null)//无参数
            {
                DebugLog1();
            }
        }
        if (GUI.Button(new Rect(0, 60, 100, 30), "Button2"))
        {
            if (OutTest2 != null)//有参数
            {
                DebugLog2(index);
            }
        }
    }

    void DebugLog1()
    {
        Debug.Log("Button1");
    }

    void DebugLog2(int index)
    {
        Debug.Log("Button2参数:" + index);
    }
}


你可能感兴趣的:(unity3d)