ToLua 入门05_AccessingLuaVariables

Tolua中的变量怎么去使用和赋值,在案例场景ToLua/Examples/04_AccessingLuaVariables中可以去学习;本章我们就分析下具体使用方式,首先先看一下提供的示范代码。

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

public class AccessingLuaVariables : MonoBehaviour 
{
    private string script =
        @"
            print('Objs2Spawn is: '..Objs2Spawn)
            var2read = 42
            varTable = {1,2,3,4,5}
            varTable.default = 1
            varTable.map = {}
            varTable.map.name = 'map'
            
            meta = {name = 'meta'}
            setmetatable(varTable, meta)
            
            function TestFunc(strs)
                print('get func by variable')
            end
        ";

	void Start () 
    {
#if UNITY_5 || UNITY_2017 || UNITY_2018
        Application.logMessageReceived += ShowTips;
#else
        Application.RegisterLogCallback(ShowTips);
#endif
        new LuaResLoader();
        LuaState lua = new LuaState();
        lua.Start();
        lua["Objs2Spawn"] = 5;
        lua.DoString(script);

        //通过LuaState访问
        Debugger.Log("Read var from lua: {0}", lua["var2read"]);
        Debugger.Log("Read table var from lua: {0}", lua["varTable.default"]);  //LuaState 拆串式table

        LuaFunction func = lua["TestFunc"] as LuaFunction;
        func.Call();
        func.Dispose();

        //cache成LuaTable进行访问
        LuaTable table = lua.GetTable("varTable");
        Debugger.Log("Read varTable from lua, default: {0} name: {1}", table["default"], table["map.name"]);
        table["map.name"] = "new";  //table 字符串只能是key
        Debugger.Log("Modify varTable name: {0}", table["map.name"]);

        table.AddTable("newmap");
        LuaTable table1 = (LuaTable)table["newmap"];
        table1["name"] = "table1";
        Debugger.Log("varTable.newmap name: {0}", table1["name"]);
        table1.Dispose();

        table1 = table.GetMetaTable();

        if (table1 != null)
        {
            Debugger.Log("varTable metatable name: {0}", table1["name"]);
        }

        object[] list = table.ToArray();

        for (int i = 0; i < list.Length; i++)
        {
            Debugger.Log("varTable[{0}], is {1}", i, list[i]);
        }

        table.Dispose();                        
        lua.CheckTop();
        lua.Dispose();
	}

    private void OnApplicationQuit()
    {
#if UNITY_5 || UNITY_2017 || UNITY_2018
        Application.logMessageReceived -= ShowTips;
#else
        Application.RegisterLogCallback(null);
#endif
    }

    string tips = null;

    void ShowTips(string msg, string stackTrace, LogType type)
    {
        tips += msg;
        tips += "\r\n";
    }

    void OnGUI()
    {
        GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 200, 600, 400), tips);
    }
}

首先第一步初始化lua脚本:

    new LuaResLoader();
    LuaState lua = new LuaState();
    lua.Start();
    lua["Objs2Spawn"] = 5; //对脚本中Objs2Spawn变量进行赋值。
    lua.DoString(script);

在初始化时可以直接获取变量对lua脚本中变量赋值。

第二步访问变量:

    //通过LuaState访问
    Debugger.Log("Read var from lua: {0}", lua["var2read"]);
    Debugger.Log("Read table var from lua: {0}", lua["varTable.default"]);  //LuaState 拆串式table

第三步调用脚本方法

    LuaFunction func = lua["TestFunc"] as LuaFunction;
    func.Call();
    func.Dispose();

第四步读取的内容转为luaTable

    LuaTable table = lua.GetTable("varTable");
    Debugger.Log("Read varTable from lua, default: {0} name: {1}", table["default"], table["map.name"]);
    table["map.name"] = "new";  //varTable.map.name 变量改变
    //改变后打印varTable.map.name 变量
    Debugger.Log("Modify varTable name: {0}", table["map.name"]);

    //table  添加新的luaTable 
    table.AddTable("newmap");
    LuaTable table1 = (LuaTable)table["newmap"]; 
    //新的luaTable 表加入字段(变量)
    table1["name"] = "table1";
    //打印测试是否成功
    Debugger.Log("varTable.newmap name: {0}", table1["name"]);
    table1.Dispose();//资源回收

获取元表

    table1 = table.GetMetaTable();

    if (table1 != null)
    {
        Debugger.Log("varTable metatable name: {0}", table1["name"]);
    }

第五步获取数组并打印

    object[] list = table.ToArray();

    for (int i = 0; i < list.Length; i++)
    {
        Debugger.Log("varTable[{0}], is {1}", i, list[i]);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值