unity3d C#用匿名委托循环注册按钮点击事件报错:索引超界 ArgumentOutOfRangeException: Index was out of range. Must be non-ne

unity3d C#用匿名委托循环注册按钮点击事件报错:索引超界 ArgumentOutOfRangeException: Index was out of range. Must be non-ne


项目场景:

就是依次给4个按钮注册点击事件


问题描述:

报错:超出索引界限
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index

我的代码:

 private new void Awake()
    {
        m_topContentButton = transform.Find("MainImage/TopButton/Button1").GetComponent<Button>();
        m_topPurposeButton = transform.Find("MainImage/TopButton/Button2").GetComponent<Button>();
        m_topTheoryButton = transform.Find("MainImage/TopButton/Button3").GetComponent<Button>();
        m_topMethodButton = transform.Find("MainImage/TopButton/Button4").GetComponent<Button>();
        m_imageContent = transform.Find("MainImage/ScrollViewContent/Viewport/Content/Image").GetComponent<Image>();
        m_gridLayoutGroups = transform.Find("MainImage/ScrollViewContent/Viewport/Content")
            .GetComponent<GridLayoutGroup>();
        topButtonList = new List<Button>
        {
            m_topContentButton,
            m_topPurposeButton,
            m_topTheoryButton,
            m_topMethodButton
        };
        spritesList = new List<Sprite>
        {
            sprite1,
            sprite2,
            sprite3,
            sprite4
        };
    }

    private void Start()
    {
        IChangeImage ichangeImage = new IntroduceController();
        //来循环注册按钮点击事件
        for (int i = 0; i <4; i++)
        {
            Debug.Log(i);
            topButtonList[i].onClick.AddListener(()=>ichangeImage.OnClockSetContent(spritesList[i]));
        }
        // m_topContentButton.onClick.AddListener(() => ichangeImage.OnClockSetContent(sprite1));
        // m_topPurposeButton.onClick.AddListener(() => ichangeImage.OnClockSetContent(sprite2));
        // m_topTheoryButton.onClick.AddListener(() => ichangeImage.OnClockSetContent(sprite3));
        // m_topMethodButton.onClick.AddListener(() => ichangeImage.OnClockSetContent(sprite4));
    }

用了循环加匿名委托去注册按钮点击事件


原因分析:

一开始没有头绪,就先用了下面注释掉的方法去注册事件了,后来想了下,应该是这出错了

topButtonList[i].onClick.AddListener(()=>ichangeImage.OnClockSetContent(spritesList[i]));

spritesList[i] 这超界了,直接填I,会导致最后一次循环会执行i++,这时候i就是4了,如果改成这样也是不行的

topButtonList[i].onClick.AddListener(()=>ichangeImage.OnClockSetContent(spritesList[i-1]));

即使减掉1,不超界了,但每次按钮点击的时候,调用的永远是最后一次循环的 spritesList[3]


解决方案:

先上代码:

 private void Start()
    {
        IChangeImage ichangeImage = new IntroduceController();
        //来循环注册按钮点击事件
        for (int i = 0; i <4; i++)
        {
            Debug.Log(i);
            int indexs=i;
            topButtonList[i].onClick.AddListener(()=>ichangeImage.OnClockSetContent(spritesList[indexs]));
        }
        // m_topContentButton.onClick.AddListener(() => ichangeImage.OnClockSetContent(sprite1));
        // m_topPurposeButton.onClick.AddListener(() => ichangeImage.OnClockSetContent(sprite2));
        // m_topTheoryButton.onClick.AddListener(() => ichangeImage.OnClockSetContent(sprite3));
        // m_topMethodButton.onClick.AddListener(() => ichangeImage.OnClockSetContent(sprite4));
    }

把i提成变量,所有的问题就都解决了。

总结

先写这些,欢迎大佬多多来给萌新指正,欢迎大家来共同探讨。
如果各位看官觉得文章有点点帮助,跪求各位给点个“一键三连”,谢啦~

声明一下:本博文章若非特殊注明皆为原创原文链接
https://blog.csdn.net/Wrinkle2017/article/details/109549878
————————————————————————————————

版权声明

版权声明:本博客为非营利性个人原创
所刊登的所有作品的著作权均为本人所拥有
本人保留所有法定权利,违者必究!
对于需要复制、转载、链接和传播博客文章或内容的
请及时和本博主进行联系
对于经本博主明确授权和许可使用文章及内容的
使用时请注明文章或内容出处并注明网址
转载请附上原文出处链接及本声明

  • 17
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值