Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

c# - How to match 2 lists with the same index position?

Using c# hi, i need to match 2 list, to asing 1 valor from 1 list, to the other list at the same range.

is something like this :

public void SetText(TextMeshProUGUI texto )
{
    string mensaje;
    
        foreach (Collider2D x in collidersTextoEmergente)
    {
        nombreColider.Add(x.name);

        for (int i = 0; i < nombreColider.Count; i++)
        {
            for (int b = 0; i < msgEmergentes.Count; i++)
                  mensaje = msgEmergentes[b];

            switch (nombreColider[i])
            {
                case "basura":
                    texto.text = mensaje;
                    break;
                case "DinnerTime":
                    texto.text = mensaje;
                    break;

                case "atomo":
                    texto.text = mensaje;
                    break;
            }
        }
    }
}

is a relly mess.... I want my function reconice the trigger or collider name and set the text automatically , the text that i already set in the msg list. now i used this but is so manually....

 private void OnTriggerEnter2D(Collider2D collision)
{

    SetText(collision);
}

public void SetText(Collider2D other)
{
    switch (other.name)
    {
        case "basura":
            cosita.text = msgEmergentes[0];
            break;
        case "DinnerTime":
            cosita.text = msgEmergentes[1];
            break;

        case "atomo":
            cosita.text = msgEmergentes[2];
            break;
        case "extintor":
            cosita.text = msgEmergentes[3];
            break;
    }

}
question from:https://stackoverflow.com/questions/65858372/how-to-match-2-lists-with-the-same-index-position

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As I see it you could achieve this in simplified ways using Dictionary

Option A

Rather use e.g. a Dictionary like

private Dictionary<string, int> indexByName = new Dictionary<string, int>()
{
    {"basura", 0},
    {"DinnerTime", 1},
    {"atomo", 2},
    {"extintor", 3},
}

private void OnTriggerEnter2D(Collider2D collision)
{
    SetText(collision);
}

public void SetText(Collider2D other)
{
    if(indexByName.TryGetValue(other.name, out var index)
    {
        cosita.text = msgEmergentes[index];
    }
}

Option B

Why even take the route through an index? Simply directly use a dictionary like

private Dictionary<string, string> msgByName = new Dictionary<string, string>()
{
    {"basura", "Whatever basura message"},
    {"DinnerTime", "Hey, it's dinner time!"},
    {"atomo", "Whatever other message"},
    {"extintor", "Yet another message!"}
}

private void OnTriggerEnter2D(Collider2D collision)
{
    SetText(collision);
}

public void SetText(Collider2D other)
{
    if(msgByName.TryGetValue(other.name, out var msg)
    {
        cosita.text = msg;
    }
}

Option C

Now if you still want to be able to adjust these in the Unity Inspector to be more flexible you could do something like

[Serializable]
public class KeyMessagePair
{
    public string name;
    public string message;
}

// Adjust these in the Inspector
public KeyMessagePair[] pairSetup;

private Dictionary<string, string> msgByName;

private void Awake ()
{
    msgByName = new Dictionary<string, string>();
    foreach(var pair in pairSetup)
    {
        msgByName.Add(pair.name, pair.message);
    }
}

private void OnTriggerEnter2D(Collider2D collision)
{
    SetText(collision);
}

public void SetText(Collider2D other)
{
    if(msgByName.TryGetValue(other.name, out var msg)
    {
        cosita.text = msg;
    }
}

Of course you could/should also add some mechanics to avoid duplicate names but I think that should do it for now ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...