9.C#把Lua表映射到数组和Dictionary

9.CSharp调用Lua-表映射到数组和Dictionary


9.1 知识点

准备工作

初始化Lua管理器,创建Lua脚本并在C#脚本调用

LuaManager.Instance.Init();
LuaManager.Instance.Require("Lesson09_CSharp调用Lua_表映射到数组和Dictionary");

创建lua脚本

print('我是Lua脚本 Lesson09_CSharp调用Lua_表映射到数组和Dictionary')

--数组
testArray1 = {1,2,3,4,5,6}
testArray2 = {"123", "123", true, 1, 1.2}

--Dictionary
testDictionary1 = {
    ["1"] = 1,
    ["2"] = 2,
    ["3"] = 3,
    ["4"] = 4
}
testDictionary2 = {
    ["1"] = 111111,
    [true] = 666,
    [false] = 999,
    ["123"] = 123123123
}

表映射到数组

通过LuaState的GetTable方法获得同一类型的Lua表

LuaTable testArray11 = LuaManager.Instance.LuaState.GetTable("testArray1");

//打印表中的值
Debug.Log(testArray11[1]);//1
Debug.Log(testArray11[2]);//2
Debug.Log(testArray11[3]);//3
Debug.Log(testArray11[4]);//4
Debug.Log(testArray11[5]);//5
Debug.Log(testArray11[6]);//6

//转成数组后遍历
object[] testArray11Array = testArray11.ToArray();
for (int i = 0; i < testArray11Array.Length; ++i)
{
    Debug.Log($"遍历testArray11Array key:{i} value:{testArray11Array[i]}");
}

//改变C#中Lua表值会影响Lua中表的值 引用拷贝
testArray11[1] = 999;
LuaTable testArray12 = LuaManager.Instance.LuaState.GetTable("testArray1");
Debug.Log(testArray12[1]);//999

通过LuaState的GetTable方法获得不同类型的Lua表

LuaTable testArray2 = LuaManager.Instance.LuaState.GetTable("testArray2");

//打印表中的值
Debug.Log(testArray2[1]);//123
Debug.Log(testArray2[2]);//123
Debug.Log(testArray2[3]);//True
Debug.Log(testArray2[4]);//1
Debug.Log(testArray2[5]);//1.2

//转成数组后遍历
object[] testArray12Array = testArray2.ToArray();
for (int i = 0; i < testArray12Array.Length; ++i)
{
    Debug.Log($"遍历testArray12Array key:{i} value:{testArray12Array[i]}");
}

表映射到Dictionary

通过LuaState的GetTable方法获得同一类型的Lua字典

LuaTable testDictionary11 = LuaManager.Instance.LuaState.GetTable("testDictionary1");

//Lua字典中的键是字符串可以直接传入
Debug.Log(testDictionary11["1"]);//1
Debug.Log(testDictionary11["2"]);//2
Debug.Log(testDictionary11["3"]);//3
Debug.Log(testDictionary11["4"]);//4

//改变C#中Lua字典的值会影响Lua中表的值 引用拷贝
testDictionary11["1"] = 999;
LuaTable testDictionary12 = LuaManager.Instance.LuaState.GetTable("testDictionary1");
Debug.Log(testDictionary12["1"]);//999

//转成Dictionary后遍历 不一定按顺序
LuaDictTable<string, int> testDictionary11Dictionary = testDictionary11.ToDictTable<string, int>();
foreach (var pair in testDictionary11Dictionary)
{
    string key = pair.Key;
    int value = pair.Value;
    Debug.Log($"遍历testDictionary11Dictionary key:{key} value:{value}");
}

通过LuaState的GetTable方法获得不同类型的Lua字典

LuaTable testDictionary2 = LuaManager.Instance.LuaState.GetTable("testDictionary2");

//Lua字典中的键不是字符串需要转成C#字典再获取值
Debug.Log(testDictionary2["1"]);//111111
//Debug.Log(testDictionary2[true]);//编译报错
//Debug.Log(testDictionary2[false]);//编译报错

//转成Dictionary后直接打印或遍历
LuaDictTable<object, object> testDictionary2Dictionary = testDictionary2.ToDictTable<object, object>();
Debug.Log(testDictionary2Dictionary["1"]);//111111
Debug.Log(testDictionary2Dictionary[true]);//666
Debug.Log(testDictionary2Dictionary[false]);//999
Debug.Log(testDictionary2Dictionary["123"]);//123123123
foreach (var pair in testDictionary2Dictionary)
{
    object key = pair.Key;
    object value = pair.Value;
    Debug.Log($"遍历testDictionary2Dictionary key:{key} value:{value}");
}

//迭代器遍历
IEnumerator<LuaDictEntry<object, object>> testDictionary2DictionaryIEnumerator = testDictionary2Dictionary.GetEnumerator();
while (testDictionary2DictionaryIEnumerator.MoveNext())
{
    object key = testDictionary2DictionaryIEnumerator.Current.Key;
    object value = testDictionary2DictionaryIEnumerator.Current.Value;
    Debug.Log($"迭代器遍历testDictionary2Dictionary key:{key} value:{value}");
}

9.2 知识点代码

Lesson09_CSharp调用Lua_表映射到数组和Dictionary.lua

print('我是Lua脚本 Lesson09_CSharp调用Lua_表映射到数组和Dictionary')

