C#/문제해결

0727 delegate 발생 시점

버스창문 2022. 7. 27. 17:19
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TabBtn : MonoBehaviour
{
    public Button[] tabs;
    public Text[] tabTexts;
    public Image focus;
    public Image[] checkDots;
    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < this.tabs.Length; i++)
        {
            this.tabs[i].onClick.AddListener(() =>
            {
                foreach (Text tabtext in this.tabTexts)
                {
                    tabtext.color = new Color(178 / 255f, 171 / 255f, 178 / 255f);
                }
                this.tabTexts[i].color = new Color(240 / 255f, 220 / 255f, 156 / 255f);
                var pos = this.focus.transform.position;
                pos.x = this.tabs[i].transform.position.x;
                this.focus.transform.position = pos;
            });
        }

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

위의 코드는 에러가 난다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TabBtn : MonoBehaviour
{
    public Button[] tabs;
    public Text[] tabTexts;
    public Image focus;
    public Image[] checkDots;
    // Start is called before the first frame update
    void Start()
    {

        this.tabs[0].onClick.AddListener(() =>
        {
            foreach (Text tabtext in this.tabTexts)
            {
                tabtext.color = new Color(178 / 255f, 171 / 255f, 178 / 255f);
            }
            this.tabTexts[0].color = new Color(240 / 255f, 220 / 255f, 156 / 255f); //F0DC9C    //B2ABB2
            var pos = this.focus.transform.position;
            pos.x = this.tabs[0].transform.position.x;
            this.focus.transform.position = pos;
        });
        this.tabs[1].onClick.AddListener(() =>
        {
            foreach (Text tabtext in this.tabTexts)
            {
                tabtext.color = new Color(178 / 255f, 171 / 255f, 178 / 255f);
            }
            this.tabTexts[1].color = new Color(240 / 255f, 220 / 255f, 156 / 255f);
            var pos = this.focus.transform.position;
            pos.x = this.tabs[1].transform.position.x;
            this.focus.transform.position = pos;
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

근데 그냥 풀어서 복붙했을뿐인 이 코드는 에러가 나지않는다.

 

delegate가 언제 일어나는지와 관련이 있는데,

 

for (int i = 0; i < this.tabs.Length; i++)
        {
            this.tabs[i].onClick.AddListener(() =>
            {
                foreach (Text tabtext in this.tabTexts)
                {
                    tabtext.color = new Color(178 / 255f, 171 / 255f, 178 / 255f);
                }
                this.tabTexts[i].color = new Color(240 / 255f, 220 / 255f, 156 / 255f);
                var pos = this.focus.transform.position;
                pos.x = this.tabs[i].transform.position.x;
                this.focus.transform.position = pos;
            });
        }

해당 코드에서 AddListener는 for문 한번이 다 끝난뒤 i++되고 나서 전달되게 된다.

즉 0이 들어갈거라고 생각한건 1로 들어가고, 1이 들어갈거라고 생각한건 2가 들어가서 OutOfIndex가 일어나게 된다.

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TabBtn : MonoBehaviour
{
    public Button[] tabs;
    public Text[] tabTexts;
    public Image focus;
    public Image[] checkDots;
    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < this.tabs.Length; i++)
        {
            int idx = i;
            this.tabs[idx].onClick.AddListener(() =>
            {
                foreach (Text tabtext in this.tabTexts)
                {
                    tabtext.color = new Color(178 / 255f, 171 / 255f, 178 / 255f);
                }
                this.tabTexts[idx].color = new Color(240 / 255f, 220 / 255f, 156 / 255f);
                var pos = this.focus.transform.position;
                pos.x = this.tabs[idx].transform.position.x;
                this.focus.transform.position = pos;
            });
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

따로 변수 idx 를 만들어서 i를 그곳에 보관한다음 AddListener를 idx를 이용해서 전달하게 되면 원하는 형태로 잘 전달되게 된다.

'C# > 문제해결' 카테고리의 다른 글

0729 별 순서대로 나오게하기  (0) 2022.07.29
0727 UI image의 enable  (0) 2022.07.27
0726 Ray에 닿은것 내용 가져오는법  (0) 2022.07.26
0719 백터 등에 수치 못넣는거 해결하는법  (0) 2022.07.19
0719 singleton  (0) 2022.07.19