FadeObjectInOut

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
Line 8: Line 8:
  
 
It's especially useful for GUI style objects, but can be used on 3D objects in the scene as well.
 
It's especially useful for GUI style objects, but can be used on 3D objects in the scene as well.
 +
 +
==Updated==
 +
Updated 7th March 2013 with the option of pausing before the fade occurs, and a bunch of fixes to 'Fade In On Start'.
  
 
==Usage==
 
==Usage==
Line 27: Line 30:
 
If you choose a different speed, it will use the new speed.  
 
If you choose a different speed, it will use the new speed.  
  
 +
NOTE: Requires materials with a shader that allows transparency through color. 
 +
*/
 +
 +
using UnityEngine;
 +
using System.Collections;
 +
 +
public class FadeObjectInOut : MonoBehaviour
 +
{
 +
/*
 +
FadeObjectInOut.cs
 +
Hayden Scott-Baron (Dock) - http://starfruitgames.com
 +
6 Dec 2012
 +
 +
This allows you to easily fade an object and its children.
 +
If an object is already partially faded it will continue from there.
 +
If you choose a different speed, it will use the new speed.
 +
 
NOTE: Requires materials with a shader that allows transparency through color.   
 
NOTE: Requires materials with a shader that allows transparency through color.   
 
*/
 
*/
Line 36: Line 56:
 
{
 
{
 
// publically editable speed
 
// publically editable speed
 +
public float fadeDelay = 0.0f;
 
public float fadeTime = 0.5f;  
 
public float fadeTime = 0.5f;  
 
public bool fadeInOnStart = false;  
 
public bool fadeInOnStart = false;  
public bool fadeOutOnStart = false;  
+
public bool fadeOutOnStart = false;
 +
private bool logInitialFadeSequence = false;
 +
 
 +
 
 +
 
 
 
 
// store colours
 
// store colours
Line 46: Line 71:
 
IEnumerator Start ()
 
IEnumerator Start ()
 
{
 
{
yield return null;  
+
//yield return null;  
 +
yield return new WaitForSeconds (fadeDelay);
 +
 
 
if (fadeInOnStart)
 
if (fadeInOnStart)
FadeIn(fadeTime);  
+
{
 +
logInitialFadeSequence = true;
 +
FadeIn ();  
 +
}
 +
 
 
if (fadeOutOnStart)
 
if (fadeOutOnStart)
FadeOut(fadeTime);  
+
{
 +
FadeOut (fadeTime);  
 +
}
 
}
 
}
+
 
 +
 
 +
 
 +
 
 
// check the alpha value of most opaque object
 
// check the alpha value of most opaque object
 
float MaxAlpha()
 
float MaxAlpha()
Line 64: Line 100:
 
return maxAlpha;  
 
return maxAlpha;  
 
}
 
}
+
 
// fade sequence
 
// fade sequence
 
IEnumerator FadeSequence (float fadingOutTime)
 
IEnumerator FadeSequence (float fadingOutTime)
Line 71: Line 107:
 
bool fadingOut = (fadingOutTime < 0.0f);
 
bool fadingOut = (fadingOutTime < 0.0f);
 
float fadingOutSpeed = 1.0f / fadingOutTime;  
 
float fadingOutSpeed = 1.0f / fadingOutTime;  
+
 
 
// grab all child objects
 
// grab all child objects
 
Renderer[] rendererObjects = GetComponentsInChildren<Renderer>();  
 
Renderer[] rendererObjects = GetComponentsInChildren<Renderer>();  
Line 85: Line 121:
 
}
 
}
 
}
 
}
+
 
 
// make all objects visible
 
// make all objects visible
 
for (int i = 0; i < rendererObjects.Length; i++)
 
for (int i = 0; i < rendererObjects.Length; i++)
Line 91: Line 127:
 
rendererObjects[i].enabled = true;
 
rendererObjects[i].enabled = true;
 
}
 
}
 +
 
 
 
