总结学习下委托和事件相关知识与用法
1. Delegate
//无参数无返回值
public delegate void testDelegate();
private testDelegate TestDelegate;
//有参数有返回值
public delegate bool testDelegate(int i);
private testDelegate TestDelegate;
void Start()
{
//TestDelegate = new testDelegate(TestFunction);
//TestDelegate += TestFunction;
TestDelegate = TestFunction;//赋值,这句话与上两句话是等价的
TestDelegate += TestFunction2;
TestDelegate();//直接调用
}
void TestFunction()
{
Debug.Log("TestFunction");
}
void TestFunction2()
{
Debug.Log("TestFunction2");
}
void Start()
{
TestDelegate = delegate (int i) {
return true;
};
TestDelegate();
}
void Start()
{
TestDelegate += (int i) => {
return true;
};
TestDelegate();
}
2. Action
Action:
public delegate void Action();
Action:
public delegate void Action (T1 arg1, T2 arg2);
public Action testAction;
void Start()
{
testAction+= (bool m) =>Debug.Log(m);
testAction(true);
}
3. Func
Func< TResult >:
public delegate TResult Func< TResult > ();
Func< T1,T2,TResult >:
public delegate TResult Func< T1, T2, TResult > (T1 arg1, T2 arg2);
public Func testFunc;
void Start()
{
testFunc += (int i) => { return i>0 } ;
testFunc(1);
}
4. Predicate
public Predicate tsetPredicate;
void Start()
{
tsetPredicate = (int i) => { return i>0 } ;
}
void StartPredicate()
{
int[] myNum = new int[8] { -12, 33, -89, 21, 15, -29, 40, 52 };
int[] myResult = Array.FindAll(myNum, tsetPredicate);
}
5. Event
public delegate void Delegate1(string str);
public Delegate1 D1;
public Delegate1 D2;
public event Delegate1 E;
void Start()
{
D1 += P1;
D1 += P2;
D2 += P3;
D2 += P4;
//单独委托添加方法,单独委托单独调用
//D1("1");
//D2("1");
//事件添加多个委托,通过事件调用
//E += D1;
//E += D2;
//E.Invoke("1");
//事件添加多个方法,通过事件调用
E += P1;
E += P2;
E += P3;
E += P4;
E.Invoke("1");
}
void P1(string s)
{
print(s + "!");
}
void P2(string s)
{
print(s + "!!!");
}
void P3(string s)
{
print(s + "-");
}
void P4(string s)
{
print(s + "---");
}
// 定义委托
public delegate void Delegate1(int i);
// 定义事件订阅者
public class Subscriber {
public void P1(int i) {
print(i);
}
}
// 定义事件发布者
public class Publishser {
private int count;
public Delegate1 D1; // 声明委托变量
//public event Delegate1 E1; // 声明一个事件
public void DoSomething() {
if (D1!= null) { // 触发事件
count++;
D1(count);
}
}
}
class Program {
static void Main(string[] args) {
Publishser pub = new Publishser();
Subscriber sub = new Subscriber();
pub.D1 += new Delegate1(sub.P1);
//按照规范应该这样触发事件
pub.DoSomething();
//但是也可以这样来触发事件,这样不是很恰当
pub.D1(100);
}
}
6. UnityAction && UnityEvent
public class Test{
public UnityEvent myEvent = new UnityEvent();
}
//使用Serializable序列化IdolEvent, 否则无法在Editor中显示
[System.Serializable]
public class MyEvent: UnityEvent {}
public class Test{
public MyEvent myEvent = new MyEvent();
}