简体   繁体   English

C# Unity 如何 select 仅数组中的一项并取消选择其他项

[英]C# Unity how to select only one item in an array and deselect other items

I have gameObject array that I'm looping through to animate the material with a delay for each gameObject.我有一个游戏对象数组,我正在循环通过它来为每个游戏对象设置一个延迟动画材质。 I am using a Coroutine in order to use WaitForSeconds and a for loop inside of it to add a delay for each object.我正在使用协程来使用 WaitForSeconds 和其中的 for 循环来为每个 object 添加延迟。

What ends up happening is all the objects in the array gets selected therefore the animation happens to all of them instead of just one object at a time最终发生的是数组中的所有对象都被选中,因此 animation 发生在所有对象上,而不是一次只发生一个 object

What I'm looking for, only the cube selected at the index to change color and to revert the color for the previous cubes.我正在寻找的,只有在索引处选择的立方体来改变颜色并恢复先前立方体的颜色。

How can I achieve that?我怎样才能做到这一点?

 public GameObject[] laneMat; 
 void Update()
 {
    StartCoroutine(matColor(laneMat, .3f));
 }

 IEnumerator matColor(GameObject[] gameObject, float delay)
 {
 time += Time.deltaTime;
  for (int i = 0; i < laneMat.Length; i++)
  {
     if (duration > time)
     {
         laneMat[i].GetComponent<Renderer>().material.SetColor("_Color", Color.red);
         
         yield return new WaitForSeconds(delay);
     }
     else if (time > duration && time < duration + .3f)
        {
            Renderer render = GetComponent<Renderer>();
            render.material.SetColor("_Color", originalColor);
            yield return null;
        }
    
 }
}

I tried Using List and making another loop inside the first loop to select the previes cubes and remove them with removeAt() but its a bit jittery and not the effect I'm looking for我尝试使用 List 并在第一个循环中创建另一个循环到 select 预览立方体并使用 removeAt() 删除它们,但它有点紧张而不是我正在寻找的效果

You can use condition to your loop here您可以在此处对循环使用条件

    public GameObject[] laneMat; 
     void Update()
     {
        StartCoroutine(matColor(laneMat, .3f));
     }
    
     IEnumerator matColor(GameObject[] gameObject, float delay)
     {
     time += Time.deltaTime;
      for (int i = 0; i < laneMat.Length; i++)
      {
         if (duration > time)
         {
if(selected = true) { laneMat[i].GetComponent<Renderer>().material.SetColor("_Color", Color.red); } else { laneMat[i].GetComponent<Renderer>().material.Color = defaultColor; }
             
             yield return new WaitForSeconds(delay);
         }
        
     }
    }

I think you should change the enumerator to be in the Start function, not the Update function.我认为您应该将枚举器更改为 Start function,而不是 Update function。 Calling StartCoroutine from Update means you start a new coroutine every frame that is independent of all the other coroutines you have already started.从 Update 调用 StartCoroutine 意味着您在每一帧都启动一个新的协程,该协程独立于您已经启动的所有其他协程。

If you move the call to the Start function, you can use delays wherever necessary to make the code wait.如果您将调用移至 Start function,您可以在必要时使用延迟来让代码等待。 One way to revert the other game objects is with a second loop nested in the first loop.恢复其他游戏对象的一种方法是在第一个循环中嵌套第二个循环。 Since you'll have access to the index of the changed gameobject, you can simply avoid reverting that game objects material.由于您可以访问更改后的游戏对象的索引,因此您可以简单地避免恢复该游戏对象的材质。

From there, it's just a matter of placing yield return statements wherever you want a delay.从那里开始,只需将 yield return 语句放置在您想要延迟的任何地方。

 public GameObject[] laneMat; 
 void Start()
 {
    StartCoroutine(matColor(.3f));
 }

 IEnumerator matColor(float delay)
 {
    while (true) {
        for (int i = 0; i < laneMat.Length; i++)
        {
            laneMat[i].GetComponent<Renderer>().material.SetColor("_Color", Color.red);
             
            //if you want a delay before reverting the other gameobject materials, it should go here
            
            for (int j = 0; j < laneMat.Length; i++) {
                if (j != i) { //if the index of the current gameObject is different from the one in the outer loop
                    laneMat[j].GetComponent<Renderer>().material.SetColor("_Color", originalColor);
                    
                    //if you want a delay between reverting each gameobject's material, it should go here
                }
            }       

            yield return new WaitForSeconds(delay);
        }
        
        //if you want a delay before the process starts again, it should go here
    }
}

I haven't tested this code, but I think it should be a good starting point.我没有测试过这段代码,但我认为它应该是一个很好的起点。

Best of luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM