在Unity3D中使用Coroutine将游戏对象淡出,然后返回.无限循环



我正在尝试让Unity 3D中的游戏对象淡出(淡出速度应可从编辑器中调整(,暂停/等待2秒(暂停长度可从编辑器调整(,然后淡出,无限循环。Coroutine是我试图在这里使用的来减少alpha值的东西,但我不确定我到底在哪里犯了错误。

我犯了一个错误。(无法将方法组"淡出"转换为非委托类型的"对象"(

这是我的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptFader : MonoBehaviour
{
// attached game object for fading
public GameObject Sphere;
// fade speed length
public float fadeSpeed;
//Pause length between fades
public int fadePause;
void Awake()
{
StartCoroutine(FadeOut(fadeSpeed));
}

//Fade Out Coroutine
public IEnumerator FadeOut(float fadeSpeed)
{
Renderer rend = Sphere.transform.GetComponent<Renderer>();
Color matColor = rend.material.color;
float alphaValue = rend.material.color.a;

//while loop to deincrement Alpha value until object is invisible
while (rend.material.color.a > 0f)
{
alphaValue -= Time.deltaTime / fadeSpeed;
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, alphaValue);
yield return new WaitForSeconds(fadePause);
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 0f);
StartCoroutine(FadeIn(fadeSpeed));
}
//Fade In Coroutine
public IEnumerator FadeIn(float fadeSpeed)
{
//waits for the return value of FadeOut coroutine to commence
yield return FadeOut;
Renderer rend = Sphere.transform.GetComponent<Renderer>();
Color matColor = rend.material.color;
float alphaValue = rend.material.color.a;

//while loop to increment object Alpha value until object is opaque
while(rend.material.color.a < 1f)
{
alphaValue += Time.deltaTime / fadeSpeed;
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, alphaValue);
yield return null;
}
rend.material.color = new Color(matColor.r, matColor.g, matColor.b, 1f);
StartCoroutine(FadeOut(fadeSpeed));
}
}

我支持冥王星的答案,但我会以不同的方式处理Fade()

private IEnumerator Fade()
{
Renderer rend = Sphere.transform.GetComponent<Renderer>();
Color initialColor = rend.material.color;
Color targetColor = new Color(initialColor.r, initialColor.g, initialColor.b, 0f);
float elapsedTime = 0f;
while (elapsedTime < fadeDuration)
{
elapsedTime += Time.deltaTime;
rend.material.color = Color.Lerp(initialColor, targetColor, elapsedTime / fadeDuration);
yield return null;
}
}

当然,您可以将材质和两种颜色作为参数传入。那样会更干净。但我认为,while循环中的逻辑更容易理解,也更常见和普遍,它可以明确地设置渐变动画需要多长时间。

CCD_ 2代表";线性插值";。如果你不熟悉它,我建议你学习它;你会在任何地方使用它。

例如,如果你把黑色作为起始色,把白色作为目标色,把1/2作为第三个参数会让你得到介于两者之间的灰色阴影;3/4将更接近白色,1/4将更接近黑色。

您可以将其用于Color.Lerp()等颜色,也可以用于数字、矢量和光谱上的任何值。

您只需编写一个控制效果的协同程序:

void Start() => StartCoroutine(FadeInOut());  
IEnumerator FadeInOut()
{
var material = Sphere.GetComponent<Renderer>().material;
//forever
while (true)
{
// fade out
yield return Fade(material, 0);
// wait
yield return new WaitForSeconds(fadePause);
// fade in
yield return Fade(material, 1);  
// wait
yield return new WaitForSeconds(fadePause);
}
}
IEnumerator Fade(Material mat, float targetAlpha)
{
while(mat.color.a != targetAlpha)
{
var newAlpha = Mathf.MoveTowards(mat.color.a, targetAlpha, fadeSpeed * Time.deltaTime);
mat.color = new Color(mat.color.r, mat.color.g, mat.color.b, newAlpha);
yield return null;
}
}

您的错误在这里(我相信您的堆栈跟踪会显示(:

//Fade In Coroutine
public IEnumerator FadeIn(float fadeSpeed)
{
//waits for the return value of FadeOut coroutine to commence
yield return FadeOut; // !!! ERROR

您缺少对函数的实际调用(括号(。我真的不确定你那句台词的用意。。。它需要存在吗?


但老实说,对于简单的循环器(在你的情况下,是阿尔法循环(来说,Coroutines是致命的。有关示例,请参阅带有协程的字体大小。

最新更新