8.CSharp调用Lua-表映射到List和Dictionary
8.1 知识点
准备工作
初始化Lua管理器,创建Lua脚本并在C#脚本调用
// 初始化Lua管理器
LuaManager.Instance.Init();
// 在C#脚本中调用Lua脚本
LuaManager.Instance.DoLuaFile("Lesson08_CSharp调用Lua_表映射到List和Dictionary");
给Lua脚本添加不同类型的表
print('我是Lua脚本 Lesson08_CSharp调用Lua_表映射到List和Dictionary')
-- List
testList1 = {1,2,3,4,5,6}
testList2 = {"123", "123", true, 1, 1.2}
-- Dictionary
testDictionary1 = {
["1"] = 1,
["2"] = 2,
["3"] = 3,
["4"] = 4
}
testDictionary2 = {
["1"] = 1,
[true] = 1,
[false] = true,
["123"] = false
}
表映射到List
获得同一类型的List
// 获得同一类型的List
List<int> testList11 = LuaManager.Instance.Global.Get<List<int>>("testList1");
for (int i = 0; i < testList11.Count; ++i)
{
Debug.Log(testList11[i]);
}
// 改变C#的List值不会影响Lua中表的值
testList11[0] = 100;
List<int> testList12 = LuaManager.Instance.Global.Get<List<int>>("testList1");
Debug.Log(testList12[0]);
获得不指定类型的List
// 获得不指定类型的List 使用object类型数组
List<object> testList2 = LuaManager.Instance.Global.Get<List<object>>("testList2");
for (int i = 0; i < testList2.Count; ++i)
{
Debug.Log(testList2[i]);
}
表映射到Dictionary
获得同一类型键值的Dictionary
// 获得同一类型键值的Dictionary
Dictionary<string, int> testDictionary11 = LuaManager.Instance.Global.Get<Dictionary<string, int>>("testDictionary1");
foreach (string item in testDictionary11.Keys)
{
Debug.Log(item + "_" + testDictionary11[item]);
}
// 改变C#的Dictionary值不会影响Lua中表的值
testDictionary11["1"] = 100000;
Dictionary<string, int> testDictionary12 = LuaManager.Instance.Global.Get<Dictionary<string, int>>("testDictionary1");
Debug.Log(testDictionary12["1"]);
获得不指定键值类型的Dictionary 使用object类型作为键值
// 获得不指定键值类型的Dictionary 使用object类型作为键值
Dictionary<object, object> testDictionary2 = LuaManager.Instance.Global.Get<Dictionary<object, object>>("testDictionary2");
foreach (object item in testDictionary2.Keys)
{
Debug.Log(item + "_" + testDictionary2[item]);
}
8.2 知识点代码
Lesson08_CSharp调用Lua_表映射到List和Dictionary.lua
print('我是Lua脚本 Lesson08_CSharp调用Lua_表映射到List和Dictionary')
--List
testList1 = {1,2,3,4,5,6}
testList2 = {"123", "123", true, 1, 1.2}
--Dictionary
testDictionary1 = {
["1"] = 1,
["2"] = 2,
["3"] = 3,
["4"] = 4
}
testDictionary2 = {
["1"] = 1,
[true] = 1,
[false] = true,
["123"] = false
}
Lesson08_CSharp调用Lua_表映射到List和Dictionary
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lesson08_CSharp调用Lua_表映射到List和Dictionary : MonoBehaviour
{
void Start()
{
LuaManager.Instance.Init();
LuaManager.Instance.DoLuaFile("Lesson08_CSharp调用Lua_表映射到List和Dictionary");
#region 知识点一 表映射到List
//获得同一类型的List
List<int> testList11 = LuaManager.Instance.Global.Get<List<int>>("testList1");
for (int i = 0; i < testList11.Count; ++i)
{
Debug.Log(testList11[i]);
}
//改变C#的List值不会影响Lua中表的值
testList11[0] = 100;
List<int> testList12 = LuaManager.Instance.Global.Get<List<int>>("testList1");
Debug.Log(testList12[0]);
//获得不指定类型的List 使用object类型数组
List<object> testList2 = LuaManager.Instance.Global.Get<List<object>>("testList2");
for (int i = 0; i < testList2.Count; ++i)
{
Debug.Log(testList2[i]);
}
#endregion
#region 知识点二 表映射到Dictionary
//获得同一类型键值的Dictionary
Dictionary<string, int> testDictionary11 = LuaManager.Instance.Global.Get<Dictionary<string, int>>("testDictionary1");
foreach (string item in testDictionary11.Keys)
{
Debug.Log(item + "_" + testDictionary11[item]);
}
//改变C#的Dictionary值不会影响Lua中表的值
testDictionary11["1"] = 100000;
Dictionary<string, int> testDictionary12 = LuaManager.Instance.Global.Get<Dictionary<string, int>>("testDictionary1");
Debug.Log(testDictionary12["1"]);
//获得不指定键值类型的Dictionary 使用object类型作为键值
Dictionary<object, object> testDictionary2 = LuaManager.Instance.Global.Get<Dictionary<object, object>>("testDictionary2");
foreach (object item in testDictionary2.Keys)
{
Debug.Log(item + "_" + testDictionary2[item]);
}
#endregion
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com