Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.Events;
- public class Timey : MonoBehaviour
- {
- private float _timer;
- public float AfterXSeconds = 3f;
- public float RunForXSeconds = 3f;
- public AnimationCurve UsingCurve = AnimationCurve.Linear(0, 0, 1, 1);
- [Range(0, 1)] public float CurrentTime;
- [Range(0, 1)] public float CurrentValue;
- private bool isRunning;
- private IEnumerator runningRoutiune;
- public bool StartImmediately = true;
- void Start()
- {
- if(StartImmediately) Activate();
- }
- public void Activate()
- {
- StopRunning();
- runningRoutiune = DoBegin();
- StartCoroutine(runningRoutiune);
- }
- private IEnumerator DoBegin()
- {
- yield return new WaitForSeconds(AfterXSeconds);
- Begin();
- }
- // Update is called once per frame
- private void Update()
- {
- if (!isRunning) return;
- if (_timer < 1)
- {
- _timer += Time.deltaTime / RunForXSeconds;
- CurrentTime = _timer;
- CurrentValue = EvaluateCurve(UsingCurve, 0, 1, CurrentTime);
- Tick.Invoke(CurrentValue);
- }
- }
- public void Begin()
- {
- CurrentValue = _timer = 0;
- isRunning = true;
- // Reset.Invoke();
- }
- private void End()
- {
- isRunning = false;
- // Finished.Invoke();
- }
- public void StopRunning()
- {
- if (runningRoutiune != null)
- {
- StopCoroutine(runningRoutiune);
- // Stopped.Invoke(CurrentValue);
- }
- }
- private float EvaluateCurve(AnimationCurve curve, float start, float end, float value)
- {
- return start + (end - start) * curve.Evaluate(value);
- }
- private TimeyEvent Stopped;
- private UnityEvent Finished;
- public TimeyEvent Tick;
- private UnityEvent Reset;
- }
- [System.Serializable]
- public class TimeyEvent : UnityEvent<float>
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement