【Unity】Delegate, Event, UnityEvent, Action, UnityAction, Func 傻傻分不清

【Unity】Delegate, Event, UnityEvent, Action, UnityAction, Func 傻傻分不清

  • Delegate 委托,函数指针
    • 一个简单的例子:一对一依赖
    • 一个简单的例子:一对多依赖
    • 所以话说……委托有啥用呢?
  • 事件 Event,特殊的委托
  • UnityEvent
  • Action,一个委托
  • UnityAction,一个委托
  • Func,带返回值的 Action
  • 使用匿名函数 / Lambda 来监听回调函数
  • 学习打开别人一个魔塔的项目,看到了满页的 Action 代码,而本人委托那一块自己写的时候压根不会用……遂学习相关知识。
  • 多数学习自知乎
  • 本文可能会有知识点错误,欢迎讨论。

Delegate 委托,函数指针

  • 首先,Delegate是C#的内容,简单来说委托是一种回调机制,被广泛应用在观察者模式中。

    回调机制貌似挺复杂,这里可以简单理解为允许使用回调函数,而在这里的回调函数可以简单理解为函数指针。

    观察者模式没有学习过的可以看其他的博客。

一个简单的例子:一对一依赖

using System.Collections;using System.Collections.Generic;using UnityEngine;public class TestMyDelegate : MonoBehaviour{    public delegate void DeleFunc(int x);    // Start is called before the first frame update    void Start()    {        DeleFunc deleFunc;        deleFunc = show;        deleFunc(10);        deleFunc(8);        deleFunc = doubleShow;        deleFunc(10);    }    void show(int x)    {        Debug.Log(x);    }    void doubleShow(int x)    {        Debug.Log(x * 2);    }}
  • 我们声明了一个委托DeleFunc(就如一个函数指针),使用到关键字 delegate

    然后,这个委托我们只告诉了它的形参和返回类型。我们需要实例化,实例化了 deleFunc

    然后,我们声明一个新的函数 show,注意这里形参和返回值需要和委托的一致

    然后,我们把 deleFunc 指向 show,进行方法回调

    然后,我们申明了一个新的函数 doubleShow ,同理,进行方法回调

    进行测试,正常运行。

    在这里插入图片描述

一个简单的例子:一对多依赖

  • 我们使用 += 为同一个委托监听多个方法回调

    对应的,使用 -= 删除一个监听方法

using System.Collections;using System.Collections.Generic;using UnityEngine;public class TestMyDelegate : MonoBehaviour{    public delegate void DeleFunc(int x);    // Start is called before the first frame update    public DeleFunc deleFunc;    void Start()    {        deleFunc += show;        deleFunc += doubleShow;        deleFunc(5);        deleFunc -= doubleShow;        deleFunc(5);    }    void show(int x)    {        Debug.Log(x);    }    void doubleShow(int x)    {        Debug.Log(x * 2);    }}

在这里插入图片描述

  • 注意,不能写成
	public delegate void DeleFunc(int x);
    // Start is called before the first frame update
    void Start()
    {
        DeleFunc deleFunc;	// 不能写到里面来,会报错 [使用了未赋值的局部变量“deleFunc”]
        deleFunc += show;
        deleFunc += doubleShow;
        deleFunc(5);
    }

所以话说……委托有啥用呢?

  • 委托不是必须用的,它的产生是随着OO,设计模式等产生的,用于代码解耦。
  • 场景一:你有很多种行为,比如 eat(),drink(),sleep(),行为越来越多,相应的调用代码也越来越长
class Service{
	void eat(){// something}
	void drink(){// something}
	void sleep(){// something}
	public void service(string name){
		switch (name){
			case "eat":eat();break;
			case "drink":drink();break;
			case "sleep":sleep();break;
		}
	}
}

若使用委托进行封装,便可以

class Service{
	void eat(){// something}
	void drink(){// something}
	void sleep(){// something}
	public delegate void action();
	public void service(action act){
		act();
	}
}

调用的时候,这样:

	action act = eat();
	Service.service(act);
	Service.service(cook);
  • 场景二:代码解耦

    一个游戏,玩家死亡后会调用函数 GameOver(),但我目前还不知道该函数里面还需要一些代码和方法

    比如,我们可能会如下实现

public void GameOver(){	GamePause();	PlaySound("death");	ClearFlags();	AddDeathCount(1);	ShowGameOverPanel();	// ……}
  • 然后这里面的每个方法又有很多代码实现。若发现死亡后的播放音效需要更改,还需要去这个代码里面单独修改。若增加了一个功能,需要在这个函数内调用,还要跑到这个函数里进行增添代码,十分麻烦,难以维护。
  • 一个做法:使用委托,即:
	public delegate void GameOver();	public GameOver gameOver;

接下来,比如在音效系统的代码中,为其增添回调函数

class VoiceManager{
	// ……
	public void addSounds(){
		gameOver += PlaySound("death");
	}
	public void PlaySound(string name){
		// ……
	}
}

其他系统同理。这样,对于需要更改音效的地方,就集中统一管理到了相应的类。

但注意,这里的 gameOver 的委托实例获取方式仍然有些耦合。需要更解耦貌似还需要使用后续提到其他的内容。

事件 Event,特殊的委托

  • 仍然,事件是 C# 的内容,并且事件是一种特殊的委托

    怎么理解呢?先看代码

    这里有两个类,一个为 TestMyDelegate ,代码同上述

    一个为 AnotherDelegate ,相对第一个类,为一个外部类(这里指委托没有声明在该类)

using System.Collections;using System.Collections.Generic;using UnityEngine;public class TestMyDelegate : MonoBehaviour{    public delegate void DeleFunc(int x);    // Start is called before the first frame update    public DeleFunc deleFunc;    void Start()    {        deleFunc += show;        deleFunc += doubleShow;        deleFunc(1);    }    void show(int x)    {        Debug.Log(x);    }    void doubleShow(int x)    {        Debug.Log(x * 2);    }}/*****************************************/using System.Collections;using System.Collections.Generic;using UnityEngine;public class AnotherDelegate : MonoBehaviour{    void Start()    {        TestMyDelegate test = GetComponent();        test.deleFunc += myfunc;        test.deleFunc(100);    }    void myfunc(int x)    {        Debug.Log(x * 3);    }}
  • 进行测试,结果如下:

    在这里插入图片描述

  • 很明显,首先外部类为其增加监听器,然后先输出了 300

    然后内部类增加了两个监听器,并相应输出了三个数字

  • 接下来,我们在内部类,把委托的实例增加 event 关键字,改成事件

    public event DeleFunc deleFunc;

    然后发现外部类报错了

    在这里插入图片描述

    没错,事件相较于委托,即事件只能在创建类中进行调用

    外部类可以对事件进行增加、删除监听器,但是不能使用 = 。等于的功能即令该委托/事件只监听这个回调函数。

    在这里插入图片描述

UnityEvent

  • 自然,该为 Unity 中的内容,为 Unity 做的一个小封装。

    我们在代码中,首先引入头文件 UnityEngine.Events,然后写一下 UnityEvent,然后 F12 查看源代码

    在这里插入图片描述

  • 哦,我们发现,UnityEvent 相较于委托,它规范化了 +=和-=,以 OO 的方式改为了 AdListener(UnityAction call) 和 RemoveListener(UnityAction call),除此之外好处是更容易调试debug,编辑器可视化等。

    还有一点,由于委托是多播设计,可能会导致重复添加同一监听器。这里 UnityEvent 与其他系统 (如 UnityAction, EventSystem 等)结合,更加方便。

  • 我们试一下代码

    这里使用了三个 UnityEvent 事件,分别监听了无参函数、一参函数和三个参数函数

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.Events;public class TestUnityEvent : MonoBehaviour{    public UnityEvent myEvent;    public UnityEvent anotherEvent;    public UnityEvent alsoEvent;    // Start is called before the first frame update    void Start()    {        myEvent.AddListener(noArg);        myEvent.Invoke();        anotherEvent.AddListener(show);        anotherEvent.Invoke(5);        alsoEvent.AddListener(threeArgs);        alsoEvent.Invoke(1, 2, 3);    }    void show(int x)    {        Debug.Log(x);    }    void noArg()    {        Debug.Log("No Arg");    }    void threeArgs(int a, int b, int c)    {        Debug.Log(a + " " + b + " " + c);    }}

顺利运行

在这里插入图片描述

Action,一个委托

  • 首先,Action 为 C# 在 System 库中的内容

    我们引入该头文件,然后输入 Action,F12 查看原码

    在这里插入图片描述

  • 额,好简单,所以 Action 就是一个委托。

    若你输入 Action 再 F12 进去看,则会显示

    在这里插入图片描述

  • 所以,若你自己写代码 public delegate void myAction(T obj); ,那么该 myAction 和 Action 就没有区别。

    那么,该测试部分和之前的委托就没什么差异了

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.Events;using System;public class TestAction : MonoBehaviour{    public Action myAction;    // Start is called before the first frame update    void Start()    {        myAction = show;        myAction += show;        myAction(100);    }    void show(int x)    {        Debug.Log(x);    }}

UnityAction,一个委托

  • 在看这个,这个明显就是 Unity 中的一个 Action

    头文件在 UnityEngine.Events中,我们进入查看原码

    在这里插入图片描述

    在这里插入图片描述

  • 呃呃呃,你们都那么简单,好吧。

    那对比,Action 和 UnityAction 只有头文件不同的区别了,其他的部分都一样啊。

Func,带返回值的 Action

  • 该内容在系统库 System,我们查看原码

    在这里插入图片描述

  • 沃耶,对比 Action,Func 即多了一个返回值的地方,原来他们都这么简单…

    好吧确实,因为它的定义即如此,有时候自己实现一个内容还能有更多的功能…

    我们照常测试一下

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.Events;using System;public class TestFunc : MonoBehaviour{    public Func func;	// 这里的 int 就是返回类型    // Start is called before the first frame update    void Start()    {        func += show;        Debug.Log(func("Hello World"));    }    int show(string x)    {        Debug.Log(x);        return 12;    }}

完美实现

]

使用匿名函数 / Lambda 来监听回调函数

  • 除了上述声明函数并直接给委托监听外,也可以用匿名函数和 lambda表达式来进行处理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class TestOtherMethods : MonoBehaviour
{
    public UnityAction myAction;
    // Start is called before the first frame update
    void Start()
    {
        // anony-
        myAction = delegate (int x, int y)
        {
            Debug.Log(x + " " + y);
        };
        myAction(1, 2);

        // lambda
        myAction = (int x, int y) =>
        {
            Debug.Log(x + " " + y);
        };
        myAction(3, 4);
    }

}
  • 学了这么多了,试了这么多例子了,大致也应该了解各个内容了

    在项目开发中,具体用到哪种其实都可以,虽然有人是推荐 UnityEvent,有人说 Action/Func 直接用,之类的。

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/acd2cac5c0.html