Uniyt热更新——LuaFrameWork学习(二)按钮点击事件

来源:互联网 发布:mysql排错指南 pdf 编辑:程序博客网 时间:2024/06/07 03:56

上一篇文章我们主要是梳理了一下框架例子的流程,知道了ulua与C#之间的调用,这篇我们接着来学习下框架对UI按钮事件的处理。思路就是点击按钮时调用lua的代码来处理相关的逻辑,那他是怎么实现的呢,不多说直接看源码。

首先我们来看下LuaBehaviour.cs的代码里面有我们想要的东西:

using UnityEngine;using LuaInterface;using System.Collections;using System.Collections.Generic;using System;using UnityEngine.UI;namespace LuaFramework {    public class LuaBehaviour : View {        private string data = null;        private Dictionary<string, LuaFunction> buttons = new Dictionary<string, LuaFunction>();        protected void Awake() {            Util.CallMethod(name, "Awake", gameObject);        }        protected void Start() {            Util.CallMethod(name, "Start");        }        protected void OnClick() {            Util.CallMethod(name, "OnClick");        }        protected void OnClickEvent(GameObject go) {            Util.CallMethod(name, "OnClick", go);        }        /// <summary>        /// 添加单击事件        /// </summary>        public void AddClick(GameObject go, LuaFunction luafunc) {            if (go == null || luafunc == null) return;            buttons.Add(go.name, luafunc);            go.GetComponent<Button>().onClick.AddListener(                delegate() {                    luafunc.Call(go);                }            );        }        /// <summary>        /// 删除单击事件        /// </summary>        /// <param name="go"></param>        public void RemoveClick(GameObject go) {            if (go == null) return;            LuaFunction luafunc = null;            if (buttons.TryGetValue(go.name, out luafunc)) {                luafunc.Dispose();                luafunc = null;                buttons.Remove(go.name);            }        }        /// <summary>        /// 清除单击事件        /// </summary>        public void ClearClick() {            foreach (var de in buttons) {                if (de.Value != null) {                    de.Value.Dispose();                }            }            buttons.Clear();        }        //-----------------------------------------------------------------        protected void OnDestroy() {            ClearClick();#if ASYNC_MODE            string abName = name.ToLower().Replace("panel", "");            ResManager.UnloadAssetBundle(abName + AppConst.ExtName);#endif            Util.ClearMemory();            Debug.Log("~" + name + " was destroy!");        }    }}
里面的AddClick函数就是我们要看的,通过这个函数我们可以给指定的Button添加OnClick事件,这个事件主要实现了调用Lua函数。Ok,再配合Lua代码我们给对应的Button添加事件,看Lua代码:

prompt = transform:GetComponent('LuaBehaviour');logWarn("Start lua--->>"..gameObject.name);prompt:AddClick(PromptPanel.btnOpen, this.OnClick);function PromptCtrl.OnClick(go)if TestProtoType == ProtocalType.BINARY thenthis.TestSendBinary();endif TestProtoType == ProtocalType.PB_LUA thenthis.TestSendPblua();endif TestProtoType == ProtocalType.PBC thenthis.TestSendPbc();endif TestProtoType == ProtocalType.SPROTO thenthis.TestSendSproto();endlogWarn("OnClick---->>>"..go.name);end
这里通过调用LuaBehaviour.cs里的AddClick函数给btnOpen按钮添加了OnClick事件,这样当我们点击btnOpen按钮是就会调用Lua里的PromptCtrl.OnClick函数,是不是很简单也很实用。

0 0