xLua教程:https://github.com/Tencent/xLua/blob/master/Assets/XLua/Doc/XLua%E6%95%99%E7%A8%8B.md
xLua配置:https://github.com/Tencent/xLua/blob/master/Assets/XLua/Doc/configure.md
FAQ:https://github.com/Tencent/xLua/blob/master/Assets/XLua/Doc/faq.md
开始学习
private LuaEnv luaenv;
// Start is called before the first frame update
void Start()
{
luaenv = new LuaEnv();
luaenv.DoString("print('Hello World!')");
}
private void OnDestroy()
{
luaenv.Dispose();
}
在lua中调用C#中的方法
luaenv.DoString("CS.UnityEngine.Debug.Log('Hello World!')");
加载运行Lua源文件
在Resources文件夹中建一个 helloworld.lua.txt 文件
里面写着lua 程序
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
public class helloworld02 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
TextAsset textAsset = Resources.Load<TextAsset>("helloworld.lua");
LuaEnv luaEnv = new LuaEnv();
luaEnv.DoString(textAsset.ToString());
luaEnv.Dispose();
}
通过内置的 loader 加载lua 源文件
luaenv.DoString(" require 'helloworld'"); //loader 会直接执行里面的代码
添加自定义的 loader 方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
public class defineloader03 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
LuaEnv env = new LuaEnv();
env.AddLoader(MyLoader);
env.DoString("require 'helloworld'");
env.Dispose();
}
private byte[] MyLoader(ref string filepath)
{
print(filepath);
string s = "print(123)";
// 将字符串转成字节数组
return System.Text.Encoding.UTF8.GetBytes(s);
}
require 会将路径传递给每一个 loader 去执行,直到找到或者确定完全找不到为止(报异常)
通过自定义Loader 加载指定目录的Lua脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
public class defineloader03 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
LuaEnv env = new LuaEnv();
env.AddLoader(MyLoader);
env.DoString("require 'helloworld007'");
env.Dispose();
}
private byte[] MyLoader(ref string filepath)
{
string absPath = Application.streamingAssetsPath + "/" + filepath + ".lua.txt";
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
}
C#访问Lua之访问Lua中的全局变量
LuaEnv env = new LuaEnv();
env.DoString("require 'CsharpCallLua'");
int a = env.Global.Get<int>("a"); //获取到lua里的全局变量a
string str = env.Global.Get<string>("str"); //获取到lua里的全局变量str
bool isDie = env.Global.Get<bool>("isDie"); //获取全局变量isDie
print(a);
print(str);
print(isDie);
env.Dispose();
C#访问Lua之访问Lua中的table(映射到class)
Person p = env.Global.Get<Person>("person");
print(p.name + "-" + p.age);
env.Dispose();
}
class Person
{
public string name;
public int age;
}
C#访问Lua之访问Lua中的table(映射到interface)
通过接口修改属性,lua中的属性也会被修改
function person:eat(a,b) --默认带一个self的参数,代表当前table
print(a+b)
end
function person.eat(self,a,b) --需要手动写一个self 来接收
print(a+b)
end
C#访问Lua之通过Dictionary或者List
dictionary 映射
List 映射
//通过Dictionary , List
Dictionary<string, object> dict = env.Global.Get<Dictionary<string, object>>("person");
foreach (string key in dict.Keys)
{
print(key + "-" + dict[key]);
}
List<object> list = env.Global.Get<List<object>>("person");
foreach (object o in list)
{
print(o);
}
C#访问Lua之通过LuaTable访问table
C#访问Lua中的全局function(第一种方式)
无参
有参
视频版本太旧了,有些地方走不通
通过LuaFunction访问Lua中的全局函数
比较慢
//映射到LuaFunction
LuaFunction func = env.Global.Get<LuaFunction>("add");
object[] os = func.Call(1, 2);
foreach (object o in os)
{
print(o);
}
env.Dispose();
在Lua中new C#对象(创建游戏物体)
LuaEnv env = new LuaEnv();
env.DoString("require 'luacallcsharp'");
env.Dispose();
Lua访问C#静态属性和方法
移除摄像机组件