lundi 27 avril 2015

My Foreach loop needs optimization

I'm working in Unity, writing some C# for a terrain hide/reveal code. The code works, but is there a way to rewrite this so that I wont have to keep writing this loooong code out every time I encounter one of these pretty simple situations?

Here are the variables relevant to this part:

// ...

private int terLevel = 0;
private GameObject[] t0;
private GameObject[] t1;
private GameObject[] t2;
private GameObject[] t3;
private GameObject[] t4;
private GameObject[] t5;

Here is the relevant part of my start function:

void Start ()
    {
        // ...

        t0 = GameObject.FindGameObjectsWithTag ("T0");
        t1 = GameObject.FindGameObjectsWithTag ("T1");
        t2 = GameObject.FindGameObjectsWithTag ("T2");
        t3 = GameObject.FindGameObjectsWithTag ("T3");
        t4 = GameObject.FindGameObjectsWithTag ("T4");
        t5 = GameObject.FindGameObjectsWithTag ("T5");
    }

And here are is the main part I want to fix in my update function:

void Update ()
    {
        // ...

        if(terLevel <= 0)
    {
        foreach(GameObject i in t1)
            i.renderer.enabled = false;
        foreach(GameObject i in t2)
            i.renderer.enabled = false;
        foreach(GameObject i in t3)
            i.renderer.enabled = false;
        foreach(GameObject i in t4)
            i.renderer.enabled = false;
        foreach(GameObject i in t5)
            i.renderer.enabled = false;
    }
    else
    {
    if(terLevel == 1)
    {
        foreach(GameObject i in t1)
            i.renderer.enabled = true;
        foreach(GameObject i in t2)
            i.renderer.enabled = false;
        foreach(GameObject i in t3)
            i.renderer.enabled = false;
        foreach(GameObject i in t4)
            i.renderer.enabled = false;
        foreach(GameObject i in t5)
            i.renderer.enabled = false;
    }
    else
    {
    if(terLevel == 2)
    {
        foreach(GameObject i in t1)
            i.renderer.enabled = true;
        foreach(GameObject i in t2)
            i.renderer.enabled = true;
        foreach(GameObject i in t3)
            i.renderer.enabled = false;
        foreach(GameObject i in t4)
            i.renderer.enabled = false;
        foreach(GameObject i in t5)
            i.renderer.enabled = false;
    }
    else
    {
    if(terLevel == 3)
    {
        foreach(GameObject i in t1)
            i.renderer.enabled = true;
        foreach(GameObject i in t2)
            i.renderer.enabled = true;
        foreach(GameObject i in t3)
            i.renderer.enabled = true;
        foreach(GameObject i in t4)
            i.renderer.enabled = false;
        foreach(GameObject i in t5)
            i.renderer.enabled = false;
    }
    else
    {
    if(terLevel == 4)
    {
        foreach(GameObject i in t1)
            i.renderer.enabled = true;
        foreach(GameObject i in t2)
            i.renderer.enabled = true;
        foreach(GameObject i in t3)
            i.renderer.enabled = true;
        foreach(GameObject i in t4)
            i.renderer.enabled = true;
        foreach(GameObject i in t5)
            i.renderer.enabled = false;
    }
    else
    {
    if(terLevel >= 5)
    {
        foreach(GameObject i in t1)
            i.renderer.enabled = true;
        foreach(GameObject i in t2)
            i.renderer.enabled = true;
        foreach(GameObject i in t3)
            i.renderer.enabled = true;
        foreach(GameObject i in t4)
            i.renderer.enabled = true;
        foreach(GameObject i in t5)
            i.renderer.enabled = true;
    }}}}}}
}

Terrifying isn't it. Imagine if I had more than 5 terrain layers, I would die before I finished the code. There must be a

Aucun commentaire:

Enregistrer un commentaire