xlua基础

1.helloworld

 LuaEnv luaenv = new LuaEnv();
        luaenv.DoString("CS.UnityEngine.Debug.Log('hello world')");
        luaenv.Dispose();

2.通过loader引用文件夹里面的lua程序 

 require引用resurces文件夹的程序

LuaEnv evn = new LuaEnv();              //构造虚拟机
        //evn.DoString(ta.text);
        evn.DoString("require 'helloLua'");                           //相当于using,会引用并执行(loader)但还是得放在resurces文件夹
        evn.Dispose();

 自定义loader
void Start () {
        LuaEnv env = new LuaEnv();
        env.AddLoader(myLoader);                        //自定义loader方法
        env.DoString("require 'luadiy'");               //执行lua类或者语句      
        env.Dispose();
	}
	
    private byte[] myLoader(ref string filePath)        //先用自定义的loader去加载,返回输出字节数组,如果没有返回则会调用下个load继续加载
    {
        print(filePath);               //文件名
        string str = Application.dataPath + "/xxx/" + filePath + ".lua.txt";
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(str));       //用资源读取类读取这个目录文件(lua类),转换为字节流数组
        //string s = "print(123)";
        return null;                                                            //返回(类)给想要执行的lua程序
        //return System.Text.Encoding.UTF8.GetBytes(s);
    }

3.csCallLua

1.全局变量直接用Global.Get
//double a= evn.Global.Get("a");
        //string b = evn.Global.Get("b");
        //bool c = evn.Global.Get("x");
 2.访问table
 1.通过class,但是相当于拷贝,不能改变lua代码中的值
person={v1="a",v2="wd",22,"str"				
}
										--[不允许加额外的逗号
function person:eat(a,b)				--function person.eat(person,a,b)不采用冒号的话要把自身当成第一个参数传递;
	print(a+b)
end
//class person                              //通过类调用只是拷贝,无法修改lua里面的值。反之也不行
    //{
    //    public string v1;
    //    public string v2;
    //}
//person p = evn.Global.Get("person");
        //print(p.v1 + p.v2);
        //p.v1 = "cnm";
        //evn.DoString("print(person.v1)");
        //p.eat(3,4);
        // p.vv(3, 4);
 2,通过接口interface,需添加
    [CSharpCallLua] 特性
//[CSharpCallLua]
    //interface person                            //通过接口相当于是引用可以改变lua里面的值
    //{
    //    string v1 { get; set; }
    //    string v2 { get; set; }
    //    void eat(int a,int b);                  //函数名字必须和lua一致
    //    //void vv(int a,int b);
    //}
 3.通过字典或者数组,只能找到有key或者没有key的
//Dictionary obj = evn.Global.Get>("person");    //轻量级可以使用字典或者数组
        //foreach(string key in obj.Keys)
        //{

        //    print(key + "-" + obj[key]);
        //}

        //List lst = evn.Global.Get>("person");
        //foreach (object j in lst)
        //{
        //    print(j);
        //}
 4,通过内置luatable
//LuaTable tab = evn.Global.Get("person");                                      //通过内置luatable来接收
        //print(tab.Get("v1"));

3,访问function

1.用Action映射到delegate
//Action as1 = evn.Global.Get("add");                                           //委托与对应(代理)把lua函数映射到delegate
        //as1();
        //as1 = null;
2,自定义委托
[CSharpCallLua]                                                                             //委托必加特性,自定义委托
    delegate int Add(int a, int b,out int c);
//映射到delegate
        ////Add as2 = evn.Global.Get("add");
        ////int c1;
        ////int fann1=as2(20, 20,out c1);                                                           //lua中多返回值可以用out,ref 接收
        ////print(c1+"-"+fann1);
        ////as2 = null;
3,映射到LuaFunction,和luatable效率慢
LuaFunction func = evn.Global.Get("add");
        object[] os = func.Call(1, 2);
        foreach(object o in os)
        {
            print(o);
        }

4,luaCallCs

 先在cs引用lua程序
CS.UnityEngine.GameObject('first')   			--CS加命名空间加上类名
												--访问静态方法,通过类来调用
local gameObject=CS.UnityEngine.GameObject;		--先用局部变量引用类名,之后再用局部变量访问静态属性和方法以提高性能。相当于在CS中运行前先把lua的tab,fun先映射出来。

local camera=gameObject.Find("Main Camera");
camera.name="Kcam";
												--访问成员方法,通过对象来调用
local test=gameObject.Find("test");
local sound=test:GetComponent("Audio Source")	--使用冒号将当前对象作为第一个参数传递过去,lua没有this;

gameObject.Destroy(sound);
5关于输入输出
--[[

lua

	xxx(t1,t2,t3)						输入属性 普通算,ref算,out不算

		return re1,t3,t4,				输出属性  普通算,ref算,out算
	end


c#

	xxx(type t1,type t2,ref t3,out t4){

	return re1;
	}

--]]


























你可能感兴趣的:(unity)