神临的uLua学习(一)

这里我要将一些简单的uLua学习内容纪录并分享一下.
1.HelloWorld(就像一开始学习编程语言一样,先写一个HelloWorld吧)

void Start () {
        LuaState l = new LuaState();//建立一个新的Lua状态
        string str = "print('hello world 世界')";//建立string
        l.DoString(str);//状态机执行
    }

2.CreateGameObject_1(这里记录了游戏对象在uLua的创建方式.这是第一个方式)

 private string script = @"
luanet.load_assembly('UnityEngine')--调用unity引擎
            GameObject =        luanet.import_type('UnityEngine.GameObject')--调用unity中的GameObject对象        
            ParticleSystem = luanet.import_type('UnityEngine.ParticleSystem')--调用粒子系统         
            local newGameObj = GameObject('NewObj')
newGameObj:AddComponent(luanet.ctype(ParticleSystem))--新建对象并添加组件
        ";

    //反射调用
    void Start () {
        LuaState lua = new LuaState();
        lua.DoString(script);//略
    }

3.CreateGameObject_2(这里记录了游戏对象在uLua的创建方式.这是第二个方式)

  private string script = @"
            luanet.load_assembly('UnityEngine')
            GameObject = UnityEngine.GameObject
            ParticleSystem = UnityEngine.ParticleSystem
            local newGameObj = GameObject('NewObj')
            newGameObj:AddComponent(ParticleSystem.GetClassType())
        ";

    //非反射调用
    void Start () {
        LuaScriptMgr lua = new LuaScriptMgr();
        lua.Start();
        lua.DoString(script);
    }

这里再解释一下反射调用和非反射调用:
反射调用方式是不推荐使用的,因为效率慢,推荐的是使用wrap(可以从Lua的组件中找到,这里不做介绍)的去反射模式,这里还继续保留下来的原因在于,在某些特定环境下,反射还是有用途的,去反射最大的弊端在于提前需要把C#的类导入到Lua中,如果上线了发现有些类没有导入,反射就可以通过临时的调用未wrap的类,进行使用,当大版本更新时,再将此类加入wrap,这时候反射就是解决这种情况出现,所以概率小,1%的可能性,但并不代表不存在。

4.LuaVariables_1(这里是Lua变量的使用方式1)

private string script = @"
            luanet.load_assembly('UnityEngine')
            GameObject = luanet.import_type('UnityEngine.GameObject')
            ParticleSystem = luanet.import_type('UnityEngine.ParticleSystem')--略
            particles = {}
for i = 1, Objs2Spawn, 1 do  --for循环开始
                local newGameObj = GameObject('NewObj' .. tostring(i))
                local ps = newGameObj:AddComponent(luanet.ctype(ParticleSystem))
                --local ps = newGameObj:AddComponent('ParticleSystem') PS:Unity5.x已经废弃这种方式,Unity4.x可用--
                ps:Stop()

                table.insert(particles, ps)
            end  --循环结束

            var2read = 42
        ";

void Start () {
        LuaState l = new LuaState();
        l["Objs2Spawn"] = 5;//对Objs2Spawn的key添加value
        l.DoString(script);//执行
        print("Read from lua: " + l["var2read"].ToString());
        LuaTable particles = (LuaTable)l["particles"];
        foreach( ParticleSystem ps in particles.Values )
        {
            ps.Play();
        }
    }

说明一下这里的”- -“在Lua里是注释的意思

5.LuaVariables_2(这里是Lua变量的使用方式2)

 //对于cstolua的使用要求必须要先定义变量才能使用
    private string var = @"Objs2Spawn = 0";
    private string script = @"      
            ParticleSystem = UnityEngine.ParticleSystem
            particles = {}

            for i = 1, Objs2Spawn, 1 do
                local newGameObj = GameObject('NewObj' .. tostring(i))
                local ps = newGameObj:AddComponent(ParticleSystem.GetClassType())
                ps:Stop()

                table.insert(particles, ps)--lua从尾部嵌入
            end

            var2read = 42
        ";

    // Use this for initialization
    void Start () {        
        LuaScriptMgr mgr = new LuaScriptMgr();//建立管理器
        mgr.Start();//运行
        LuaState l = mgr.lua;//状态
        l.DoString(var);
        l["Objs2Spawn"] = 5;
        l.DoString(script);//略

        print("Read from lua: " + l["var2read"].ToString());
        LuaTable particles = (LuaTable)l["particles"];
        foreach( ParticleSystem ps in particles.Values )
        {
            ps.Play();
        }
    }

6.ScriptsFromFile_01(这里是从文件中调用的方法)

public TextAsset scriptFile;//从外部挂载Lua脚本

void Start()
    {
        LuaState l = new LuaState();
        l.DoString(scriptFile.text);
    }

7.ScriptsFromFile_02(从内部加载的方法)

void Start()
    {
        //只是展示如何加载文件,不推荐这么做
        LuaState l = new LuaState();
        string path = Application.dataPath + "/uLua/Examples/04_ScriptsFromFile/ScriptsFromFile02.lua";        
        l.DoFile(path);
    }

8.CallLuaFunction_01(使用运行函数)

private string script = @"
            function luaFunc(message)
                print(message)
                return 42
            end
        ";


void Start () {
        LuaState l = new LuaState();
        l.DoString(script);
        LuaFunction f = l.GetFunction("luaFunc");
        object[] r = f.Call("I called a lua function!");
        print(r[0]);//这里不多解释,认真看就行
    }

如果觉得看着费劲可以去到这里http://www.runoob.com/lua/lua-tutorial.html学习一下Lua基础

9.CallLuaFunction_02(第二种方式)

private string script = @"
            function luaFunc(num)                
                return num
            end
        ";

    LuaFunction func = null;


void Start () {
        LuaScriptMgr mgr = new LuaScriptMgr();
        mgr.DoString(script);
        func = mgr.GetLuaFunction("luaFunc");
        object[] r = func.Call(123456);        
        print(r[0]);
        int num = CallFunc();
        print(num);
    }

    void OnDestroy()
    {
        if (func != null)
        {
            func.Release();
        }
    }

    int CallFunc()
    {
        int top = func.BeginPCall();
        IntPtr L = func.GetLuaState();
        LuaScriptMgr.Push(L, 123456);
        func.PCall(top, 1);
        int num = (int)LuaScriptMgr.GetNumber(L, -1);
        func.EndPCall(top);
        return num;
    }//这里代码就较多了,不过并不难,然真看就行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值