源码:
C#
namespace Game.Scripts.Listener
{
public class BaseEventListener : MonoBehaviour
{
public LuaFunction func = null;
public void OnFunc(PointerEventData eventData)
{
if (func != null)
{
func.Call(this.transform, eventData);
}
}
}
public class OnClickFunc : BaseEventListener, IPointerClickHandler
{
Selectable selectable;
void Awake()
{
selectable = GetComponent<Selectable>();
}
public void OnPointerClick(PointerEventData eventData)
{
if (!eventData.dragging && (selectable == null || (selectable.enabled && selectable.interactable)))
{
OnFunc(eventData);
}
}
}
public class OnDownFunc : BaseEventListener, IPointerDownHandler
{
public void OnPointerDown(PointerEventData eventData)
{
OnFunc(eventData);
}
}
public class OnUpFunc : BaseEventListener, IPointerUpHandler
{
public void OnPointerUp(PointerEventData eventData)
{
OnFunc(eventData);
}
}
public class OnDragFunc : BaseEventListener, IDragHandler
{
public void OnDrag(PointerEventData eventData)
{
OnFunc(eventData);
}
}
public class OnBeginDragFunc : BaseEventListener, IBeginDragHandler
{
public void OnBeginDrag(PointerEventData eventData)
{
OnFunc(eventData);
}
}
public class OnEndDragFunc : BaseEventListener, IEndDragHandler
{
public void OnEndDrag(PointerEventData eventData)
{
OnFunc(eventData);
}
}
public class OnEnterFunc : BaseEventListener, IPointerEnterHandler
{
public void OnPointerEnter(PointerEventData eventData)
{
OnFunc(eventData);
}
}
public class OnExitFunc : BaseEventListener, IPointerExitHandler
{
public void OnPointerExit(PointerEventData eventData)
{
OnFunc(eventData);
}
}
[DisallowMultipleComponent]
[LuaCallCSharp]
public class PointerEventListenerIL : MonoBehaviour
{
private void SetEventListener<T>(LuaFunction func) where T : BaseEventListener
{
T handler = gameObject.GetComponent<T>();
if (handler == null)
{
handler = gameObject.AddComponent<T>();
}
handler.func = func;
}
public LuaFunction onClick
{
set
{
SetEventListener<OnClickFunc>(value);
}
}
public LuaFunction onDown
{
set
{
SetEventListener<OnDownFunc>(value);
}
}
public LuaFunction onUp
{
set
{
SetEventListener<OnUpFunc>(value);
}
}
public LuaFunction onDrag
{
set
{
SetEventListener<OnDragFunc>(value);
}
}
public LuaFunction onBeginDrag
{
set
{
SetEventListener<OnBeginDragFunc>(value);
}
}
public LuaFunction onEndDrag
{
set
{
SetEventListener<OnEndDragFunc>(value);
}
}
public LuaFunction onEnter
{
set
{
SetEventListener<OnEnterFunc>(value);
}
}
public LuaFunction onExit
{
set
{
SetEventListener<OnExitFunc>(value);
}
}
}
}
Lua:
local Helpers = {}
function Helpers.GetPointerEvent(tr)
local types = typeof(CS.Game.Scripts.Listener.PointerEventListenerIL);
local listener = tr:GetComponent(types);
if listener == nil or listener:Equals(nil) then
listener = tr.gameObject:AddComponent(typeof(types))
end
return listener
end
function Helpers.OnClickListener(tr, func)
Helpers.GetPointerEvent(tr).onClick = func
end
function Helpers.OnDownListener(tr, func)
Helpers.GetPointerEvent(tr).onDown = func
end
function Helpers.OnUpListener(tr, func)
Helpers.GetPointerEvent(tr).onUp = func
end
function Helpers.OnDragListener(tr, func)
Helpers.GetPointerEvent(tr).onDrag = func
end
function Helpers.OnBeginDragListener(tr, func)
Helpers.GetPointerEvent(tr).onBeginDrag = func
end
function Helpers.OnEndDragListener(tr, func)
Helpers.GetPointerEvent(tr).onEndDrag = func
end
function Helpers.OnEnterListener(tr, func)
Helpers.GetPointerEvent(tr).onEnter = func
end
function Helpers.OnExitListener(tr, func)
Helpers.GetPointerEvent(tr).onExit = func
end
return Helpers
示例:
-- View
local UITest = ClassView("UITest")
local Helpers= require("Helpers")
function UITest:OnCreate()
self.testUI = self:FindChild("TestUI")
self:InitEvent()
end
function UITest:InitEvent()
Helpers.OnDownListener(self.testUI, function(obj, eventData)
logdebug(string.format("OnDownListener obj:%s", obj))
end)
Helpers.OnUpListener(self.testUI, function(obj, eventData)
logdebug(string.format("OnUpListener obj:%s", obj))
end)
Helpers.OnDragListener(self.testUI, function(obj, eventData)
logdebug(string.format("OnDragListener obj:%s", obj))
end)
end