C#:
public class Lesson3
{
public int[] array = new int[] { 1, 2, 3, 3 };
public List<int> list = new List<int>();
public Dictionary<int, string> dic = new Dictionary<int, string>();
}
数组
- 获取
local obj=CS.Lesson3() 获取对象
print(obj.array.Length) 获取对象中的数组 - 数组是userdata类型 使用和C#中一致
- 获取数组值
print(obj.array[0]) - 遍历
for i=0,obj.array.Length-1 do
print(obj.array[i])
end - 创建数组
int类型 长度为10
local array=CS.System.Array.CreateInstance(typeof(CS.System.Int32),10)
列表
- 获取
obj.list:Add(1)
print(obj.list.Count)
print(obj.list[0]) - 创建
旧版本:
local list2=CS.System.Collections.Generic["List`1[System.String]"]()
新版本:
获取 一个List<T>的类 然后实例化
1. 获取List<string>
local list_String=CS.System.Collections.Generic.List(CS.System.String)
2. 实例化
local list3=list_String()
字典
- 获取
obj.dic:Add(1,“123”)
print(obj.dic[1]) - 遍历
for k,v in pairs(obj.dic) do
print(k,v)
end - 创建字典
local Dic_String_Vector3=CS.System.Collections.Generic.Dictionary(CS.System.String,CS.UnityEngine.Vector3)
local dic3=Dic_String_Vector3()
dic3:Add(“s”,CS.UnityEngine.Vector3.right); - lua中创建的字典若键不是int类型 需要使用特殊的访问方式
print(dic3:get_Item(“s”)) 访问
dic3:set_Item(“s”,nil) 设置
print(dic3:TryGetValue(“s”)) 返回bool值和结果
print(dic3[“s”]) 常规访问为nil