aaaaaa
aaaaaa
--lua语言像py一样是一种轻量级语言也是siki老师说的约定大于约束,所以在实际开发中像访问table是需要约束的进行值访问
--luaTable.Length --只返回使用默认key的value[如a={"abc",123,3.14} 则返回3]
访问时只需指定所属类型和索引即可[如 luaTable.Get<string>(1)]
Action<string> run = luaEnv.Global.Get<Action<string>>("run");
run("siki")
Action<> 只支持单参数的使用,无返回值
foreach (var key in tab.GetKeys()) {
print("key:"+key+"---keyType:"+key.GetType()+"---value:"+tab[key]);
}
table中本没有键的那些值,转换成LuaTable后,会自动添加一个int64类型的键,下标从1开始。
CSarpCallLua
只需要把assets目录下的内容拷贝到unity就行了
怎样用代码解决这个问题,不要直接打开txt另存更改编码的
Luaenv.DoString
是
[CSharpCallLua]
报错弄了半天
最后用2017.4.2f2,加老师提供的xLua-master.zip
才能解决
github上的说什么支持2018 都是骗人的
不能使用Resources的话可以选择使用文件流
private LuaEnv mLuaEnv;
void Start () {
mLuaEnv = new LuaEnv();
using (StreamReader sr = new StreamReader(Application.dataPath + "/Scenes/002-RunLuaFile/LuaHelloWorld.lua"))
{
string script = sr.ReadToEnd();
mLuaEnv.DoString(script);
sr.Dispose();
}
}
void OnDistroy()
{
mLuaEnv.Dispose();
}
自定义Loader没看出有啥用。。。看下一节吧
由于uLua的使用习惯,我lua文档不习惯以txt结尾
mLuaEnv = new LuaEnv();
mLuaEnv.AddLoader((ref string filePath) => {
string script = string.Empty;
using (StreamReader sr = new StreamReader(Application.dataPath + "/Scenes/002-RunLuaFile/MyLua/" + filePath + ".lua"))
{
script = sr.ReadToEnd();
sr.Dispose();
}
return System.Text.Encoding.UTF8.GetBytes(script);
});
mLuaEnv.DoString("require 'LuaHelloWorld'");//loader
Lua2GlobalFunction
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using XLua; namespace DM08 { public class Lua2GlobalFunction : MonoBehaviour { LuaEnv xLuaEnv; #region 生命 void Start() { xLuaEnv = new LuaEnv(); xLuaEnv.DoString("require 'SourceFile08'"); // UseGlobalFunction(); } private void OnDestroy() { xLuaEnv.Dispose(); } #endregion #region 辅助1 ///
映射不了带Key的(包括函数)void UseGlobalFunction() { print("UseGlobalFunction()"); NoPara(); HavePara(); CustomDelegate(); CustomDelegate_Return(); CustomDelegate_ReturnOut(); CustomDelegate_ReturnRef(); } #endregion #region 辅助2 ///
无参数void NoPara() { Action act1 = xLuaEnv.Global.Get("func1"); act1(); // xLuaEnv.Dispose();//Dispose()放着,前面没有act1 = null;会报错 } ///
有参数void HavePara() { Action act1 = xLuaEnv.Global.Get>("func2"); act1(1, 2); } #endregion #region 辅助2 自定义委托 void CustomDelegate() { Func3 act = xLuaEnv.Global.Get("func3"); act(1, 2); } [CSharpCallLua] delegate void Func3(int a, int b); void CustomDelegate_Return() { Func4 act = xLuaEnv.Global.Get("func4"); int sum = act(1, 2); print(sum); } [CSharpCallLua] delegate int Func4(int a, int b); void CustomDelegate_ReturnOut() { Func5 act = xLuaEnv.Global.Get("func5"); int resa; int resb; int sum=act(1, 2, out resa, out resb); print(sum); print(resa); print(resb); } [CSharpCallLua] delegate int Func5(int a, int b,out int resa,out int resb); void CustomDelegate_ReturnRef() { Func6 act = xLuaEnv.Global.Get("func6"); int resa=0; int resb=0; int sum = act(1, 2, ref resa, ref resb); print(sum); print(resa); print(resb); } [CSharpCallLua] delegate int Func6(int a, int b, ref int resa, ref int resb); #endregion } } -------------------------------------- function func1() print("func1") end function func2(a,b) print("func2") print(a) print(b) end function func3(a,b) print("func3") print(a) print(b) end function func4(a,b) print("func4") print(a) print(b) return a+b end function func5(a,b) print("func5") print(a) print(b) return a+b,a,b end function func6(a,b) print("func6") print(a) print(b) return a+b,a,b end,>