using UnityEngine;
using UnityEngine.UI;
#nullable enable
namespace Yarn.Unity.Addons.Snaaake
{
///
/// Randomly varies the property of an over time, at a fixed frame rate.
///
public class RandomImageFill : MonoBehaviour
{
///
/// The minimum amount to fill.
///
public float MinFill = 0.5f;
///
/// The maximum amount to fill.
///
public float MaxFill = 1.0f;
///
/// The degree to which the fill amount will change each frame.
///
public float Speed = 1f;
///
/// The rate at which the fill amount will change.
///
public float FrameRate = 8f;
///
/// The image to change the fill amount of.
///
public Image? Image;
void Update()
{
if (Image == null)
{
// No image, so nothing to update.
return;
}
var t = Time.time;
// Snap time to framerate
t *= FrameRate;
t = Mathf.FloorToInt(t);
t /= FrameRate;
// Increase speed
t *= Speed;
// Evaluate the noise at our calculated point and update the image's
// fill amount
var noise = Mathf.PerlinNoise1D(t);
var fill = Mathf.Lerp(MinFill, MaxFill, noise);
Image.fillAmount = fill;
}
}
}