rabidus-test/Assets/Scripts/LeanTimer.cs

54 lines
1.2 KiB
C#
Raw Normal View History

2023-09-11 15:44:17 +03:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class LeanTimer : MonoBehaviour
{
[Header("Start values:")]
public bool StartOnAwake = false;
public float StartValue;
public float EndValue;
public float Time;
2023-09-11 15:44:17 +03:00
public UnityEvent<float> OnUpdate = new UnityEvent<float>();
2023-10-11 17:33:14 +03:00
public UnityEvent OnTotalComplete;
2023-09-11 15:44:17 +03:00
private LTDescr LTDescr;
private void Start()
{
if (StartOnAwake)
{
2023-10-11 17:33:14 +03:00
ManualStart();
}
}
[ContextMenu("Debug")]
2023-10-11 17:33:14 +03:00
public void ManualStart()
{
StartTimer(StartValue, EndValue, Time);
}
2023-09-11 15:44:17 +03:00
public void StartTimer(float startValue, float endValue, float time, System.Action OnComplete = null)
{
LTDescr = LeanTween.value(1, 0, time).setOnUpdate((float x) =>
{
OnUpdate?.Invoke(x);
2023-10-11 17:33:14 +03:00
}).setOnComplete(() =>
{
OnComplete?.Invoke();
OnTotalComplete?.Invoke();
});
2023-09-11 15:44:17 +03:00
}
2023-09-18 20:09:22 +03:00
public void StopTimer(float value = 0)
2023-09-11 15:44:17 +03:00
{
if (LTDescr != null)
2023-09-18 20:09:22 +03:00
{
2023-09-11 15:44:17 +03:00
LeanTween.cancel(LTDescr.id);
2023-09-18 20:09:22 +03:00
OnUpdate?.Invoke(value);
}
2023-09-11 15:44:17 +03:00
}
}