Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4.  
  5. public class Timey : MonoBehaviour
  6. {
  7.     private float _timer;
  8.     public float AfterXSeconds = 3f;
  9.     public float RunForXSeconds = 3f;
  10.  
  11.     public AnimationCurve UsingCurve = AnimationCurve.Linear(0, 0, 1, 1);
  12.  
  13.     [Range(0, 1)] public float CurrentTime;
  14.  
  15.     [Range(0, 1)] public float CurrentValue;
  16.  
  17.     private bool isRunning;
  18.  
  19.  
  20.     private IEnumerator runningRoutiune;
  21.  
  22.     public bool StartImmediately = true;
  23.     void Start()
  24.     {
  25.         if(StartImmediately) Activate();
  26.     }
  27.  
  28.     public void Activate()
  29.     {
  30.         StopRunning();
  31.         runningRoutiune = DoBegin();
  32.         StartCoroutine(runningRoutiune);
  33.     }
  34.  
  35.     private IEnumerator DoBegin()
  36.     {
  37.         yield return new WaitForSeconds(AfterXSeconds);
  38.         Begin();
  39.     }
  40.  
  41.     // Update is called once per frame
  42.     private void Update()
  43.     {
  44.         if (!isRunning) return;
  45.         if (_timer < 1)
  46.         {
  47.             _timer += Time.deltaTime / RunForXSeconds;
  48.             CurrentTime = _timer;
  49.             CurrentValue = EvaluateCurve(UsingCurve, 0, 1, CurrentTime);
  50.             Tick.Invoke(CurrentValue);
  51.         }
  52.     }
  53.  
  54.  
  55.     public void Begin()
  56.     {
  57.         CurrentValue = _timer = 0;
  58.         isRunning = true;
  59.       //  Reset.Invoke();
  60.     }
  61.  
  62.     private void End()
  63.     {
  64.         isRunning = false;
  65.       //  Finished.Invoke();
  66.     }
  67.  
  68.     public void StopRunning()
  69.     {
  70.         if (runningRoutiune != null)
  71.         {
  72.             StopCoroutine(runningRoutiune);
  73.           //  Stopped.Invoke(CurrentValue);
  74.         }
  75.     }
  76.  
  77.     private float EvaluateCurve(AnimationCurve curve, float start, float end, float value)
  78.     {
  79.         return start + (end - start) * curve.Evaluate(value);
  80.     }
  81.  
  82.     private TimeyEvent Stopped;
  83.     private UnityEvent Finished;
  84.     public TimeyEvent Tick;
  85.     private UnityEvent Reset;
  86.  
  87. }
  88.  
  89. [System.Serializable]
  90. public class TimeyEvent : UnityEvent<float>
  91. {
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement