protected override void _Load() { _luaEnv?.Dispose(); _watcher?.Dispose(); _luaEnv = new LuaEnv(); _luaEnv.Global.Set("Game", Game); _luaEnv.Global.Set("Logger", LogSystem); _luaEnv.Global.Set("Plugin", this); _luaEnv.Global.Set("DataPath", Path.Combine(Descriptor.Directory, "Data")); _luaEnv.DoString(_luaSandbox); _sandbox = _luaEnv.Global.Get <LuaTable>("sandbox_env"); var entryScript = File.ReadAllText(EntryFilePath); _luaEnv.DoString(entryScript, "chunk", _sandbox); if (Convert.ToBoolean(Func("load"))) { _watcher = new FileSystemWatcher(); _watcher.Path = Path.GetDirectoryName(EntryFilePath); _watcher.Filter = Path.GetFileName(EntryFilePath); _watcher.EnableRaisingEvents = true; _watcher.Changed += (sender, e) => { _refresh = true; }; } Loaded = true; }
public void Clear() { luaOnDestroy?.Invoke(); GameEntry.Event.Unsubscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneSuccess); luaStart = null; luaUpdate = null; luaLateUpdate = null; luaFixedUpdate = null; luaOnSceneLoaded = null; luaOnDestroy = null; m_luaEnv?.Dispose(); }
// Use this for initialization void Start() { LuaEnv luaenv = new LuaEnv(); luaenv.DoString(script); CalcNew calc_new = luaenv.Global.GetInPath<CalcNew>("Calc.New"); ICalc calc = calc_new(10, "hi", "john"); //constructor Debug.Log("sum(*10) =" + calc.Add(1, 2)); calc.Mult = 100; Debug.Log("sum(*100)=" + calc.Add(1, 2)); luaenv.Dispose(); }
void OnDestroy() { //释放 luaEnv.Dispose(); }
void OnDestroy() { luaenv.Dispose(); }
// Use this for initialization void Start() { LuaEnv luaenv = new LuaEnv(); luaenv.DoString("CS.UnityEngine.Debug.Log('hello world')"); luaenv.Dispose(); }
// Use this for initialization void Start() { LuaEnv luaenv = new LuaEnv(); luaenv.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson); luaenv.AddBuildin("lpeg", XLua.LuaDLL.Lua.LoadLpeg); luaenv.AddBuildin("pb", XLua.LuaDLL.Lua.LoadLuaProfobuf); luaenv.AddBuildin("ffi", XLua.LuaDLL.Lua.LoadFFI); luaenv.DoString(@" ------------------------------------ local rapidjson = require 'rapidjson' local t = rapidjson.decode('{""a"":123}') print(t.a) t.a = 456 local s = rapidjson.encode(t) print('json', s) ------------------------------------ local lpeg = require 'lpeg' print(lpeg.match(lpeg.R '09','123')) ------------------------------------ local pb = require 'pb' local protoc = require 'protoc' assert(protoc:load [[ message Phone { optional string name = 1; optional int64 phonenumber = 2; } message Person { optional string name = 1; optional int32 age = 2; optional string address = 3; repeated Phone contacts = 4; } ]]) local data = { name = 'ilse', age = 18, contacts = { { name = 'alice', phonenumber = 12312341234 }, { name = 'bob', phonenumber = 45645674567 } } } local bytes = assert(pb.encode('Person', data)) print(pb.tohex(bytes)) local data2 = assert(pb.decode('Person', bytes)) print(data2.name) print(data2.age) print(data2.address) print(data2.contacts[1].name) print(data2.contacts[1].phonenumber) print(data2.contacts[2].name) print(data2.contacts[2].phonenumber) --------------------------------- local ffi = require('ffi') ffi.cdef [[ typedef struct {int fake_id;unsigned int len;} CSSHeader; ]] ffi.cdef [[ typedef struct { CSSHeader header; float x; float y; float z; } Vector3; ]] local Vector3Native = ffi.typeof('Vector3 *') local v = CS.UnityEngine.Vector3(1, 2, 3) local vn = ffi.cast(Vector3Native, v) print(vn) if vn.header.fake_id == -1 then print('vector { ', vn.x, vn.y, vn.z, '}') else print('please gen code') end " ); luaenv.Dispose(); }
public void Destroy() { luaEnv.Dispose(); }
//销毁 public void Dispose() { luaEnv.Dispose(); m_instance = null; }
//销毁 public void Dispose() { luaEnv.Tick(); luaEnv.Dispose(); }
public void Dispose() { LuaEnv?.Dispose(); LuaEnv = null; }
private void OnDestroy() { env.Dispose(); }
public void Destroy() { _luaEnv.Dispose(); _luaEnv = null; }
private void OnApplicationQuit() { luaEnv.Dispose(); }
private void OnDestroy() { luaenv.Dispose(); luaenv = null; }
private void OnDestroy() { EnvLua.Dispose(); }
private void OnDestroy() { mLuaEnv.Dispose(); }
public void RunLuaCode(string msg) { _L.DoString(msg); _L.Dispose(); }
public override void OnDestroy() { m_luaEnv.Dispose(); }
// Use this for initialization public override void Start() { LuaEnv luaenv = new LuaEnv(); //这两个例子都必须生成代码才能正常运行 //例子1:改造Vector3 //沿用Vector3原来的映射方案Vector3 -> userdata,但是把Vector3的方法实现改为lua实现,通过xlua.genaccessor实现不经过C#直接操作内存 //改为不经过C#的好处是性能更高,而且你可以省掉相应的生成代码以达成省text段的效果 //仍然沿用映射方案的好处是userdata比table更省内存,但操作字段比table性能稍低,当然,你也可以结合例子2的思路,把Vector3也改为映射到table luaenv.DoString(@" function test_vector3(title, v1, v2) print(title) print(v1.x, v1.y, v1.z) print(v1, v2) print(v1 + v2) v1:Set(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z) print(v1) print(CS.UnityEngine.Vector3.Normalize(v1)) end test_vector3('----before change metatable----', CS.UnityEngine.Vector3(1, 2, 3), CS.UnityEngine.Vector3(7, 8, 9)) local get_x, set_x = xlua.genaccessor(0, 8) local get_y, set_y = xlua.genaccessor(4, 8) local get_z, set_z = xlua.genaccessor(8, 8) local fields_getters = { x = get_x, y = get_y, z = get_z } local fields_setters = { x = set_x, y = set_y, z = set_z } local ins_methods = { Set = function(o, x, y, z) set_x(o, x) set_y(o, y) set_z(o, z) end } local mt = { __index = function(o, k) --print('__index', k) if ins_methods[k] then return ins_methods[k] end return fields_getters[k] and fields_getters[k](o) end, __newindex = function(o, k, v) return fields_setters[k] and fields_setters[k](o, v) or error('no such field ' .. k) end, __tostring = function(o) return string.format('vector3 { %f, %f, %f}', o.x, o.y, o.z) end, __add = function(a, b) return CS.UnityEngine.Vector3(a.x + b.x, a.y + b.y, a.z + b.z) end } xlua.setmetatable(CS.UnityEngine.Vector3, mt) test_vector3('----after change metatable----', CS.UnityEngine.Vector3(1, 2, 3), CS.UnityEngine.Vector3(7, 8, 9)) "); Debug.Log("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); //例子2:struct映射到table改造 //PushAsTableStruct传送到lua侧将会是table,例子里头还为这个table添加了一个成员方法SwapXY,静态方法Print,打印格式化,以及构造函数 luaenv.DoString(@" local mt = { __index = { SwapXY = function(o) --成员函数 o.x, o.y = o.y, o.x end }, __tostring = function(o) --打印格式化函数 return string.format('struct { %d, %d}', o.x, o.y) end, } xlua.setmetatable(CS.PushAsTableStruct, mt) local PushAsTableStruct = { Print = function(o) --静态函数 print(o.x, o.y) end } setmetatable(PushAsTableStruct, { __call = function(_, x, y) --构造函数 return setmetatable({x = x, y = y}, mt) end }) xlua.setclass(CS, 'PushAsTableStruct', PushAsTableStruct) "); PushAsTableStruct test; test.x = 100; test.y = 200; luaenv.Global.Set("from_cs", test); luaenv.DoString(@" print('--------------from csharp---------------------') assert(type(from_cs) == 'table') print(from_cs) CS.PushAsTableStruct.Print(from_cs) from_cs:SwapXY() print(from_cs) print('--------------from lua---------------------') local from_lua = CS.PushAsTableStruct(4, 5) assert(type(from_lua) == 'table') print(from_lua) CS.PushAsTableStruct.Print(from_lua) from_lua:SwapXY() print(from_lua) "); luaenv.Dispose(); }
public void Release() { _luaEnv.Dispose(); }