我想在团结中创造一些萤火虫。我想增加光的强度,然后等待几秒钟,然后在团结中减少它。当它们被孕育出来时,我希望它们增加它们的光强,等几秒钟,然后消失。我怎样才能以干净的方式创建这个“过程”呢?
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
}因此很明显,在第一个WaitWhile()中,协同线永远停止,我如何创建这样的代码链?当褪色时,我的意思是改变光的强度。
发布于 2017-09-26 08:00:41
尽管这个问题已经解决了,但是当前的解决方案只是在减少变量,并且还创建了每个帧的新对象(WaitForSeconds)。
在联合中,正确的方法是使用Mathf.Lerp和Time.deltaTime。这种类型的操作是Mathf.Lerp创建的,它将从您的minLuminosity转到maxLuminosity。您可以在我的另一个问题中更多地了解到这一点:使用GameObject的alpha组件这里使其褪色/消失。
我从那个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;
}
}现在,要创建您想要的确切效果,即增加光的强度,然后等待几秒钟,然后减小它,创建另一个协同函数,它调用上面的函数并等待它完成。您可以通过生成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);
}
}使用
public Light lightToFade;
public float eachFadeTime = 2f;
public float fadeWaitTime = 5f;
void Start()
{
StartCoroutine(fadeInAndOutRepeat(lightToFade, eachFadeTime, fadeWaitTime));
}发布于 2017-09-26 07:19:29
代码中的问题是,您只应用了亮度更改一次,因此您的WaitWhile条件永远无法达到。我会将这两个WaitWhile转换为简单的while循环,然后使用WaitForEndOfFrame
private IEnumerator ChangeIntensity()
{
while(true)
{
while(pointLight.intensity <= maxLuminosity)
{
pointLight.intensity += luminositySteps; // increase the firefly intensity / fade in
yield return new WaitForEndOfFrame();
}
yield return new WaitForSeconds(shineDuration); // wait 3 seconds
while(pointLight.intensity > minLuminosity)
{
pointLight.intensity -= luminositySteps;
yield return new WaitForEndOfFrame();
}
}
}https://stackoverflow.com/questions/46419975
复制相似问题