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里运行的效果吧

Mac Unity5.45环境下运行

原创粉丝点击