纳金网

标题: Flash-esque Event System for Unity,flash事件与unity3d事件的比较(一) [打印本页]

作者: 会飞的鱼    时间: 2011-12-20 14:51
标题: Flash-esque Event System for Unity,flash事件与unity3d事件的比较(一)
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  发表


作者: 难骑    时间: 2011-12-30 18:50
提示: 作者被禁止或删除 内容自动屏蔽
作者: 晃晃    时间: 2012-2-22 23:25
呵呵,很好,方便罗。

作者: 菜刀吻电线    时间: 2012-4-21 23:19
长了不少见识

作者: C.R.CAN    时间: 2012-8-13 11:43
不错哦,谢谢楼主

作者: 奇    时间: 2012-8-22 00:18
很有心,部分已收录自用,谢谢

作者: 奇    时间: 2012-8-28 23:18
水……生命之源……灌……

作者: C.R.CAN    时间: 2012-10-2 23:21
我是老实人,我来也!

作者: 奇    时间: 2012-10-5 23:24
不错哦,谢谢楼主

作者: C.R.CAN    时间: 2012-10-15 23:24
好可爱的字,学习了

作者: 菜刀吻电线    时间: 2013-2-9 23:19
路过、路过、快到鸟,列位请继续...ing

作者: hcjamm    时间: 2015-1-4 06:25
flash事件,学习了




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5