- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
- 纳金币
- -1
- 精华
- 11
|
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 发表
|
|