随着时间的推移增加和减少光强度

pet*_*ann 2 c# unity-game-engine

我想在 Unity 中创建一些萤火虫。我想增加光强度,然后等待几秒钟,然后在 Unity 中减少它。当它们生成时,我希望它们增加光强度,等待几秒钟,然后淡出。我怎样才能以干净的方式创建这个“过程”?

private Light pointLight; // The light component of the firefly

private float minLuminosity = 0; // min intensity
private float maxLuminosity = 1; // max intensity

private float luminositySteps = 0.005f; // factor when increasing / decreasing

private float shineDuration = 3; // wait 3 seconds when faded in

private void Start()
{
    pointLight = GetComponent<Light>();
    pointLight.intensity = Random.Range(minLuminosity, maxLuminosity); // start with a random intensity
    StartCoroutine(ChangeIntensity()); // start the process
}

private IEnumerator ChangeIntensity()
{
    pointLight.intensity += luminositySteps; // increase the firefly intensity / fade in
    yield return new WaitWhile(() => pointLight.intensity >= maxLuminosity); // wait for the maximum intensity

    yield return new WaitForSeconds(shineDuration); // wait 3 seconds

    pointLight.intensity -= luminositySteps;
    yield return new WaitWhile(() => pointLight.intensity <= maxLuminosity); // wait for the minimum intensity

    StartCoroutine(ChangeIntensity()); // do it again
}
Run Code Online (Sandbox Code Playgroud)

很明显,协程从一开始就永远停止了,WaitWhile()我怎样才能创建这样的代码链呢?淡入或淡出时,我只是指改变光强度。

Pro*_*mer 5

尽管这个问题已经解决了,但当前的解决方案只是递减变量,并且WaitForSeconds每帧都会创建新对象 ( )。

在 Unity 中执行此操作的正确方法是使用Mathf.LerpTime.deltaTime。这种类型的操作是为从您到Mathf.Lerp进行的。您可以在我的另一个关于使用其 alpha 组件淡出/淡入 GameObject 的问题中阅读更多相关信息minLuminositymaxLuminosity

fadeInAndOut从该答案中获取了该函数并将其移植到该Light组件中。这是一个简单的灯光淡入/淡出功能:

IEnumerator fadeInAndOut(Light lightToFade, bool fadeIn, float duration)
{
    float minLuminosity = 0; // min intensity
    float maxLuminosity = 1; // max intensity

    float counter = 0f;

    //Set Values depending on if fadeIn or fadeOut
    float a, b;

    if (fadeIn)
    {
        a = minLuminosity;
        b = maxLuminosity;
    }
    else
    {
        a = maxLuminosity;
        b = minLuminosity;
    }

    float currentIntensity = lightToFade.intensity;

    while (counter < duration)
    {
        counter += Time.deltaTime;

        lightToFade.intensity = Mathf.Lerp(a, b, counter / duration);

        yield return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,要创建您想要的确切效果,即增加光强度,然后等待几秒钟,然后减少光强度,请创建另一个协程函数来调用上面的函数并等待其完成。您可以通过生成fadeInAndOut函数来做到这一点。请注意如何在循环WaitForSeconds外部声明,while以便它不会每次都创建新对象。

//Fade in and out forever
IEnumerator fadeInAndOutRepeat(Light lightToFade, float duration, float waitTime)
{
    WaitForSeconds waitForXSec = new WaitForSeconds(waitTime);

    while (true)
    {
        //Fade out
        yield return fadeInAndOut(lightToFade, false, duration);

        //Wait
        yield return waitForXSec;

        //Fade-in 
        yield return fadeInAndOut(lightToFade, true, duration);
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

public Light lightToFade;
public float eachFadeTime = 2f;
public float fadeWaitTime = 5f;

void Start()
{
    StartCoroutine(fadeInAndOutRepeat(lightToFade, eachFadeTime, fadeWaitTime));
}
Run Code Online (Sandbox Code Playgroud)