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));  
     }  

     floatUpdate(to);  
   }  

   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

Blog Introduction

No comments
The intent of this blog is to provide a localized place to provide updates, ideas and opinions on the various things Beard Logic is working on.  I hope to be able to impart some useful information from my experience as well as receive interesting feedback based on yours.  

Beard Logic as it stands, is a small indie development company.  I am currently the only active employee.  The company has recently released Cows Control as the first of many games (On IOS, Android and Windows Phone).  I have a great many ideas that I would like to explore so hopefully this things rolls on for a long time!



No comments :

Post a Comment