查看: 5938|回复: 0
打印 上一主题 下一主题

[其他] 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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

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

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

GMT+8, 2024-5-21 07:26 , Processed in 0.084323 second(s), 33 queries .

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

© 2008-2019 Narkii Inc.

回顶部