Code for making fadein, fadeout in Unity3d C#,
Fadein,
IEnumerator FadeIn(GameObject gameObj){
for (float f = 0f; f <= 1; f += 0.01f)
{
Color c = gameObj.renderer.material.color;
c.a = f;
gameObj.renderer.material.color = c;
yield return new WaitForSeconds(.01f);
}
yield return null;
}
Fadeout,
IEnumerator FadeOut(GameObject gameObj){
for (float f = 1f; f >= 0; f -= 0.01f)
{
Color c = gameObj.renderer.material.color;
c.a = f;
gameObj.renderer.material.color = c;
yield return new WaitForSeconds(.01f);
}
yield return null;
}
Pre Requisites: Object you are passing as argument must have shader which has color property in it.
FadeInFadeOut using NGUI UISprite and Coroutine,
public UIAtlas RunTimeAtlas;
IEnumerator ColorFadeInFadeOut()
{
UISprite newSprite = NGUITools.AddWidget<UISprite>(GameObject.Find("Story"));
newSprite.depth = 6;
newSprite.gameObject.name = "FadeInFadeOut";
newSprite.atlas = RunTimeAtlas;
newSprite.spriteName = "White120";
newSprite.SetDimensions (Screen.width * 3, Screen.height * 3);
TweenAlpha ta1 = newSprite.gameObject.AddComponent<TweenAlpha> ();
ta1.from = 0.0f;
ta1.to = 1.0f;
yield return new WaitForSeconds(1.10f);
TweenAlpha ta2 = newSprite.gameObject.AddComponent<TweenAlpha> ();
ta2.from = 1.0f;
ta2.to = 0.0f;
yield return new WaitForSeconds(1.0f);
NGUITools.Destroy(newSprite.gameObject);
}
Pre Requisites: Atlas reference must be assigned to RunTimeAtlas in Unity Inspector having white texture in it. Texture of other color can also be used for fade in and fade out effect.