1. 程式人生 > >ulua與unity互傳陣列

ulua與unity互傳陣列

using UnityEngine;
using System.Collections;
using LuaInterface;
using System;

public class LuaTest : MonoBehaviour
{
    private string lua = @"
        --定義一個函式
        function luaFunc(objs, len)
            for i = 0, len - 1 do
                print(objs[i])
            end
            --返回一個列表
            local table1 = {'111', '222', '333'}
            return table1
        end
    ";

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

    void Start ()
    {
        LuaScriptMgr luaMgr = new LuaScriptMgr();
        LuaState luaState = luaMgr.lua;
        luaState.DoString(lua);

        //呼叫lua的函式獲取返回值
        LuaFunction f = luaState.GetFunction("luaFunc");
        object[] rs = f.Call(objs, objs.Length);

        //輸出lua的返回值
        LuaTable table = rs[0] as LuaTable;
        foreach (DictionaryEntry de in table)
        {
            Debug.Log(de.Value);
        }
    }
}