tolua 学习笔记 UI监听事件

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Jason_520/article/details/54233497

编写UI事件监听脚本,参考雨松大神:http://www.xuanyusong.com/archives/3325

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

namespace LuaFramework
{
    public class EventTriggerListener : EventTrigger
    {
        //带参数是为了方便取得绑定了UI事件的对象    
        public delegate void PointerEventDelegate(GameObject go, PointerEventData eventData);

        public delegate void BaseEventDelegate(GameObject go, BaseEventData eventData);

        public delegate void AxisEventDelegate(GameObject go, AxisEventData eventData);

        public event PointerEventDelegate onPointerEnter;
        public event PointerEventDelegate onPointerExit;
        public event PointerEventDelegate onPointerDown;
        public event PointerEventDelegate onPointerUp;
        public event PointerEventDelegate onPointerClick;
        public event PointerEventDelegate onInitializePotentialDrag;
        public event PointerEventDelegate onBeginDrag;
        public event PointerEventDelegate onDrag;
        public event PointerEventDelegate onEndDrag;
        public event PointerEventDelegate onDrop;
        public event PointerEventDelegate onScroll;
        public event BaseEventDelegate onUpdateSelected;
        public event BaseEventDelegate onSelect;
        public event BaseEventDelegate onDeselect;
        public event AxisEventDelegate onMove;
        public event BaseEventDelegate onSubmit;
        public event BaseEventDelegate onCancel;


        public static EventTriggerListener Get(GameObject go)
        {
            EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
            if (listener == null) listener = go.AddComponent<EventTriggerListener>();
            return listener;
        }

        public override void OnPointerEnter(PointerEventData eventData)
        {
            if (onPointerEnter != null) onPointerEnter(gameObject, eventData);
        }

        public override void OnPointerExit(PointerEventData eventData)
        {
            if (onPointerExit != null) onPointerExit(gameObject, eventData);
        }

        public override void OnPointerDown(PointerEventData eventData)
        {
            if (onPointerDown != null) onPointerDown(gameObject, eventData);
        }

        public override void OnPointerUp(PointerEventData eventData)
        {
            if (onPointerUp != null) onPointerUp(gameObject, eventData);
        }

        public override void OnPointerClick(PointerEventData eventData)
        {
            if (onPointerClick != null) onPointerClick(gameObject, eventData);
        }

        public override void OnInitializePotentialDrag(PointerEventData eventData)
        {
            if (onInitializePotentialDrag != null) onInitializePotentialDrag(gameObject, eventData);
        }

        public override void OnBeginDrag(PointerEventData eventData)
        {
            if (onBeginDrag != null) onBeginDrag(gameObject, eventData);
        }

        public override void OnDrag(PointerEventData eventData)
        {
            if (onDrag != null) onDrag(gameObject, eventData);
        }

        public override void OnEndDrag(PointerEventData eventData)
        {
            if (onEndDrag != null) onEndDrag(gameObject, eventData);
        }

        public override void OnDrop(PointerEventData eventData)
        {
            if (onDrop != null) onDrop(gameObject, eventData);
        }

        public override void OnScroll(PointerEventData eventData)
        {
            if (onScroll != null) onScroll(gameObject, eventData);
        }

        public override void OnUpdateSelected(BaseEventData eventData)
        {
            if (onUpdateSelected != null) onUpdateSelected(gameObject, eventData);
        }

        public override void OnSelect(BaseEventData eventData)
        {
            if (onSelect != null) onSelect(gameObject, eventData);
        }

        public override void OnDeselect(BaseEventData eventData)
        {
            if (onDeselect != null) onDeselect(gameObject, eventData);
        }

        public override void OnMove(AxisEventData eventData)
        {
            if (onMove != null) onMove(gameObject, eventData);
        }

        public override void OnSubmit(BaseEventData eventData)
        {
            if (onSubmit != null) onSubmit(gameObject, eventData);
        }

        public override void OnCancel(BaseEventData eventData)
        {
            if (onCancel != null) onCancel(gameObject, eventData);
        }
    }
}

在框架中找到CustomSettings,添加:

_GT(typeof(EventTriggerListener)),

重新生成Wrap。
编写lua代码:

--主入口函数。从这里开始lua逻辑

require "Common/define"

function Main()                 
    AddEvent();
end

--场景切换通知
function OnLevelWasLoaded(level)
    Time.timeSinceLevelLoad = 0
end

function AddEvent()
    local btn=GameObject.Find("btn");
    EventTriggerListener.Get(btn).onPointerClick = EventTriggerListener.Get(btn).onPointerClick+Onclick;
end

function Onclick(obj)
    print('lua click trigger! from GameObject:'.. obj.name);
end

在define.lua中需添加

EventTriggerListener = LuaFramework.EventTriggerListener;

大功告成,笔者也是刚刚接触LuaFramework,博文只是作为学习笔记与大家分享,希望能与大家共同进步。


好了,祝进步。

每天进步一点点。

展开阅读全文

没有更多推荐了,返回首页