需要帮助产生协同程序

时间:2015-01-17 23:36:06

标签: c# unity3d coroutine yield-return ienumerator

我正在运行一个协程,其中正在操纵GameObject的子项的material.shaders。在进入下一步之前,我似乎无法弄清楚如何让coroutine等到所有材料都达到所需的混合水平。一切都运转正常,我只需要协程不要继续经过第28行,直到所有材料都改变为白天。有什么想法吗?

public GameObject changeInto;

private bool coroutineStarted = false;
private Renderer[] renderersArray;
private List<Material> materialsList = new List<Material>();
private GameObject newForm;

void Start(){
    renderersArray = this.gameObject.GetComponentsInChildren<Renderer>();
}
void Update(){
    if(!coroutineStarted){
        coroutineStarted = true;
        StartCoroutine(Change());
    }
}

private IEnumerator Change(){
    foreach(Renderer renderer in renderersArray){
        for(int i = 0; i < renderer.materials.Length; i++){
            if(renderer.materials[i].shader.name == "Toon/Basic Blender"){
                materialsList.Add(renderer.materials[i]);
            }
        }
    }
    foreach(Material material in materialsList){
        StartCoroutine(ChangeToWhite(material));
    }
    animation.Play("Evolution");
    newForm = Instantiate(changeInto, this.transform.position, this.transform.rotation) as GameObject;
    yield return null;
}

private IEnumerator ChangeToWhite(Material mat){
    float counter = mat.GetFloat("_Blend");
    while(counter != 1f){
        float increase = mat.GetFloat("_Blend") + 0.01f;
        mat.SetFloat("_Blend", increase);
        counter += increase;
        yield return null;
    }
}

1 个答案:

答案 0 :(得分:1)

这一部分:

 foreach(Material material in materialsList){
        StartCoroutine(ChangeToWhite(material));
    }

必须包含更改为白色例程的等待时间。在你的情况下,它会变量,因为你没有使用时间强烈鼓励的Time.deltaTime。

编辑:这是您可能想要使用的代码

foreach(Material material in materialsList){
        StartCoroutine(ChangeToWhite(material));
        yield return new WaitForSeconds(1);
    }

    private IEnumerator ChangeToWhite(Material mat){
    float counter = mat.GetFloat("_Blend");
    while(counter != 1f){ //the 1 here is the time to wait
        float increase = mat.GetFloat("_Blend") + Time.deltaTime;
        mat.SetFloat("_Blend", increase);
        counter += increase;
        yield return null;
    }
}