委托是一个类,它定义了方法的类型和参数。先将方法动态的赋给委托的对象,在使用时,通过这个对象完成方法的调用。
unity代码
using UnityEngine;
using System.Collections;
public class CCall : MonoBehaviour {
public delegate int Call (int num1, int num2);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
if(GUI.Button(new Rect(0, 0, 200, 120),"DO"))
{
Call objCall;
SimpleMath objMath = new SimpleMath ();
objCall = new Call (objMath.Multiply);
objCall += objMath.Mus;
objCall += objMath.Divide;
int result = objCall (5, 3);
Debug.Log (result);
}
}
public class SimpleMath
{
public int Multiply (int num1, int num2)
{
Debug.Log("Multiply");
return num1 * num2;
}
public int Divide(int num1, int num2)
{
Debug.Log("Divide");
return num1 / num2;
}
public int Mus(int num1, int num2)
{
Debug.Log("Mus");
return num1 - num2;
}
}
}