XLua使用C#数组、列表、字典

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>();
}

数组

  1. 获取
    local obj=CS.Lesson3() 获取对象
    print(obj.array.Length) 获取对象中的数组
  2. 数组是userdata类型 使用和C#中一致
  3. 获取数组值
    print(obj.array[0])
  4. 遍历
    for i=0,obj.array.Length-1 do
    print(obj.array[i])
    end
  5. 创建数组
    int类型 长度为10
    local array=CS.System.Array.CreateInstance(typeof(CS.System.Int32),10)

列表

  1. 获取
    obj.list:Add(1)
    print(obj.list.Count)
    print(obj.list[0])
  2. 创建
旧版本:
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()

字典

  1. 获取
    obj.dic:Add(1,“123”)
    print(obj.dic[1])
  2. 遍历
    for k,v in pairs(obj.dic) do
    print(k,v)
    end
  3. 创建字典
    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);
  4. lua中创建的字典若键不是int类型 需要使用特殊的访问方式
    print(dic3:get_Item(“s”)) 访问
    dic3:set_Item(“s”,nil) 设置
    print(dic3:TryGetValue(“s”)) 返回bool值和结果
    print(dic3[“s”]) 常规访问为nil
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值