// get current max alpha
 
// get current max alpha
 
float alphaValue = MaxAlpha();   
 
float alphaValue = MaxAlpha();   
+
 
 +
 
 +
// This is a special case for objects that are set to fade in on start.
 +
// it will treat them as alpha 0, despite them not being so.
 +
if (logInitialFadeSequence && !fadingOut)
 +
{
 +
alphaValue = 0.0f;
 +
logInitialFadeSequence = false;
 +
}
 +
 
 
// iterate to change alpha value  
 
// iterate to change alpha value  
 
while ( (alphaValue >= 0.0f && fadingOut) || (alphaValue <= 1.0f && !fadingOut))  
 
while ( (alphaValue >= 0.0f && fadingOut) || (alphaValue <= 1.0f && !fadingOut))  
Line 119: Line 165:
 
}
 
}
 
}
 
}
 +
 +
 +
Debug.Log ("fade sequence end : " + fadingOut);
 +
 
}
 
}
 
 
Line 143: Line 193:
 
StartCoroutine("FadeSequence", -newFadeTime);  
 
StartCoroutine("FadeSequence", -newFadeTime);  
 
}
 
}
 +
  
 
+
// These are for testing only.  
// These are for testing only. Comment out to try fading in and out!
+
// void Update()
// void Update()
+
// {
+
// if (Input.GetKeyDown (KeyCode.Alpha0) )
+
 
// {
 
// {
// FadeIn();
+
// if (Input.GetKeyDown (KeyCode.Alpha0) )
 +
// {
 +
// FadeIn();
 +
// }
 +
// if (Input.GetKeyDown (KeyCode.Alpha9) )
 +
// {
 +
// FadeOut();
 +
// }
 
// }
 
// }
// if (Input.GetKeyDown (KeyCode.Alpha9) )
+
// {
+
}
// FadeOut();
+
 
// }
+
 
// }
+
  
 
}
 
}
  
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 11:37, 7 March 2013

Author: Hayden Scott-Baron (Dock)

Contents

Description

This allows you to easily fade an object and its children. If an object is already partially faded it will continue from there. If you choose a different speed, it will use the new speed.

It's especially useful for GUI style objects, but can be used on 3D objects in the scene as well.

Updated

Updated 7th March 2013 with the option of pausing before the fade occurs, and a bunch of fixes to 'Fade In On Start'.

Usage

Place this script on the gameobject you wish to fade, and call 'FadeIn()' or 'FadeOut()'. Adjust fading time with 'FadeIn(4.0f)', or by adjusting the public time variable.

Technical Discussion

This requires objects to have materials that allow for alpha transparency.

C# - FadeObjectInOut.cs

/*
	FadeObjectInOut.cs
 	Hayden Scott-Baron (Dock) - http://starfruitgames.com
 	6 Dec 2012 
 
	This allows you to easily fade an object and its children. 
	If an object is already partially faded it will continue from there. 
	If you choose a different speed, it will use the new speed. 
 
	NOTE: Requires materials with a shader that allows transparency through color.  
*/
 
using UnityEngine;
using System.Collections;
 
public class FadeObjectInOut : MonoBehaviour
{
/*
	FadeObjectInOut.cs
 	Hayden Scott-Baron (Dock) - http://starfruitgames.com
 	6 Dec 2012 
 
	This allows you to easily fade an object and its children. 
	If an object is already partially faded it will continue from there. 
	If you choose a different speed, it will use the new speed. 
 
	NOTE: Requires materials with a shader that allows transparency through color.  
*/
 
using UnityEngine;
using System.Collections;
 
public class FadeObjectInOut : MonoBehaviour
{
	// publically editable speed
	public float fadeDelay = 0.0f; 
	public float fadeTime = 0.5f; 
	public bool fadeInOnStart = false; 
	public bool fadeOutOnStart = false;
	private bool logInitialFadeSequence = false; 
 
 
 
 
	// store colours
	private Color[] colors; 
 
