2016-08-24 41 views
0

我在下面的代码中遇到了问题。它的功能是建立一条路线,一个接一个。C#IEnumerator不可更改

currentLength = Random.Range(maxFloorLength - 2, maxFloorLength); 
    List<GameObject> tempList = new List<GameObject>(); 

    for (int i = 0; i < currentLength; ++i) 
    { 
     foreach (GameObject g in pooledFloor) 
     { 
      if (!g.activeInHierarchy) 
      { 
       g.SetActive(true); 
       g.transform.position = startPoint + buildDirection * i; 
       g.transform.DOScaleY(5, 0.25f).Play(); 
       lastFloorPositions.Add(g.transform.position); 
       tempList.Add(g); 
       break; 
      } 
     } 
     yield return new WaitForSeconds(0.05f); 
    } 

虽然这正常工作,当我将其更改为:

currentLength = Random.Range(maxFloorLength - 2, maxFloorLength); 
    List<GameObject> tempList = new List<GameObject>(); 

    for (int i = 0; i < currentLength; ++i) 
    { 
     foreach (GameObject g in pooledFloor) 
     { 
      if (!g.activeInHierarchy) 
      { 
       g.transform.position = startPoint + buildDirection * i; 
       lastFloorPositions.Add(g.transform.position); 
       tempList.Add(g); 
       break; 
      } 
     } 
    } 

    foreach (GameObject g in tempList) 
    { 
     g.SetActive(true); 
     g.transform.DOScaleY(5, 0.25f).Play(); 
     yield return new WaitForSeconds(0.05f); 
    } 

它是所有创造之路的一个部分(就是那个应该是tempList列表的末尾)。我想要的是该脚本将所有预制件添加到列表中,然后再与他们一起做其他事情,所以我可以在两者之间添加一些内容。我试图将其更改为void函数,并将这些代码作为ienumerator调用,更改循环类型等,但似乎没有任何工作,我不知道为什么它不起作用。我使用Debug.Log检查了它是否仍然发现超过1个预制,并且它确实存在,但它只激活并扩展其中的一个。我敢打赌,它的东西很明显,但我就是不能看到它:/

如何看起来:

How its working

那应该怎么看:

How it should look

忘了提及 - IEnumerator内部的所有内容

+0

不知道,但也许你可以/应该问这http://gamedev.stackexchange.com/? –

+0

第一回合是嵌套的,第二回合不是。尝试: –

+0

@Kuniq检查我的答案 –

回答

2

在第一个代码,当你调用g.SetActive(true);,在for循环的下一次迭代的if条件相同的对象不会是真实的,因此将评估为下一个不活动的对象,并将其添加到tempList

在第二种情况下,对于for loop的所有迭代,它始终对同一对象g(pooledFloor上的第一个非活动对象)进行评估,因为它未被激活。

为了达到预期的行为,我建议您在pooledFloor上使用for-loop而不是for-each,并在列表中的对象从列表中移除,如果它不活动。

是这样的:

for (int i = 0; i < currentLength; ++i) 
{ 
    for (int j = 0; j < pooledFloor.Count; j++) 
    { 
     GameObject g = pooledFloor[j]; 
     if (!g.activeInHierarchy) 
     { 
      g.transform.position = startPoint + buildDirection * i; 
      lastFloorPositions.Add(g.transform.position); 
      tempList.Add(g); 
      pooledFloor.Remove(g); // Removing it from list would solve the problem. 
      break; 
     } 
    } 
} 
+0

从列表中删除是肯定的好表现 - 方向(我猜),但主要问题是你提到的 - 我没有打开游戏对象,然后在未来的迭代中发现相同的对象一次又一次,所以我做了什么,我只是将SetActive从底部循环移到了顶部,然后在底部循环中启用了Renderer组件。这几乎解决了所有问题^^非常感谢:)我只知道这件事很简单 – Kuniq

+0

我很乐意提供帮助。请接受我的回答是正确的 –

0

第一遍是嵌套的,第二遍是不是。尝试:它

for (int i = 0; i<currentLength; ++i) 
{ 
    foreach (GameObject g in tempList) 
    { 
     g.SetActive(true); 
     g.transform.DOScaleY(5, 0.25f).Play(); 
    } 
    yield return new WaitForSeconds(0.05f); 

} 
0

这样想:

public void passONE() 
    { 
     currentLength = Random.Range(maxFloorLength - 2, maxFloorLength); 
     List<GameObject> tempList = new List<GameObject>(); 

     for (int i = 0; i < currentLength; ++i) 
     { 
      foreach (GameObject g in pooledFloor) 
      { 
       if (!g.activeInHierarchy) 
       { 
        g.transform.position = startPoint + buildDirection * i; 
        lastFloorPositions.Add(g.transform.position); 
        tempList.Add(g); 
        break; 
       } 
      } 
     } 
     passTwo(currentLength, tempList); 

    } 
    private void passTwo(double currentLength,List<GameObject> tempList) 
    { 


     for (int i = 0; i < currentLength; ++i) 
     { 
      foreach (GameObject g in tempList) 
      { 
       g.SetActive(true); 
       g.transform.DOScaleY(5, 0.25f).Play(); 

      } 
      yield return new WaitForSeconds(0.05f); 
     } 


    }