1. 程式人生 > >tolua 學習筆記 UI監聽事件

tolua 學習筆記 UI監聽事件

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,博文只是作為學習筆記與大家分享,希望能與大家共同進步。

好了,祝進步。

每天進步一點點。