Calming The Coroutine (Unity, Technical)

No comments

Calming The Coroutine

While working on Cows Control I've written a good number of coroutines specifically to smoothly transition a number or vector from one to another.  In the case of the GIF, the Score Boost Slider Bar.

Seeing as there is almost always a better way when such situation occur, I came up with this neat class. It works really well for progress bars and the like and could easily be extended to include vectors.

It simplifies the entire smooth transition into these lines of code:
 this.Transition(startFloat, endFloat, transitionTime, delegate (float updatedFloat)  
 valueToUpdate = updatedFloat;  

The back-end looks like this:
 public static class CX_Utility  
   private static IEnumerator TransitionFloatOverTime(float from, float to, float overTime, System.Action floatUpdate)  

     float time = overTime;  
     float alpha = 0.0f;  

     while(time > 0.0f)  
       yield return new WaitForEndOfFrame();\  
       time -= Time.deltaTime;  
       alpha = time / overTime;  
       float Update(Mathf.SmoothStep(from, to, 1.0f - alpha));  


   public static void Transition(this MonoBehaviour monoBehaviour, float from, float to, float overTime, System.Action floatUpdate)  
     monoBehaviour.StartCoroutine(TransitionFloatOverTime(from, to, overTime, floatUpdate));  

Let me know what you think. Cheers!

No comments :

Post a Comment