位置: 编程技术 - 正文

委托、事件、单例在Unity3D中的使用(事件委托jq)

编辑:rootadmin

推荐整理分享委托、事件、单例在Unity3D中的使用(事件委托jq),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:事件委托作用,什么是委托事件,什么叫事件委托,事件委托作用,事件委托jq,什么是委托?事件跟委托是什么关系?,委托和事件的区别(讲的很详细),什么是委托事件,内容如对您有帮助,希望把文章链接给更多的朋友!

原文 请点击这里 翻译:claudio

Here I demonstrate how to create delegates, events and singletons to work in conjunction. This tutorial is written for Unity3D, However, similar code can be used for any C# or .NET application.

在这里我将展示如何结合使用委托、事件以及单例。这个教程针对Unity3D所写,然而,相似的代码可以使用到任何C#或.Net程序中。

Why would I want to know this? As a young self-taught programmer, I often found myself writing tons and tons of boolean statements to determine if some event or action has happened. I listen to those events through Coroutines and other methods to return values. If you find yourself doing this as well, STOP IT!

为什么我想要知道这个? 作为一个自学编程的人,我发现自己在代码中使用了大量的布尔值来决定一些事件或者行为的发生。我监听协同程序(Coroutines)和其他方法来得到返回值。如果你发现自己也在做同样的事情,停止它!

Welcome to Events…

欢迎来到事件…

Intro Lately, I’ve been trying to improve on my C# programming skills and found myself lacking knowledge of the basic understand for Events. So, while looking through numerous tutorials on MSDN and other blogs, I found most tutorials to be over complicated and lush with convoluted code not pertinent to the core concept. I don’t want this to happen to you!

简介 最近我在试图提高自己的C#编程技能,发现自己对事件缺乏基本的了解。所以我阅读了很多MSDN的教程和其他博客,但我发现大多数都写的都十分繁琐并且包含了很多与核心内容不相干的代码。我不希望这些事也在你身上发生。

With that said I will try to explain the basics of Events and how they are used in a project…

正如所说的,我将试图解释事件的基础以及如何将它们应用到项目中。

Singleton? If you don’t know what a Singleton is, you probably should. A Singleton is a script that cannot be an Instanced or ‘Duplicated’. It is, well… Single.

单例? 或许你不知道单例。单例是一个不能被实例化或者“复制”的脚本,也就是说它是“唯一的”。

I recommend using Singletons for things that do not need to be copied multiple times during a game. Such as an Inventory System. Typically, the player only needs one Inventory, so we don’t want to call Instances of it, we only want one. And when we call it, we want to make sure it doesn’t get duplicated.

我建议在那些在游戏中不需要被复制很多次的东西上使用单例。例如目录系统。通常,角色只需要一个目录,我们不想去调用它的实例。当调用它时,我们希望确保它没有被多次复制。

There are many ways to create Singletons, but this method is often used because it’s simple…

有很多方式创造单例,下面是一种常用的方法,因为它比较简单…

Here, I have a class ‘Clicker’ that is attached to my Camera. This class handles all clicks that I send into 3D Space with a Raycast.

这里,我创建了一个名为‘Clicker’的类,并将它链接到摄像机。这个类用来控制三维场景中所有基于射线的点击。

To access my ‘DoSomething’ method from another script, I simply call…

通过在其他脚本中访问‘DoSomething’方法进行简单调用,我只需要简单的调用…

csharp code: Clicker.Instance.DoSomething();

This eliminates the need to use a bunch of Static Method and Variable calls, plus gives us one instance only!

这样就排除了需要创建大量静态方法和变量的情况,好处是只给了我们一个实例。

委托、事件、单例在Unity3D中的使用(事件委托jq)

Delegate and Event? A Delegate can be thought of as a reference pointer to an object/method. When it gets called, it notifies all methods that reference the delegate.

委托和事件? 委托可以想成是一个针对对象和方法的引用指针。当其被调用时,它会通知所有引用委托的方法。

So, first things first…

所以,第一件事首先…

Define a Delegate and the method that gets called when it fires…

定义一个委托和调用时的相应方法…

The delgate called ‘OnClickEvent’ passes a ‘GameObject’ that we can use to define what game object it came from. Then, we defined an ‘event’ OnClick that gets called when the delegate is called.

例子中创建了一个名为‘OnClickEvent’的委托,它包含了一个‘GameObject’类型的参数来获取对象信息。之后,我们定义一个名为‘OnClick‘的事件,当委托被调用时它将得到消息。

Now, in the same script, we need to call the delegate and pass it our GameObject. I’ve done this through a Raycast…

现在,在同一个脚本里,我们需要调用委托来传递给GameObject。我通过使用射线完成了这一步。

As you can see, if the Ray has contact an Object in the scene and we Left-Mouse-Click, we call the event and pass the GameObject.

正如你看到的,如果摄像在场景中接触到了物体,点击鼠标左键,我们调用了事件并且传递了GameObject。

The last thing we must do is reference the delegate from our other scripts that are listening to the call. For this I’ve created a class called ‘GoldPile’.

最后要做的是在其他脚本中引用委托来监听调用。所以我创建了一个名为’GoldPile‘的类。

In our Awake() method, we’ve defined our listening Event and assigned a local method that gets called ‘OnClick’. ‘OnClick’ does not need to be the same as our delegate method, but it can be.

在Awake()方法中,我们定义了正在监听的事件并且分配了一个名为‘OnClick’的本地方法。‘OnClick’不需要和委托方法(命名)一致,但实际上可以。

Note: In our previous post we added a Singleton to our Clicker class. This allows us to use Clicker.Instance

注意:之前,我们已经在Clicker类中加入了单例。这允许我们使用Clicker.Instance。

As you can see, we’ve also created the OnClick() method that passes our GameObject we clicked on.

正如你看到的,我们已经创建了OnClick()方法并将点击到的对象用作了参数。

Note: You must use if (g == gameObject), otherwise it will hide other instances of that method in the scene as well… This is why we pass the GameObject for reference!

注意:你必须使用 if (g == gameObject),否则它也将隐藏场景中的其他实例物体。这既是为什么我们需要为引用传递GameObject参数。

Now you are free to add this method to any other script in your game if needed. Don’t forget to define the method and delegate in your Awake().

现在你可以自由地添加这个方法到你游戏中的其他脚本。不要忘记在Awake()中定义方法和委托。

You can download the complete project for your reference HERE. Enjoy! :)

你可以在这里下载完成的工程文件。

Unity3d 基于物理渲染Physically-Based Rendering之最终篇 前情提要:讲求基本算法Unity3d基于物理渲染Physically-BasedRendering之specularBRDFplus篇Unity3d基于物理渲染Physically-BasedRendering之实现最后我们用fragmentshader实现

关于 Unity3D 网络请求的笔记 Unity脚本关于网络请求的方法有如下:publicWWW(stringurl,byte[]postData,Dictionarystring,stringheaders)publicWWW(stringurl,byte[]postData,Hashtableheaders)-----deprecatedpublicWWW(stringu

Unity3d导出Lightingmap的方法 前段时间遇到了这样的问题,美术人员在Unity中烘焙好了一个场景A中的部分模型,想导入另外一个场景B,经过各种搜索实验,找到了解决办法。但是前

标签: 事件委托jq

本文链接地址:https://www.jiuchutong.com/biancheng/383577.html 转载请保留说明!

上一篇:unity3d加密代码结束篇(unity游戏加密)

下一篇:Unity3d 基于物理渲染Physically-Based Rendering之最终篇(unity3d物理现象模拟)

  • 个税申报表中的基本养老保险怎么填
  • 发票勾选后什么时候可以抵扣税
  • 做网站的费用会计分录
  • 统一企业员工
  • 其他应收款可以在贷方吗
  • 汽车计提折旧年限及残值率
  • 股权转让企业所得税税率
  • 金税三期会查之前的票吗
  • 外购固定资产账务处理
  • 商场做促销
  • 预付账款属于什么账户
  • 提供劳务收取现金会计分录
  • 供应链公司的组织架构图
  • 银行美元账户调汇会计分录
  • 应收而未收的装修款如何做账务处理?
  • 实收资本退还
  • 劳务报酬代扣代缴个人所得税怎么做账
  • 开租金发票可以提前开的吗?
  • 建筑企业一般纳税人提供建筑服务属于老项目
  • 收到的专票都必须开吗
  • 递延所得税资产怎么计算
  • 小规模核定征收怎么交税
  • 出口报关和不报关的区别
  • 新公司值得入职吗
  • 营改增项目
  • 本期填写的适用3减1政策的本期发生额大于
  • 什么是债券持有人
  • cmos密码和bios密码
  • php匹配邮箱
  • 公司已开票给客户,但客户未打款怎么办?
  • 固定资产更新改造的账务处理
  • 银行企业未达账户怎么办
  • 只有使用权的房子能继承吗
  • 新建厂房费用应计入什么
  • 生育津贴有什么补贴
  • 如何用php制作网页
  • php使用redis缓存技术
  • js进阶视频教程
  • 财务的几张报表
  • 手把手教大家
  • 桥接模式例题
  • 小规模纳税人差额征收税率是多少
  • 企业破产员工补偿标准是按上年平均工资
  • 公司纳税信用等级B级是什么意思
  • 公司借个人借款协议书范本图片
  • 金蝶如何新增客户
  • 只有劳务报酬 能否扣5000
  • 补缴上年度所得税的会计分录
  • 应收账款和预收账款都是企业的流动资产
  • 个人向对公账户付款有风险吗
  • 固定资产售后回租融资租赁利息可以抵扣进项税额么
  • 销售完一定要结工资吗
  • 发票已开后 对方公司名称变更怎么处理?
  • mysql %d
  • Linux下MySQL 5.5/5.6的修改字符集编码为UTF8的方法
  • centos怎么调出终端
  • freebsd操作命令
  • 索尼vaio笔记本无法开机
  • win10系统打不开jpg图片
  • 双linux系统
  • fdreader.exe是什么程序
  • win7系统出现蓝屏怎么进去桌面
  • Win10 build 10240有"启用快速启动"功能吗?如何开启和关闭这个功能?
  • 在linux系统中,用来存放各种配置文件的目录
  • android中的active_result
  • Android---61---TabHost简单使用
  • 怎样用div css制作网页
  • css固定在底部
  • Android中SQLite数据库的使用
  • javascript代码写在哪个标签里
  • 安卓大作业小游戏五子棋
  • python3.8.3怎么用
  • python数据的概念
  • javascript基于什么的语言
  • 关联公司销售
  • 广东电子税务局电话
  • 在进口环节海关代征的税种有
  • 小规模纳税人专票开1%还是3%
  • 武汉税务局官网查询系统
  • 继承房产过户后可以要求分割吗?
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设