神临的uLua学习(五)
来源:互联网 发布:国外网络摄像头直播 编辑:程序博客网 时间:2024/05/17 04:26
17.LuaArray(这是关于Array的uLua使用方式)
string source = @" function luaFunc(objs, len) for i = 0, len - 1 do print(objs[i]) end local table1 = {'111', '222', '333'} return unpack(table1) end "; string[] objs = { "aaa", "bbb", "ccc" }; // Use this for initialization void Start() { LuaScriptMgr luaMgr = new LuaScriptMgr(); luaMgr.Start(); LuaState l = luaMgr.lua; l.DoString(source); //c# array to lua table LuaFunction f = l.GetFunction("luaFunc"); object[] rs = f.Call(objs, objs.Length); f.Release(); //lua table to c# array foreach (object de in rs) { Debug.Log(de.ToString()); } }
难度不大,有优秀C#基础或则有其他编程能力的人应该很容易就会看懂.
18.LuaEnum(这是关于Enum在uLua的使用)
const string source = @" local type = LuaEnumType.IntToEnum(1); print(type == LuaEnumType.AAA); "; // Use this for initialization void Start () { LuaScriptMgr mgr = new LuaScriptMgr(); mgr.Start(); mgr.lua.DoString(source); }
下面是辅助类
public enum LuaEnumType { AAA = 1, BBB = 2, CCC = 3, DDD = 4}
相信你会说 So Easy
19.LuaClass(这是uLua关于类的使用方式)
const string source = @" Account = { balance = 0 }; function Account:new(o) o = o or {}; setmetatable(o, { __index = self }); return o; end function Account.deposit(self, v) self.balance = self.balance + v; end function Account:withdraw(v) if (v) > self.balance then error 'insufficient funds'; end self.balance = self.balance - v; end SpecialAccount = Account:new(); function SpecialAccount:withdraw(v) if v - self.balance >= self:getLimit() then error 'insufficient funds'; end self.balance = self.balance - v; end function SpecialAccount.getLimit(self) return self.limit or 0; end s = SpecialAccount:new{ limit = 1000 }; print(s.balance); s:deposit(100.00); print (s.limit); print (s.getLimit(s)) print (s.balance) "; void Start () { LuaScriptMgr mgr = new LuaScriptMgr(); mgr.Start(); mgr.lua.DoString(source); }
相信大家对于类都耳熟能详了,对比着学就行.
20.Debugger(观名知意,就不多说了)
void Start () { //LuaState l = new LuaState(); //LuaDLL.luaopen_socket_core(l.L); //l.DoFile("C:/Users/Administrator/Documents/New Unity Project/Assets/uLua/Lua/debugger.lua"); LuaScriptMgr mgr = new LuaScriptMgr(); mgr.Start(); mgr.DoFile("debugger"); }
21.LuaCall(这里是函数的使用方式)
const string script = @" A_LuaCall = luanet.import_type('A_LuaCall') LuaClass = {} LuaClass.__index = LuaClass function LuaClass:New() local self = {}; setmetatable(self, LuaClass); return self; end function LuaClass:test() A_LuaCall.OnSharpCall(self, self.callback); end function LuaClass:callback() print('test--->>>'); end LuaClass:New():test(); "; void Start () { LuaState lua = new LuaState(); lua.DoString(script); } public static void OnSharpCall(LuaTable self, LuaFunction func) { func.Call(self); } 这里我还没有使用过,有时间的话再试试喽
好了,关于uLua的整理就到这里了,写了这么多,也是够麻烦的,尤其是对于我这样的人.
下面整理Shader的知识吧,谢谢.
阅读全文