	// allow automatic fading on the start of the scene
	IEnumerator Start ()
	{
		//yield return null; 
		yield return new WaitForSeconds (fadeDelay); 
 
		if (fadeInOnStart)
		{
			logInitialFadeSequence = true; 
			FadeIn (); 
		}
 
		if (fadeOutOnStart)
		{
			FadeOut (fadeTime); 
		}
	}
 
 
 
 
	// check the alpha value of most opaque object
	float MaxAlpha()
	{
		float maxAlpha = 0.0f; 
		Renderer[] rendererObjects = GetComponentsInChildren<Renderer>(); 
		foreach (Renderer item in rendererObjects)
		{
			maxAlpha = Mathf.Max (maxAlpha, item.material.color.a); 
		}
		return maxAlpha; 
	}
 
	// fade sequence
	IEnumerator FadeSequence (float fadingOutTime)
	{
		// log fading direction, then precalculate fading speed as a multiplier
		bool fadingOut = (fadingOutTime < 0.0f);
		float fadingOutSpeed = 1.0f / fadingOutTime; 
 
		// grab all child objects
		Renderer[] rendererObjects = GetComponentsInChildren<Renderer>(); 
		if (colors == null)
		{
			//create a cache of colors if necessary
			colors = new Color[rendererObjects.Length]; 
 
			// store the original colours for all child objects
			for (int i = 0; i < rendererObjects.Length; i++)
			{
				colors[i] = rendererObjects[i].material.color; 
			}
		}
 
		// make all objects visible
		for (int i = 0; i < rendererObjects.Length; i++)
		{
			rendererObjects[i].enabled = true;
		}
 
 
		// get current max alpha
		float alphaValue = MaxAlpha();  
 
 
		// This is a special case for objects that are set to fade in on start. 
		// it will treat them as alpha 0, despite them not being so. 
		if (logInitialFadeSequence && !fadingOut)
		{
			alphaValue = 0.0f; 
			logInitialFadeSequence = false; 
		}
 
		// iterate to change alpha value 
		while ( (alphaValue >= 0.0f && fadingOut) || (alphaValue <= 1.0f && !fadingOut)) 
		{
			alphaValue += Time.deltaTime * fadingOutSpeed; 
 
			for (int i = 0; i < rendererObjects.Length; i++)
			{
				Color newColor = (colors != null ? colors[i] : rendererObjects[i].material.color);
				newColor.a = Mathf.Min ( newColor.a, alphaValue ); 
				newColor.a = Mathf.Clamp (newColor.a, 0.0f, 1.0f); 				
				rendererObjects[i].material.SetColor("_Color", newColor) ; 
			}
 
			yield return null; 
		}
 
		// turn objects off after fading out
		if (fadingOut)
		{
			for (int i = 0; i < rendererObjects.Length; i++)
			{
				rendererObjects[i].enabled = false; 
			}
		}
 
 
		Debug.Log ("fade sequence end : " + fadingOut); 
 
	}
 
 
	void FadeIn ()
	{
		FadeIn (fadeTime); 
	}
 
	void FadeOut ()
	{
		FadeOut (fadeTime); 		
	}
 
	void FadeIn (float newFadeTime)
	{
		StopAllCoroutines(); 
		StartCoroutine("FadeSequence", newFadeTime); 
	}
 
	void FadeOut (float newFadeTime)
	{
		StopAllCoroutines(); 
		StartCoroutine("FadeSequence", -newFadeTime); 
	}
 
 
	// These are for testing only. 
//		void Update()
//		{
//			if (Input.GetKeyDown (KeyCode.Alpha0) )
//			{
//				FadeIn();
//			}
//			if (Input.GetKeyDown (KeyCode.Alpha9) )
//			{
//				FadeOut(); 
//			}
//		}
 
}
 
 
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox