第一个hello world 代码
using XLua;
public class HelloWorld01 : MonoBehaviour {
private LuaEnv luaenv;
// Use this for initialization
void Start () {
//xlua的运行环境
luaenv = new LuaEnv();
//luaenv.DoString("print('Hello World')");
luaenv.DoString("CS.UnityEngine.Debug.Log('Hello World')");
}
//游戏退出或者切换场景的时候调用
private void OnDestroy()
{
//释放
luaenv.Dispose();
}
}
读取Xlua文件
using XLua;
using System.IO;
public class CreatLoader : MonoBehaviour {
// Use this for initialization
void Start ()
{
LuaEnv luaenv = new LuaEnv();
luaenv.AddLoader(MyLoader);
luaenv.DoString("require 'test007'");
luaenv.Dispose();
}
private byte[] MyLoader(ref string filePath)
{
print(filePath);
//string s = "print(123)";
////string转化为字节数组
//return System.Text.Encoding.UTF8.GetBytes(s);
print(Application.streamingAssetsPath);
string absPath = Application.streamingAssetsPath + "/" + filePath + ".lua.txt";
//路径转化为txt文件,再转化为字节数组
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
}
}
C#中调用Xlua
using XLua;
using System;
public class CSharpCallLua : MonoBehaviour {
// Use this for initialization
void Start () {
LuaEnv luaEnv = new LuaEnv();
luaEnv.DoString("require 'CSharpCallLua'");
////访问lua中的变量 lua中的number对应 int float double
//int a = luaEnv.Global.Get<int>("a");
//print(a);
//string str = luaEnv.Global.Get<string>("str");
//print(str);
//bool isShow = luaEnv.Global.Get<bool>("isShow");
//print(isShow);
////===1======通过Class
////值拷贝,比较耗费性能
//Person p = luaEnv.Global.Get<Person>("person");
//print(p.name);
////这样的赋值的age并不会改变lua中的值
//p.age = 18;
//luaEnv.DoString("print(person.age)");
////===2======通过interface
////名i由原来的lele变成了lele.lele
////接口为引用拷贝
//IPerson ip = luaEnv.Global.Get<IPerson>("person");
//ip.name = "lele.lele";
//luaEnv.DoString("print(person.name)");
////注意调用的是ip.eat(p,11,11)
//ip.eat(11,11);
////===3======通过Dictionary,List
////Dictionary 不映射member
//Dictionary<string, object> dic = luaEnv.Global.Get<Dictionary<string, object>>("person");
//foreach (string key in dic.Keys)
//{
// print(key + "--" + dic[key]);
//}
//List<object> list = luaEnv.Global.Get<List<object>>("person");
//foreach (object o in list)
//{
// print(o);
//}
////===4=====通过LuaTable,不常用
//LuaTable tab = luaEnv.Global.Get<LuaTable>("person");
//print(tab.Get<string>("name"));
//print(tab.Get<int>("age"));
////访问lua中的全局变量,无参数的时候
//Action act1 = luaEnv.Global.Get<Action>("add");
//act1();
////Dispose之前要置空
//act1 = null;
////==1、映射到delegate,访问functiont推荐delegate
////访问lua中的全局变量,有参数
Add add = luaEnv.Global.Get<Add>("add");
int resa = 0;
int resb = 0;
//int res=add(11, 12,out resa,out resb);
int res = add(11, 12, ref resa, ref resb);
print(res + "=" + resa + "+" + resb);
add = null;
////==2、映射到Luafunction
//LuaFunction lfc = luaEnv.Global.Get<LuaFunction>("add");
//object[] obs= lfc.Call(1, 2);
//foreach (object o in obs)
//{
// print(o);
//}
luaEnv.Dispose();
}
//有参数的方法通过委托来进行映射,必须添加特性
[CSharpCallLua]
//delegate int Add(int a, int b,out int resa,out int resb);
delegate int Add(int a, int b, ref int resa, ref int resb);
//class Person
//{
// public string name;
// public int age;
//}
//接口中必须添加特性,访问table推荐使用Interface
[CSharpCallLua]
interface IPerson
{
//接口中不能包含字段
string name { get; set; }
int age { get; set; }
void eat(int a,int b);
}
}
Lua调用C#
using XLua;
//枚举
[LuaCallCSharp]
enum EnemyType
{
Normal,
Hard,
NB
}
public class LuaCallCSharp : MonoBehaviour {
// Use this for initialization
void Start () {
LuaEnv luaenv = new LuaEnv();
luaenv.DoString("require 'LuaCallCSharp'");
luaenv.Dispose();
}
}
--生成游戏物体==new GameObject
CS.UnityEngine.GameObject()
--访问静态变量
print(CS.UnityEngine.Time.deltaTime)
--CS.命名空间+类名+方法名
GameObject=CS.UnityEngine.GameObject
local camera=GameObject.Find("Main Camera")
camera.name="changebylua"
--调用成员方法的时候使用:
local cameraCom=camera:GetComponent("Camera")
GameObject.Destroy(cameraCom)
local light=GameObject.Find("Direction Light")