12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 5887|回复: 11
打印 上一主题 下一主题

[其他] Flash-esque Event System for Unity,flash事件与unity3d事件的比较(一)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-12-20 14:51:51 |只看该作者 |倒序浏览
Flash-esque Event System for Unity,flash事件与unity3d事件的比较
看来由flash转向unity3d的朋友真的比较多,老外写的一篇关于二者事件系统的教程。
Lets see some hands – how many Unity users are from the Flash camp originally?
..That’s what I thought.
One of the things I constantly hear about from people making the jump from Flash is Unity’s lack-there-of event system, like the one we have in Flash. Sure, we can use SendMessage() or produce some custom delegates and assign them… but it isn’t as hassle free as a simple call like this:
Add the listener:
myObject.addEventListener(Event.COMPLETE, OnComplete);

Dispatch the event:
dispatchEvent(new Event(Event.COMPLETE));

And when you’re done:
myObject.removeEventListener(Event.COMPLETE, OnComplete);

I’ll admit, it’s handy.
Well I’ve been doing some playing around and I’ve got what appears to be a working port of the AS3 style event handler system to Unity C#. I’m going to test it with a more robust situation to make sure everything has been accounted for before I release it on the Wiki, just to insure that you have the best working product I can offer.
For now, this is what we’ve got:






Our scene, beautiful – no?







Hierarchy panel

Now for the scripts:
The cube – Note it extends an EQEQBehaviour – *not* MonoBehaviour
using UnityEngine; using System.Collections; public class MyCube : EQEQBehaviour { // Use this for initialization void Start () { StartCoroutine(MyCoroutine()); } IEnumerator MyCoroutine() { yield return new WaitForSeconds(3); dispatchEvent(new EQEQEvent(EQEQEvent.COMPLETE,new Object())); yield return new WaitForSeconds(3); dispatchEvent(new EQEQEvent(EQEQEvent.COMPLETE,new Object())); } }

The coroutine above is just so that I can see stuff happen procedurally.. it isn’t necessary to the event system.
You might also see a second parameter in the constructor of that EQEQEvent, don’t worry about that… I’ve set it up where you will be able to extend and build custom events just like in AS3. I just have that extra parameter so that we can pass some other default value by default.
And now the sphere – Also a derivative of EQEQBehaviour.
using UnityEngine; using System.Collections; public class MySphere : EQEQBehaviour { public MyCube myCube; void Awake() { myCube.addEventListener(EQEQEvent.COMPLETE, OnComplete); } public void OnComplete(EQEQEvent e) { print("event with type: " + e.type + " was received."); myCube.removeEventListener(EQEQEvent.COMPLETE, OnComplete); } }

This looks eerily familiar to Flash, no? It’s supposed to.
Well when I run this, our output looks like this:






Our console output



Okay so everything happens just as expected, since we remove the listener after the first time it receives the event, it should only fire once. But wait, what is this “No listeners with associated type: “complete” found.”? You’ll need not worry, that is just a print statement that is fired on dispatchEvent() when there are no listeners for that particular event type.
Okay, so that is what I have so far. I plan on doing a more robust test and running it through the new profiler this weekend. I will be sure to post up everything shortly after I am sure everything is copacetic.



由 uke  发表

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

1274

主题

1

听众

4万

积分

禁止发言

纳金币
43676
精华
4
沙发
发表于 2011-12-30 18:50:22 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

板凳
发表于 2012-2-22 23:25:29 |只看该作者
呵呵,很好,方便罗。
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-4-21 23:19:16 |只看该作者
长了不少见识
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-8-13 11:43:56 |只看该作者
不错哦,谢谢楼主
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-8-22 00:18:17 |只看该作者
很有心,部分已收录自用,谢谢
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-8-28 23:18:48 |只看该作者
水……生命之源……灌……
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-10-2 23:21:20 |只看该作者
我是老实人,我来也!
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-10-5 23:24:02 |只看该作者
不错哦,谢谢楼主
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2012-10-15 23:24:43 |只看该作者
好可爱的字,学习了
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-27 17:39 , Processed in 0.090859 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部