toLua入门学习笔记(一):update函数在toLua内的实现
来源:互联网 发布:excel 下拉数据递增 编辑:程序博客网 时间:2024/06/05 00:16
先上一个C#脚本,它可以帮助我们快速的创建一个新的LuaClass对象并且挂载到指定的物体上
using UnityEngine;using System.Collections;using LuaInterface;using System;using LuaFramework;public class LuaComponent : Base { LuaTable table; //添加Lua组件 public static LuaTable Add(GameObject go,LuaTable tableClass) { LuaFunction fun = tableClass.GetLuaFunction ("New"); if (fun == null) return null; object[] results = fun.Call (tableClass); if (results.Length != 1) return null; LuaComponent cmp = go.AddComponent(typeof(LuaComponent)) as LuaComponent; cmp.table = (LuaTable)results[0]; cmp.CallAwake(); return cmp.table; } //获取Lua组件 public static LuaTable Get(GameObject go,LuaTable table) { LuaComponent[] cmps = go.GetComponents(typeof(LuaComponent)) as LuaComponent[]; foreach (LuaComponent cmp in cmps) { string met1 = table.ToString (); string met2 = cmp.table.GetMetaTable ().ToString (); if (met1 == met2) { return cmp.table; } } return null; } void CallAwake() { LuaFunction fun = table.GetLuaFunction ("Awake"); if (fun != null) { fun.Call (table, gameObject); } } protected void OnDestroy() { Util.ClearMemory (); Debug.Log ("~" + name + " was destroied!"); }}
下面新建一个Lua Table 作为”类”
require "Common/functions"Tank = { Hp = 100, Atk = 10, FireInterval = 5, Name = "T180天启坦克",}function Tank:GetHp() log("Tank 的血量等于=========="..self.Hp)endfunction Tank:Update() log(self.Name.." is doing Update")endfunction Tank:FixedUpdate() log(self.Name.." is doing FixedUpdate")endfunction Tank:LateUpdate() log(self.Name.." is doing LateUpdate")endfunction Tank:AddAllUpdateEvents() if self.Update ~= nil then UpdateBeat:Add(self.Update,self) end if self.LateUpdate ~= nil then LateUpdateBeat:Add(self.LateUpdate,self) end if self.FixedUpdate ~= nil then FixedUpdateBeat:Add(self.FixedUpdate,self) endendfunction Tank:New(obj) local o = {} if obj == nil then log(" 初始化参数 obj 为空") end setmetatable(o,self) self.__index = self; return oend
在ToLua的Main.lua 中做如下修改
--我把Tank.lua放到了新建的Edu文件夹下require "Edu/Tank"local Input = UnityEngine.Inputlocal KeyCode = UnityEngine.KeyCode--主入口函数。从这里开始lua逻辑function Main() local go1 = UnityEngine.GameObject("tank1",typeof(UnityEngine.BoxCollider)) local tank1 = LuaComponent.Add(go1,Tank) tank1.Name = "新的tank:幻影先锋200TX" tank1:GetHp() tank1.Hp = 999999 tank1:GetHp() tank1:AddAllUpdateEvents() local go2 = UnityEngine.GameObject("tank2",typeof(UnityEngine.BoxCollider)) local tank2 = LuaComponent.Add(go2,Tank) tank2.Name = "新的tank:极光暗影坦克" tank2:GetHp() tank2.Hp = 3333333 tank2:GetHp() tank2:AddAllUpdateEvents() --tank2.Name = "改了名字的坦克!!!!!"end--场景切换通知function OnLevelWasLoaded(level) collectgarbage("collect") Time.timeSinceLevelLoad = 0end
在CustomSettings.cs里注册一下该类型
//在这里添加你要导出注册到lua的类型列表 public static BindType[] customTypeList = { //------------------------为例子导出-------------------------------- //_GT(typeof(TestEventListener)), //_GT(typeof(TestProtol)), _GT(typeof(TestAccount)), _GT(typeof(Dictionary<int, TestAccount>)).SetLibName("AccountMap"), _GT(typeof(KeyValuePair<int, TestAccount>)), //_GT(typeof(TestExport)), //_GT(typeof(TestExport.Space)), //------------------------------------------------------------------- //-----------------------------------Custom------------------------------------- _GT(typeof(LuaComponent)),
重新wrap一下!
好啦,来看看Unity里运行的效果吧
阅读全文
0 0
- toLua入门学习笔记(一):update函数在toLua内的实现
- ToLua学习笔记,使用Update方法(一)
- ToLua学习笔记,通信(一)
- tolua学习笔记之一
- ToLua学习笔记,使用Update方法(二)
- ToLua学习笔记,使用Update方法(三)
- ToLua学习笔记,使用Update方法(四)
- tolua 学习笔记 插件的使用
- tolua++入门
- tolua++入门
- tolua++入门
- ToLua 笔记
- Tolua使用笔记一:开始使用Tolua的准备工作与lua文件读取方法
- Tolua使用笔记一:开始使用Tolua的准备工作与lua文件读取方法
- Unity ToLua & LuaFramework学习笔记
- tolua++
- tolua
- tolua
- 谈谈程序员面试之刷题
- struts2中jsp页面传值到action方式总结
- docx文档表格抽取(word)
- 利用RecyclerView实现仿陌陌滑动浏览图片
- RK3399 添加默认分辨率
- toLua入门学习笔记(一):update函数在toLua内的实现
- HDU6029 Graph Theory
- equals和==的区别
- 6.labview利用FOR循环求和sum
- Ubuntu下J2EE环境搭建
- 使用Python进行分布式系统协调 (ZooKeeper,Consul, etcd )
- 岭回归与lasso详解
- HTML-超文本标记语言基础01
- 优化算法——粒子群算法(PSO)