Lua里实现Unity的拖拽、移入移出等响应事件

39 篇文章 1 订阅
14 篇文章 1 订阅

源码:

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

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值