如何在Unity中制作二维闪烁灯?
How to make 2D Flickering light in Unity?
我需要帮助,谢谢。我只需要制作闪烁的 2Dlight(使用 Universal Pipeline)。一段时间后,灯开始闪烁并且 returns 回到其原始值 (light.intensivity = 0.36f),一段时间后他又开始闪烁。但是用这个糟糕的代码我不能那样做,闪烁只起作用,因为协程每次重新启动(StartCoroutine(LightFlicker()); 在协程中)。但我不知道如何制作这个东西。也许使用其他方法来闪烁光?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering.Universal;
using UnityEngine.Experimental;
using UnityEngine.Experimental.Rendering.LWRP;
public class FlickeringLight : MonoBehaviour {
public UnityEngine.Experimental.Rendering.Universal.Light2D renderLight;
public AudioSource AS; // AudioSource to play the audio.
public AudioClip LightAudio; // Audio to play.
[SerializeField] float firstValue = 0f;
[SerializeField] float secondValue = 0.36f;
[SerializeField] float secondsBetweenFlickers = 2f;
void Start ()
{
renderLight.intensity = renderLight.intensity;
StartCoroutine(LightFlicker());
}
private void Awake()
{
renderLight = GetComponent<UnityEngine.Experimental.Rendering.Universal.Light2D>();
}
IEnumerator LightFlicker()
{
renderLight.intensity = Random.Range(firstValue, secondValue);
yield return new WaitForSeconds(secondsBetweenFlickers);
AS.PlayOneShot(LightAudio); // Play the audio.
StartCoroutine(LightFlicker());
}
//failed try to pause Coroutine LightFLicker
/* IEnumerator TimerLight()
{
StopCoroutine(LightFlicker());
renderLight.intensity = 0.36f;
yield return new WaitForSeconds(3f);
StartCoroutine(LightFlicker());
}
*/
}```
添加一个 while 循环以永不停止协程
IEnumerator TimerLight()
{
while (true)
{
renderLight.intensity = Random.Range(firstValue, secondValue);
yield return new WaitForSeconds(secondsBetweenFlickers);
}
}
那你可以试试随机等待时间
IEnumerator TimerLight()
{
while (true)
{
renderLight.intensity = Random.Range(firstValue, secondValue);
var randomTime = Random.Range(0, secondsBetweenFlickers);
yield return new WaitForSeconds(randomTime);
}
}
也许您有时需要某种移动平均线来使其平滑。
private Queue<float> queue; // TODO: initialize
private int smoothing = 5;
IEnumerator TimerLight()
{
var sum = 0f;
while (true)
{
while (queue.Count > smoothing)
{
sum -= queue.Dequeue();
}
var newValue = Random.Range(firstValue, secondValue);
queue.enque(newValue);
sum += newValue;
renderLight.intensity = sum / queue.Count;
yield return new WaitForEndOfFrame();
}
}
我还没有测试过这段代码,但它应该几乎可以正常工作。因为我以前使用过这个移动平均队列来统一闪烁光。我认为它是 运行 每一帧...
我需要帮助,谢谢。我只需要制作闪烁的 2Dlight(使用 Universal Pipeline)。一段时间后,灯开始闪烁并且 returns 回到其原始值 (light.intensivity = 0.36f),一段时间后他又开始闪烁。但是用这个糟糕的代码我不能那样做,闪烁只起作用,因为协程每次重新启动(StartCoroutine(LightFlicker()); 在协程中)。但我不知道如何制作这个东西。也许使用其他方法来闪烁光?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering.Universal;
using UnityEngine.Experimental;
using UnityEngine.Experimental.Rendering.LWRP;
public class FlickeringLight : MonoBehaviour {
public UnityEngine.Experimental.Rendering.Universal.Light2D renderLight;
public AudioSource AS; // AudioSource to play the audio.
public AudioClip LightAudio; // Audio to play.
[SerializeField] float firstValue = 0f;
[SerializeField] float secondValue = 0.36f;
[SerializeField] float secondsBetweenFlickers = 2f;
void Start ()
{
renderLight.intensity = renderLight.intensity;
StartCoroutine(LightFlicker());
}
private void Awake()
{
renderLight = GetComponent<UnityEngine.Experimental.Rendering.Universal.Light2D>();
}
IEnumerator LightFlicker()
{
renderLight.intensity = Random.Range(firstValue, secondValue);
yield return new WaitForSeconds(secondsBetweenFlickers);
AS.PlayOneShot(LightAudio); // Play the audio.
StartCoroutine(LightFlicker());
}
//failed try to pause Coroutine LightFLicker
/* IEnumerator TimerLight()
{
StopCoroutine(LightFlicker());
renderLight.intensity = 0.36f;
yield return new WaitForSeconds(3f);
StartCoroutine(LightFlicker());
}
*/
}```
添加一个 while 循环以永不停止协程
IEnumerator TimerLight()
{
while (true)
{
renderLight.intensity = Random.Range(firstValue, secondValue);
yield return new WaitForSeconds(secondsBetweenFlickers);
}
}
那你可以试试随机等待时间
IEnumerator TimerLight()
{
while (true)
{
renderLight.intensity = Random.Range(firstValue, secondValue);
var randomTime = Random.Range(0, secondsBetweenFlickers);
yield return new WaitForSeconds(randomTime);
}
}
也许您有时需要某种移动平均线来使其平滑。
private Queue<float> queue; // TODO: initialize
private int smoothing = 5;
IEnumerator TimerLight()
{
var sum = 0f;
while (true)
{
while (queue.Count > smoothing)
{
sum -= queue.Dequeue();
}
var newValue = Random.Range(firstValue, secondValue);
queue.enque(newValue);
sum += newValue;
renderLight.intensity = sum / queue.Count;
yield return new WaitForEndOfFrame();
}
}
我还没有测试过这段代码,但它应该几乎可以正常工作。因为我以前使用过这个移动平均队列来统一闪烁光。我认为它是 运行 每一帧...