Files
H3VR-TNH-Quality-of-Life-Im…/Assets/_Scripts/MeshRendererFlicker.cs
T

67 lines
1.7 KiB
C#
Raw Normal View History

2022-01-26 01:23:39 -08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TNHQoLImprovements
{
/// <summary>
/// Flash attached MeshRenderer the timer runs out.
/// </summary>
public class MeshRendererFlicker : MonoBehaviour
{
private bool initialized = false;
private float beginTime;
private float onLength;
private float offLength;
2022-01-26 01:23:39 -08:00
private float stateChangeTime = 0;
private float flashQuicklyAfter;
2022-01-26 01:23:39 -08:00
private bool visible = true;
private MeshRenderer mesh;
void Start()
{
mesh = GetComponent<MeshRenderer>();
}
/// <summary>
///
/// </summary>
/// <param name="interval">How long an off-on cycle lasts.</param>
/// <param name="onToOffRatio">On-time to off-time ratio for the flashing sequence.</param>
/// <param name="beginAfter">Postpone the flashing time.</param>
/// <param name="flashQuicklyAfter">Once flashing starts, how long until it flashes very quickly.</param>
public void Init(float interval, float onToOffRatio = 0.5f, float beginAfter = 0, float flashQuicklyAfter = -1)
2022-01-26 01:23:39 -08:00
{
beginTime = Time.time + beginAfter;
onLength = interval * onToOffRatio;
offLength = interval * (1 - onToOffRatio);
this.flashQuicklyAfter = flashQuicklyAfter;
2022-01-26 01:23:39 -08:00
initialized = true;
}
void Update()
{
if (!initialized || Time.time < beginTime)
return;
if (Time.time >= stateChangeTime)
{
visible = !visible;
mesh.enabled = visible;
if (visible) // set time to stay on
{
stateChangeTime = Time.time + onLength;
}
else // set time to stay off
{
stateChangeTime = Time.time + offLength;
}
}
}
}
}