草庐IT

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

溢流眼泪 2023-05-22 原文

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

  • 学习打开别人一个魔塔的项目,看到了满页的 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<TestMyDelegate>();
        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<int> anotherEvent;
    public UnityEvent<int, int, int> 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,一个委托

  • 首先,ActionC#System 库中的内容
    我们引入该头文件,然后输入 ActionF12 查看原码
  • 额,好简单,所以 Action 就是一个委托。
    若你输入 Action<>F12 进去看,则会显示
  • 所以,若你自己写代码 public delegate void myAction<in T>(T obj); ,那么该 myActionAction 就没有区别。
    那么,该测试部分和之前的委托就没什么差异了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System;
public class TestAction : MonoBehaviour
{
    public Action<int> 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中,我们进入查看原码

  • 呃呃呃,你们都那么简单,好吧。
    那对比,ActionUnityAction 只有头文件不同的区别了,其他的部分都一样啊。

Func,带返回值的 Action

  • 该内容在系统库 System,我们查看原码
  • 沃耶,对比 ActionFunc 即多了一个返回值的地方,原来他们都这么简单…
    好吧确实,因为它的定义即如此,有时候自己实现一个内容还能有更多的功能…
    我们照常测试一下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using System;
public class TestFunc : MonoBehaviour
{
    public Func<string, int> 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<int, int> 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 直接用,之类的。

有关【Unity】Delegate, Event, UnityEvent, Action, UnityAction, Func 傻傻分不清的更多相关文章

  1. ruby - 在 Ruby 中实现 `call_user_func_array` - 2

    我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)

  2. ruby-on-rails - rails : How to make a form post to another controller action - 2

    我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak

  3. ruby-on-rails - 如何在 Rails Controller Action 上触发 Facebook 像素 - 2

    我有一个ruby​​onrails应用程序。我按照facebook的说明添加了一个像素。但是,要跟踪转化,Facebook要求您将页面置于达到预期结果时出现的转化中。即,如果我想显示客户已注册,我会将您注册后转到的页面作为成功对象进行跟踪。我的问题是,当客户注册时,在我的应用程序中没有登陆页面。该应用程序将用户带回主页。它在主页上显示了一条消息,所以我想看看是否有一种方法可以跟踪来自Controller操作而不是实际页面的转化。我需要计数的Action没有页面,它们是ControllerAction。是否有任何人都知道的关于如何执行此操作的gem、文档或最佳实践?这是进入布局文件的像素

  4. Unity 热更新技术 | (三) Lua语言基本介绍及下载安装 - 2

    ?博客主页:https://xiaoy.blog.csdn.net?本文由呆呆敲代码的小Y原创,首发于CSDN??学习专栏推荐:Unity系统学习专栏?游戏制作专栏推荐:游戏制作?Unity实战100例专栏推荐:Unity实战100例教程?欢迎点赞?收藏⭐留言?如有错误敬请指正!?未来很长,值得我们全力奔赴更美好的生活✨------------------❤️分割线❤️-------------------------

  5. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  6. unity---接入Admob - 2

    目录1.AdmobSDK下载地址2.将下载好的unityPackagesdk导入到unity里​编辑 3.解析依赖到项目中

  7. Unity 3D 制作开关门动画,旋转门制作,推拉门制作,门把手动画制作 - 2

    Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u

  8. ruby-on-rails - 优雅的 Rails : multiple routes, 相同的 Controller Action - 2

    让多条路线去同一条路的最优雅的方式是什么ControllerAction?我有:get'dashboard',to:'dashboard#index'get'dashboard/pending',to:'dashboard#index'get'dashboard/live',to:'dashboard#index'get'dashboard/sold',to:'dashboard#index'这很丑陋。有什么“更优雅”的建议吗?一个类轮的奖励积分。 最佳答案 为什么不只有一个路由和一个Controller操作,并根据传递给它的参数来

  9. ruby - 将属性方法委托(delegate)给父对象 - 2

    我有以下类(class):classAlphabetattr_reader:letter_freqs,:statistic_letterdefinitialize(lang)@lang=langcaselangwhen:en@alphabet=('A'..'Z').to_a@letter_freqs={...}when:ru@alphabet=('А'..'Я').to_a.insert(6,'Ё')@letter_freqs={...}...end@statistic_letter=@letter_freqs.max_by{|k,v|v}[0]endendfoo=Alphabet.n

  10. ruby-on-rails - Rails Rspec 测试 Controller 新 Action - 2

    我正在尝试在我的Controller中测试新操作。目前它看起来像这样:Controllerdefnew@business=Business.new@business.addresses.buildend规范describe'GET#new'doit'assignsanewbusinessto@business'doget:newexpect(assigns(:business)).tobe_a_new(Business)endend我想测试“@business.addresses.build”这一行。我该怎么做?提前致谢! 最佳答案

随机推荐