注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Faye Euler's blog

技術交流歡迎留言

 
 
 

日志

 
 

Unity3d EZGUI 輸入事件 Using Input delegates  

2011-09-16 12:11:16|  分类: Unity3D |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Input delegates are useful for when you want to know about every little bit of pointer input that is sent to your control. This allows you not only to respond to each kind of input immediately, but also allows you to override and micro-manage the control's behavior.

A delegate is a method/function that gets called by another piece of code. You define a delegate function/method and then tell another object to call it under certain circumstances. In this case, we want a control to call our function when it receives pointer input. Since the calling object needs to send certain information to, or expects to receive certain information back from the delegate, the delegate must follow a certain pattern. In the case of EZ GUI input delegates, that pattern looks like this:

 
void MyDelegate(ref POINTER_INFO ptr)


In other words, it returns no value (void), and accepts a single argument: a POINTER_INFO struct which contains the information about the pointer interacting with the control.

First, we'll define our delegate somewhere in one of our scripts. NOTE: Because of limitations in JavaScript/UnityScript, input delegates must be written in C#.

 
void MyDelegate(ref POINTER_INFO ptr)
{
// Display a message in the console if
// the pointer is dragged over the control:
if(ptr.evt == POINTER_INFO.INPUT_EVENT.DRAG)
Debug.Log("Dragged!");
}


The above code checks to see if the pointer event (.evt) is a drag event. Now we must tell our control about this delegate for it to be called:

 
void Start()
{
// Get a reference to our control script:
IUIObject control = (IUIObject) GetComponent(typeof(IUIObject));

// Tell the control to call our method when it receives input:
control.AddInputDelegate(MyDelegate);
}


That's all there is to it.

No suppose we have a button and want it not to enter the "OVER" state by ignoring the MOVE event. We can "ignore" certain events by fooling the control into thinking the event is something other than what it is, simply by changing the value stored in the POINTER_INFO variable, like so:

 
void MyDelegate(ref POINTER_INFO ptr)
{
if(ptr.evt == POINTER_INFO.INPUT_EVENT.MOVE)
ptr.evt = POINTER_INFO.INPUT_EVENT.NO_CHANGE;
}


Here we've changed the event to a "NO_CHANGE" event, which will be be ignored by the button.

There is almost no limit to how you can extend the functionality or behavior of EZ GUI controls using input delegates.
  评论这张
 
阅读(1502)| 评论(2)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017