需要帮助生成协程
Need Help Yielding a Coroutine
我是 运行 一个协程,其中 GameObject 的子 material.shaders 正在被操纵。我似乎一辈子都想不出如何让协程等到所有材料都达到所需的混合水平,然后再继续下一步。一切正常,我只需要协程在所有材料都 ChangedToWhite 之前不要继续通过第 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;
}
}
这部分:
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;
}
}
我是 运行 一个协程,其中 GameObject 的子 material.shaders 正在被操纵。我似乎一辈子都想不出如何让协程等到所有材料都达到所需的混合水平,然后再继续下一步。一切正常,我只需要协程在所有材料都 ChangedToWhite 之前不要继续通过第 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;
}
}
这部分:
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;
}
}