位置: 编程技术 - 正文
推荐整理分享A Type-Safe Event System for Unity3D,希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!
转载自: Event Listener pattern is an extremely common design pattern. Using Events instead of method calls let an object communicate with another object (or many objects) without explicit knowledge of the other object. With events acting as an implicit interface between objects, we can write much more loosely coupled (thus more reusable) code.
Unity’s own message passing system can be leveraged to achieve this effect, but there are a few problems with it. First, sending messages is hierarchy-dependent. You either need a reference to the object you wish to send the message (event) to, or you need a reference to that object’s parent object. This is not loosely coupled. Secondly, it’s not statically type-safe.
There have been several solutions to this problem (for example, FlashBang’s messaging system or this one on the UnifyCommunity wiki). These still lack type-safety, and won’t quite do.
Here is my event system implementation. It looks quite a bit like the event system in AS3:
To use this thing, first we declare a GameEvent subclass. This event can carry with it all of the parameters needed by the objects listening for the event.
Registering to listen for the event looks like this:
And finally, to raise the event, do this:
The cool thing about this implementation is that it’s type-safe (listener registration errors will be caught at compile time, and no casting of events or event arguments) and it doesn’t require listening objects to implement a special interface or use Unity’s built-in message passing system.
The interface for this system is almost identical to that presented by Mike Mittleman at Unite , and I’d wager our implementations are similar. If you really want a rundown of the benefits and pitfalls of event-driven Unity development, I suggest watching his presentation on Unity’s website.
NGUI带位置图标的拖动例子 转载自:
Unity3D ugui事件监听机制 UGUI控件的事件响应有很多种方式,比如使用组件EventTrigger来添加事件监听,或者实现IDragHandler等接口,或者更直接地继承EventTrigger来进行更灵话的调用
Screen.sleepTimeout=SleepTimeOut.NeverSleep 禁止屏幕锁屏 Screen.
标签: A Type-Safe Event System for Unity3D
本文链接地址:https://www.jiuchutong.com/biancheng/369340.html 转载请保留说明!上一篇:UGUI 实现Button长按效果(RepeatButton)(ugui scrollview)
下一篇:NGUI带位置图标的拖动例子
友情链接: 武汉网站建设