--数组
testArray1 = {1,2,3,4,5,6}
testArray2 = {"123", "123", true, 1, 1.2}

--Dictionary
testDictionary1 = {
    ["1"] = 1,
    ["2"] = 2,
    ["3"] = 3,
    ["4"] = 4
}
testDictionary2 = {
    ["1"] = 111111,
    [true] = 666,
    [false] = 999,
    ["123"] = 123123123
}

Lesson09_CSharp调用Lua_表映射到数组和Dictionary

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

public class Lesson09_CSharp调用Lua_表映射到数组和Dictionary : MonoBehaviour
{
    void Start()
    {
        LuaManager.Instance.Init();
        LuaManager.Instance.Require("Lesson09_CSharp调用Lua_表映射到数组和Dictionary");

        #region 知识点一 表映射到数组

        //通过LuaState的GetTable方法获得同一类型的Lua表
        LuaTable testArray11 = LuaManager.Instance.LuaState.GetTable("testArray1");

        //打印表中的值
        Debug.Log(testArray11[1]);//1
        Debug.Log(testArray11[2]);//2
        Debug.Log(testArray11[3]);//3
        Debug.Log(testArray11[4]);//4
        Debug.Log(testArray11[5]);//5
        Debug.Log(testArray11[6]);//6

        //转成数组后遍历
        object[] testArray11Array = testArray11.ToArray();
        for (int i = 0; i < testArray11Array.Length; ++i)
        {
            Debug.Log($"遍历testArray11Array key:{i} value:{testArray11Array[i]}");
        }

        //改变C#中Lua表值会影响Lua中表的值 引用拷贝
        testArray11[1] = 999;
        LuaTable testArray12 = LuaManager.Instance.LuaState.GetTable("testArray1");
        Debug.Log(testArray12[1]);//999


        //通过LuaState的GetTable方法获得不同类型的Lua表
        LuaTable testArray2 = LuaManager.Instance.LuaState.GetTable("testArray2");

        //打印表中的值
        Debug.Log(testArray2[1]);//123
        Debug.Log(testArray2[2]);//123
        Debug.Log(testArray2[3]);//True
        Debug.Log(testArray2[4]);//1
        Debug.Log(testArray2[5]);//1.2

        //转成数组后遍历
        object[] testArray12Array = testArray2.ToArray();
        for (int i = 0; i < testArray12Array.Length; ++i)
        {
            Debug.Log($"遍历testArray12Array key:{i} value:{testArray12Array[i]}");
        }

        #endregion

        #region 知识点二 表映射到Dictionary

        //通过LuaState的GetTable方法获得同一类型的Lua字典
        LuaTable testDictionary11 = LuaManager.Instance.LuaState.GetTable("testDictionary1");

        //Lua字典中的键是字符串可以直接传入
        Debug.Log(testDictionary11["1"]);//1
        Debug.Log(testDictionary11["2"]);//2
        Debug.Log(testDictionary11["3"]);//3
        Debug.Log(testDictionary11["4"]);//4

        //改变C#中Lua字典的值会影响Lua中表的值 引用拷贝
        testDictionary11["1"] = 999;
        LuaTable testDictionary12 = LuaManager.Instance.LuaState.GetTable("testDictionary1");
        Debug.Log(testDictionary12["1"]);//999

        //转成Dictionary后遍历 不一定按顺序
        LuaDictTable<string, int> testDictionary11Dictionary = testDictionary11.ToDictTable<string, int>();
        foreach (var pair in testDictionary11Dictionary)
        {
            string key = pair.Key;
            int value = pair.Value;
            Debug.Log($"遍历testDictionary11Dictionary key:{key} value:{value}");
        }


        //通过LuaState的GetTable方法获得不同类型的Lua字典
        LuaTable testDictionary2 = LuaManager.Instance.LuaState.GetTable("testDictionary2");

        //Lua字典中的键不是字符串需要转成C#字典再获取值
        Debug.Log(testDictionary2["1"]);//111111
        //Debug.Log(testDictionary2[true]);//编译报错
        //Debug.Log(testDictionary2[false]);//编译报错

        //转成Dictionary后直接打印或遍历
        LuaDictTable<object, object> testDictionary2Dictionary = testDictionary2.ToDictTable<object, object>();
        Debug.Log(testDictionary2Dictionary["1"]);//111111
        Debug.Log(testDictionary2Dictionary[true]);//666
        Debug.Log(testDictionary2Dictionary[false]);//999
        Debug.Log(testDictionary2Dictionary["123"]);//123123123
        foreach (var pair in testDictionary2Dictionary)
        {
            object key = pair.Key;
            object value = pair.Value;
            Debug.Log($"遍历testDictionary2Dictionary key:{key} value:{value}");
        }

        //迭代器遍历
        IEnumerator<LuaDictEntry<object, object>> testDictionary2DictionaryIEnumerator = testDictionary2Dictionary.GetEnumerator();
        while (testDictionary2DictionaryIEnumerator.MoveNext())
        {
            object key = testDictionary2DictionaryIEnumerator.Current.Key;
            object value = testDictionary2DictionaryIEnumerator.Current.Value;
            Debug.Log($"迭代器遍历testDictionary2Dictionary key:{key} value:{value}");
        }

        #endregion
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