天天看點

神臨的uLua學習(五)

17.LuaArray(這是關于Array的uLua使用方式)

string source = @"
        function luaFunc(objs, len)
            for i = 0, len - 1 do
                print(objs[i])
            end
            local table1 = {'111', '222', '333'}
            return unpack(table1)
        end
    ";

    string[] objs = { "aaa", "bbb", "ccc" };

    // Use this for initialization
    void Start() {
        LuaScriptMgr luaMgr = new LuaScriptMgr();
        luaMgr.Start();
        LuaState l = luaMgr.lua;
        l.DoString(source);

        //c# array to lua table
        LuaFunction f = l.GetFunction("luaFunc");
        object[] rs = f.Call(objs, objs.Length);
        f.Release();

        //lua table to c# array
        foreach (object de in rs) {
            Debug.Log(de.ToString());
        }
    }
           

難度不大,有優秀C#基礎或則有其他程式設計能力的人應該很容易就會看懂.

18.LuaEnum(這是關于Enum在uLua的使用)

const string source = @"
        local type = LuaEnumType.IntToEnum(1);
        print(type == LuaEnumType.AAA);
    ";

    // Use this for initialization
    void Start () {
        LuaScriptMgr mgr = new LuaScriptMgr();
        mgr.Start();
        mgr.lua.DoString(source);
    }
           

下面是輔助類

public enum LuaEnumType {
    AAA = ,
    BBB = ,
    CCC = ,
    DDD = 
}
           

相信你會說 So Easy

19.LuaClass(這是uLua關于類的使用方式)

const string source = @"
        Account = { balance = 0 };

        function Account:new(o)    
            o = o or {};
            setmetatable(o, { __index = self });     
            return o;  
        end  

        function Account.deposit(self, v)  
            self.balance = self.balance + v;  
        end  

        function Account:withdraw(v)  
            if (v) > self.balance then error 'insufficient funds'; end  
            self.balance = self.balance - v;  
        end 

        SpecialAccount = Account:new();

        function SpecialAccount:withdraw(v)  
            if v - self.balance >= self:getLimit() then  
                error 'insufficient funds';  
            end  
            self.balance = self.balance - v;  
        end  

        function SpecialAccount.getLimit(self)  
            return self.limit or ;  
        end  

        s = SpecialAccount:new{ limit =  };
        print(s.balance);  
        s:deposit();

        print (s.limit);  
        print (s.getLimit(s))  
        print (s.balance)  
    ";

    void Start () {
        LuaScriptMgr mgr = new LuaScriptMgr();
        mgr.Start();
        mgr.lua.DoString(source);
    }

           

相信大家對于類都耳熟能詳了,對比着學就行.

20.Debugger(觀名知意,就不多說了)

void Start () {
        //LuaState l = new LuaState();
        //LuaDLL.luaopen_socket_core(l.L);
        //l.DoFile("C:/Users/Administrator/Documents/New Unity Project/Assets/uLua/Lua/debugger.lua");

        LuaScriptMgr mgr = new LuaScriptMgr();
        mgr.Start();
        mgr.DoFile("debugger");
    }
           

21.LuaCall(這裡是函數的使用方式)

const string script = @"
        A_LuaCall = luanet.import_type('A_LuaCall')  

        LuaClass = {}
        LuaClass.__index = LuaClass

        function LuaClass:New() 
            local self = {};   
            setmetatable(self, LuaClass); 
            return self;    
        end

        function LuaClass:test() 
            A_LuaCall.OnSharpCall(self, self.callback);
        end

        function LuaClass:callback()
            print('test--->>>');
        end

        LuaClass:New():test();
    ";

    void Start () {
        LuaState lua = new LuaState();
        lua.DoString(script);
    }

    public static void OnSharpCall(LuaTable self, LuaFunction func) {
        func.Call(self);
    } 這裡我還沒有使用過,有時間的話再試試喽
           

好了,關于uLua的整理就到這裡了,寫了這麼多,也是夠麻煩的,尤其是對于我這樣的人.

下面整理Shader的知識吧,謝謝.