using System;
using UnityEngine;
public class Project_Delegate_Func : MonoBehaviour
{
//委託類型 【Func】範例
//Action<> 支持 1~16個參數的方法
//Func 支持 1~16個參數的方法 每個方法都需要有一個返回值
Func<int, int, int> func1;
int Result;
void Start()
{
// // 將方法與委託進行綁定
// // func1 = Test;
// func1 += Test;
// func1 += Test2;
// // 委託調度時 實際就是調度綁定的方法
// Result = func1(100,800);
// Debug.Log(Result);
// //取消委託的註冊,移除綁定的方法
// func1 -= Test;
// // Result = func1(100,800);//無法執行,func1 為空
//當作方法的參數進行傳遞
Debug.Log(Test3(Test2,200,300));
}
public int Test(int a, int b){
return a+b;
}
public int Test2(int a, int b){
return a-b;
}
public int Test3(Func<int,int,int> callback)
{
if(callback!= null){
callback(500,100);
}
return 0;
}
public int Test3(Func<int,int,int> callback, int a,int b)
{
Debug.Log("Test3......");
if(callback!= null){
return callback(a,b);
}
return 0;
}
}