You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class FloatTween : MonoBehaviour
{
public bool RunOnAwake = true;
public float StartDelay = 0.1f;
public float fromValue = 0f;
public float toValue = 1f;
public float Speed = 1f;
[System.Serializable]
public class FloatEvent : UnityEvent<float> { }
public FloatEvent OnChange;
public UnityEvent OnComplete;
void Awake()
{
if (RunOnAwake)
Tween();
}
public void Tween()
{
StartCoroutine(Tween(fromValue, toValue));
}
IEnumerator Tween(float from, float to)
{
OnChange.Invoke(from);
yield return new WaitForSeconds(StartDelay);
var value = from;
while (!Mathf.Approximately(value, to))
{
value = Mathf.MoveTowards(value, to, Time.deltaTime * Speed);
OnChange.Invoke(value);
yield return null;
}
OnComplete.Invoke();
}
}