前言
输入事件的相应机制,一般有两种: 1.继承型 2.绑定型
一、继承型
继承型是指事件先响应到基类,再由基类反馈到父类,由父类做处理,这样UI既可以得到对事件的响应,也可以自行修改自己需要的逻辑。
二、绑定型
绑定型是指在对事件响应之前,为UI元素绑定一个事件响应的组件。例如:编写一个绑定型事件类UIEvent,当某个UI元素需要输入事件的回调时,对这个物体绑定一个UIEvent,并且对UIEvent里需要的相关响应事件进行赋值或注册操作函数。 代码如下(示例):
public class UIEvent:EventTrigger
{
protected const float CLICK_INTERVAL_TIME = 0.2f;
protected const float CLICK_INTERVAL_POS = 2;
public delegate void PointerEventDelegate(PointerEventData eventData,UIEvent ev);
public delegate void BaseEventDelegate(BaseEventData eventData,UIEvent ev);
public delegate void AxisEventDelegate(AxisEventData eventData,UIEvent ev);
public Dictionary<string, object> mArgs = new Dictionary<string, object>();
public BaseEventDelegate onSelect = null;
public BaseEventDelegate onDeselect = null;
public BaseEventDelegate onUpdateSelect = null;
public BaseEventDelegate onCancel = null;
public BaseEventDelegate onSubmit = null;
public PointerEventDelegate onBeginDrag = null;
public PointerEventDelegate onDrag = null;
public PointerEventDelegate onEndDrag = null;
public PointerEventDelegate onDrop = null;
public PointerEventDelegate onClick = null;
public PointerEventDelegate onDown = null;
public PointerEventDelegate onEnter = null;
public PointerEventDelegate onExit = null;
public PointerEventDelegate onUp = null;
public PointerEventDelegate onScroll = null;
public PointerEventDelegate onInitializePotentialDrag = null;
public AxisEventDelegate onMove = null;
private static PointerEventData mPointData = null;
public void SetData(string key,object val)
{
mArgs[key] = val;
}
public T GetData<T>(string key)
{
if (mArgs.ContainsKey(key))
{
return (T)mArgs[key];
}
return default(T);
}
public static UIEvent Get(GameObject go)
{
UIEvent listener = go.GetComponent<UIEvent>();
if (listener == null)
{
listener = go.AddComponent<UIEvent>();
}
return listener;
}
public override void OnPointerEnter(PointerEventData eventData)
{
base.OnPointerEnter(eventData);
onEnter?.Invoke(eventData,this);
}
public override void OnPointerExit(PointerEventData eventData)
{
base.OnPointerExit(eventData);
onExit?.Invoke(eventData, this);
}
public override void OnDrag(PointerEventData eventData)
{
base.OnDrag(eventData);
onDrag?.Invoke(eventData, this);
}
public override void OnDrop(PointerEventData eventData)
{
base.OnDrop(eventData);
onDrop?.Invoke(eventData,this);
}
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
onDown?.Invoke(eventData,this);
}
public override void OnPointerUp(PointerEventData eventData)
{
base.OnPointerUp(eventData);
onUp?.Invoke(eventData,this);
}
public override void OnPointerClick(PointerEventData eventData)
{
base.OnPointerClick(eventData);
onClick?.Invoke(eventData,this);
}
public override void OnSelect(BaseEventData eventData)
{
base.OnSelect(eventData);
onSelect?.Invoke(eventData,this);
}
public override void OnDeselect(BaseEventData eventData)
{
base.OnDeselect(eventData);
onDeselect?.Invoke(eventData,this);
}
public override void OnScroll(PointerEventData eventData)
{
base.OnScroll(eventData);
onScroll?.Invoke(eventData,this);
}
public override void OnMove(AxisEventData eventData)
{
base.OnMove(eventData);
onMove?.Invoke(eventData,this);
}
public override void OnUpdateSelected(BaseEventData eventData)
{
base.OnUpdateSelected(eventData);
onUpdateSelect?.Invoke(eventData,this);
}
public override void OnInitializePotentialDrag(PointerEventData eventData)
{
base.OnInitializePotentialDrag(eventData);
onInitializePotentialDrag?.Invoke(eventData,this);
}
public override void OnBeginDrag(PointerEventData eventData)
{
base.OnBeginDrag(eventData);
onBeginDrag?.Invoke(eventData,this);
}
public override void OnEndDrag(PointerEventData eventData)
{
base.OnEndDrag(eventData);
onEndDrag?.Invoke(eventData,this);
}
public override void OnSubmit(BaseEventData eventData)
{
base.OnSubmit(eventData);
onSubmit?.Invoke(eventData,this);
}
public override void OnCancel(BaseEventData eventData)
{
base.OnCancel(eventData);
onCancel?.Invoke(eventData,this);
}
}
总结
继承型和绑定型有一个共同的特点,都需要与UI元素相关联,其区别是继承型融入在了各种组件内,而绑定型是以独立的组件形式体现出来的。
|