2

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'm at a dead end as to how to set the new color after lerping it. Here is my code :

public static  IEnumerator FadeTo(float aValue, float aTime,GameObject gameObj)
            {
                Component[] allComponents = gameObj.GetComponents(typeof(Component));
                Component fadableComponent;
                Type type = null;
                float alpha=0;
                foreach(Component c in allComponents)
                {
                    if(c.GetType()==typeof(Image))
                    {
                        fadableComponent = c;
                        alpha = fadableComponent.GetComponent<Image>().color.a;
                        type = fadableComponent.GetType();
                        Debug.Log("Found a image");
                        break;
                    }
                    if (c.GetType() == typeof(Renderer))
                    {
                        fadableComponent = c;
                        alpha = fadableComponent.GetComponent<Renderer>().material.color.a;
                        type = fadableComponent.GetType();
                        break;
                    }
                    if (c.GetType() == typeof(SpriteRenderer))
                    {
                        fadableComponent = c;
                        alpha = fadableComponent.GetComponent<SpriteRenderer>().color.a;
                        type = fadableComponent.GetType();
                        break;
                    }
                    if(c.GetType()== typeof(CanvasGroup))
                    {
                        fadableComponent = c;
                        alpha = fadableComponent.GetComponent<CanvasGroup>().alpha;
                        type = fadableComponent.GetType();
                    }
                    Debug.Log(alpha.ToString());
                }


                for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
                {
                    Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha, aValue, t));
                    gameObj.transform.GetComponent(type) // What now ?!
                    yield return null;
                }
                yield return null;
            }
        }

Now i know how to do this with lets say another set of if's or what not, but I want to avoid that. For example of solutions I want to avoid I took a look at the iTween implementation. Here is a perfect example of what I want to avoid:

if(target.GetComponent<GUITexture>()){
            target.GetComponent<GUITexture>().color=colors[3];
        }else if(target.GetComponent<GUIText>()){
            target.GetComponent<GUIText>().material.color=colors[3];
        }else if(target.GetComponent<Renderer>()){
            target.GetComponent<Renderer>().material.color=colors[3];
        }else if(target.GetComponent<Light>()){
            target.GetComponent<Light>().color=colors[3];   
        }

This is a mess, moms spaghetti. Extending this will be a nightmare.

Uri Popov
  • 2,127
  • 2
  • 25
  • 43
  • Uri, you're probably looking for (1) in the c# language, **generics** which are an incredibly powerful part of the language. an "instant introduction" is beyond the scope of a QA, please google for tutorials. But (2) In Unity I think you're exactly looking for ***Tweeng*** ("the single most amazing thing in all of Unity") ... http://stackoverflow.com/questions/36121120/c-sharp-generic-with-slight-difference-for-types – Fattie May 14 '16 at 15:25
  • Uri, note that more basically you **must** use Extensions here. (Whatever solution you choose.) If new to Unity http://stackoverflow.com/a/35629303/294884 – Fattie May 14 '16 at 15:29

2 Answers2

11

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, Mathf.SmoothStep(0f, 1f, t) ) );
            yield return null;
            }
        
        var(zz);
        }
}

(Note that those examples include smoothing. You may need no-smoothing in some cases. In fact, in projects now we always have both "Tweeng" and "SmoothedTweeng".)

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

Tweeng - it could save your life!

Roman Pushkin
  • 5,639
  • 3
  • 40
  • 58
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 1
    great answer as always. One more question will this work on iOS? – Uri Popov May 15 '16 at 07:50
  • 1
    I have to say I just implemented Tweeng into my Tools and its beautiful.A singly manly tear rolled down my cheek. – Uri Popov May 16 '16 at 07:33
  • 1
    Nice. I edited the vector form to have smoothing as well. – Ruzihm Feb 18 '21 at 00:20
  • 1
    hi @Ruzihm ! that's a fantastic idea but please add it for yourself as a separate answer! (indeed I myself always have a "Tween" and also a "SmoothedTweeng") you rock – Fattie Feb 18 '21 at 01:16
  • 1
    actually ignore that @Ruzihm it's more consistent the way you did it! heh ! :) – Fattie Feb 18 '21 at 01:16
1

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);
        }
Fattie
  • 27,874
  • 70
  • 431
  • 719