You may be looking for
Tweeng
"the single most amazing thing in all Oneness."
This is crack cocaine gaming programming.
Basic syntax
time.Tweeng( do something )
so (in pseudocode)
StartCoroutine( 2f.Tweeng( .. move the ball .. ) ); StartCoroutine( .5f.Tweeng( .. spin the spaceship .. ) ); StartCoroutine( 1f.Tweeng( .. fade the text .. ) );
These examples are:
- move the ball in two seconds
- spacecraft rotation in 1/2 second
- text fades in one second.
Using Tweeng is so euphoric that it can cause ongoing psychological problems if you do not have a firm head.
// tweeng z to 20 degrees in .12 seconds StartCoroutine(.12f.Tweeng( (t)=>transform.Eulers(0f,0f,t), 0f,20f) ); // fade in alpha in .75 seconds StartCoroutine(.75f.Tweeng( (u)=>{ca=u;s.color=c;}, 0f,1f) ); // duck volume.. StartCoroutine( .3f.Tweeng( x=>{current.volume=x;}, current.volume, 0f) ); // move something.. StartCoroutine(secs.Tweeng( (p)=>parked.transform.position=p, parked.transform.position, landing.transform.position) ); // twist image yield return StartCoroutine( .3f.Tweeng( (u)=>polaroid.localEulerAngles = new Vector3(0f,0f,u), 0f,9f) );
You can tweeng anything, of course, including fades.
base code base for Tweeng
(Actually, I personally like to have several different Tweengs in each Extensions file for each type, the generics approach is nice, but maybe too smart. It’s important to realize that it’s not transparent to you, in your code base you are just “Tweeng”, not caring about the type, which is great. In short,
in the extension file write Tweeng for each type that suits you. You can then pinpoint the purpose of your question throughout your code base. Just twin nothing.
Note that now you have covered every kind of animation; not just disappearing as mentioned in your question, but each one moves, rotates, bounces, scales, effect, etc. etc.
Make sure to get into a hypnotic state! :)
In fact, here the extension (using Tweeng) I use in some cases for fades. It is convenient to set variables for convenient use inside a lambda (for example, "c" in the example).
public static void FadeIn(this Main mb, SpriteRenderer s) { Color c = s.color; ca = 0f; s.color = c; mb.StartCoroutine( 1.25f.Tweeng( (u)=>{ca=u;s.color=c;}, 0f,1f) ); }
Also!
Not related to extensions and tweeng. Don't forget that the little-known crossFadeAlpha call crossFadeAlpha recently been added to Unity! The call is a bit confusing, but it can work very well.
Here is an example (presented as an extension!)
The critical fact is that canvasRenderers always has alpha capability in the user interface system.
public static class ExtensionsMeasures { public static void FadeIn(this Graphic g) { g.GetComponent<CanvasRenderer>().SetAlpha(0f); g.CrossFadeAlpha(1f,.15f,false); } public static void FadeOut(this Graphic g) { g.GetComponent<CanvasRenderer>().SetAlpha(1f); g.CrossFadeAlpha(0f,.15f,false); }