Create a coroutine to fade out different types of object

前端 未结 2 900
暗喜
暗喜 2020-12-01 23:12

Hi I\'m trying to create a coroutine in unity that will handle fading on all manner of object. So far I\'m able to get the alpha values of the different types I want but I\'

相关标签:
2条回答
  • 2020-12-01 23:40

    It could be you're looking for

    Tweeng

    The crack cocaine of game programming!

    The basic syntax is

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

    Those examples are,

    • move the ball over two seconds
    • spin the spaceship in 1/2 second
    • fade the text over one second.

    More...

    // 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)=>{c.a=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, certainly including fades.

    Basic code base for Tweeng :

    Just put this in a file Extns.cs:

    public static class Extns
        {
        public static IEnumerator Tweeng( this float duration,
                   System.Action<float> var, float aa, float zz )
            {
            float sT = Time.time;
            float eT = sT + duration;
            
            while (Time.time < eT)
                {
                float t = (Time.time-sT)/duration;
                var( Mathf.SmoothStep(aa,zz, t) );
                yield return null;
                }
            
            var(zz);
            }
    
        public static IEnumerator Tweeng( this float duration,
                   System.Action<Vector3> var, Vector3 aa, Vector3 zz )
            {
            float sT = Time.time;
            float eT = sT + duration;
            
            while (Time.time < eT)
                {
                float t = (Time.time-sT)/duration;
                var( Vector3.Lerp(aa,zz, t) );
                yield return null;
                }
            
            var(zz);
            }
    }
    

    (If you're interested, you can use a generics approach - QA on that)

    Tweeng - it could save your life!

    0 讨论(0)
  • 2020-12-01 23:40

    FYI regarding this specific question:

    Be aware of crossFadeAlpha , recently added to Unity. It is confusing but it can work well. Be aware that canvas renderers always have an alpha.

    public static class MoExtensions
        {
    
        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);
            }
    
    0 讨论(0)
提交回复
热议问题