首页 > 解决方案 > 同时更改列表中游戏对象的颜色

问题描述

所以我有一个完整的游戏对象列表。我想同时改变它们的颜色。我现在这样做的方式是使用 2 个 for 循环逐个更改每个游戏对象的颜色,但我需要同时更新它。这是我的代码

        yield return new WaitForSeconds(2.3f);
        var places = GameObject.FindGameObjectsWithTag("ground");
            for (int i = 0; i <= GOlist.Count; i++)
            {
                renderer = GOlist[i].GetComponent<SpriteRenderer>();
                Color c = renderer.material.color;
                for (float s = 0.28f; s <= 1f; s += 0.02f)
                {
                    c.r = s;
                    c.g = s;
                    c.b = s;

                  renderer.color = c;


                yield return new WaitForSeconds(0.01f);
                }
            }

标签: c#listunity3dcolors

解决方案


只需交换 for 循环...

var places = GameObject.FindGameObjectsWithTag("ground");
for (float s = 0.28f; s <= 1f; s += 0.02f)
{
    for (int i = 0; i <= GOlist.Count; i++)
    {
        renderer = GOlist[i].GetComponent<SpriteRenderer>();
        Color c = renderer.material.color;
        c.r = s;
        c.g = s;
        c.b = s;
        renderer.color = c;
    }
    yield return new WaitForSeconds(0.01f);
}

推荐阅读