Create a coroutine to infer various types of objects

Hey. I am trying to create a coroutine in unity that will handle fading on all objects. So far, I can get alpha values ​​for the different types that I want, but I'm at a dead end on how to set a new color after it spills 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, if you can say another set of if or what not, but I want to avoid it. For example, the solutions I want to avoid, I looked at the implementation of iTween. Here is a great 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, mom spaghetti. Extending this will be a nightmare.

+2
c # unity3d
source share
1 answer

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); } 
+4
source share

All Articles