I made a script to alter color over time for GUI elements, but I want to know if there was a more efficient way of checking the types and making stuff happen.
My colorFader script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
[DisallowMultipleComponent] //Prevent people from adding 2 of this script to a single object.
public class colorFader : MonoBehaviour {
public enum colorType { Unselected, Image, Sprite, RawImage, Text, Particle} //Enum for determining source object.
public colorType ColorType = colorType.Unselected; //Enum type to help code determine the proper object to derive color from.
public Color FadeToColor; //The color we will be fading towards.
public object objectToColor; //The object we will be coloring.
public Color objectsColor; //The Changing color of the object.
public Color originalColor; //The Original color of the object.
public float fadeTime; //Amount of time to take fading.
public float startTime; //By default is 0, but if looped, will be reset to zero once fadeTime is reached.
public bool destroyOnFade; //Will the object be destroyed after fadeTime is reached?
public bool willLoop; //Will the object Loop? it can't do both.
void Start () {
// We must determine what type of script that we are effecting the color of.
#region Image
if (ColorType == colorType.Image) //Was the Selected Enum an Image?
{
objectToColor = gameObject.GetComponent<Image>(); //Getting the source, assuming it is an image.
if (objectToColor != null) //GetComponent does not throw an error, even if it fails to get your variable.
{
Image oTC = (Image)objectToColor; //Declaring we actually have an image.
objectsColor = oTC.color; //Finally we can get a color from it
originalColor = oTC.color; //We need to store the original color for looping or resetting purposes.
}
}
#endregion
#region Sprite
else if (ColorType == colorType.Sprite) //Was the Selected Enum an Sprite?
{
objectToColor = gameObject.GetComponent<SpriteRenderer>(); //Getting the source, assuming it is an sprite.
if (objectToColor != null) //GetComponent does not throw an error, even if it fails to get your variable.
{
SpriteRenderer oTC = (SpriteRenderer)objectToColor; //Declaring we actually have a Sprite.
objectsColor = oTC.color; //Finally we can get a color from it
originalColor = oTC.color; //We need to store the original color for looping or resetting purposes.
}
}
#endregion
#region RawImage
else if (ColorType == colorType.RawImage) //Was the Selected Enum a RawImage?
{
objectToColor = gameObject.GetComponent<RawImage>(); //Getting the source, assuming it is a RawImage.
if (objectToColor != null) //GetComponent does not throw an error, even if it fails to get your variable.
{
RawImage oTC = (RawImage)objectToColor; //Declaring we actually have a RawImage.
objectsColor = oTC.color; //Finally we can get a color from it
originalColor = oTC.color; //We need to store the original color for looping or resetting purposes.
}
}
#endregion
#region Text
else if (ColorType == colorType.Text) //Did user select the Text Enum?
{
objectToColor = gameObject.GetComponent<Text>(); //Getting the source, assuming it is an Text.
if (objectToColor != null) //GetComponent does not throw an error, even if it fails to get your variable.
{
Text oTC = (Text)objectToColor; //Declaring we actually have a RawImage.
objectsColor = oTC.color; //Finally we can get a color from it
originalColor = oTC.color; //We need to store the original color for looping or resetting purposes.
}
}
#endregion
startTime = 0; //Setting the start time to 0, in C# this is a redundancy.
}
void Update () {
// We must determine what type of object the source object is, yet again
#region Image
if (ColorType == colorType.Image)
{
if (objectToColor != null)
{
startTime += Time.deltaTime; //Add the time it took from last Update to our overall Time.
Image oTC = (Image)objectToColor; //Declare the object is what it is again.
objectsColor = Color.Lerp(objectsColor, FadeToColor, (Mathf.InverseLerp(0, fadeTime, startTime / 10))); //Blend the two colors based on a scaled Inverse lerp of the time/max time.
oTC.color = objectsColor; //Set the color of our object to our new color.
}
}
#endregion
#region RawImage
else if (ColorType == colorType.RawImage)
{
if (objectToColor != null)
{
startTime += Time.deltaTime; //Add the time it took from last Update to our overall Time.
RawImage oTC = (RawImage)objectToColor; //Declare the object is what it is again.
objectsColor = Color.Lerp(objectsColor, FadeToColor, (Mathf.InverseLerp(0, fadeTime, startTime / 10))); //Blend the two colors based on a scaled Inverse lerp of the time/max time.
oTC.color = objectsColor; //Set the color of our object to our new color.
}
}
#endregion
#region Sprite
else if (ColorType == colorType.Sprite)
{
if (objectToColor != null)
{
startTime += Time.deltaTime; //Add the time it took from last Update to our overall Time.
SpriteRenderer oTC = (SpriteRenderer)objectToColor; //Declare the object is what it is again.
objectsColor = Color.Lerp(objectsColor, FadeToColor, (Mathf.InverseLerp(0, fadeTime, startTime / 10))); //Blend the two colors based on a scaled Inverse lerp of the time/max time.
oTC.color = objectsColor; //Set the color of our object to our new color.
}
}
#endregion
#region Text
else if (ColorType == colorType.Text)
{
if (objectToColor != null)
{
startTime += Time.deltaTime; //Add the time it took from last Update to our overall Time.
Text oTC = (Text)objectToColor; //Declare the object is what it is again.
objectsColor = Color.Lerp(objectsColor, FadeToColor, (Mathf.InverseLerp(0, fadeTime, startTime / 10))); //Blend the two colors based on a scaled Inverse lerp of the time/max time.
oTC.color = objectsColor; //Set the color of our object to our new color.
}
}
#endregion
#region Bools
if (startTime > fadeTime && destroyOnFade) //Check if the overall time is greater than our fade time, and if the object should be destroyed.
{
Destroy(gameObject); //Destroy the GameObject this script is attached to.
}
else if (startTime > fadeTime && willLoop) //Check if the overall time is greater than our fade time, and if the object should continue to loop.
{
Color tempColor = FadeToColor; //Create a temporary color so we don't overwrite our original color.
FadeToColor = originalColor; //Set our destination color to the original color, because we reached our destination.
originalColor = tempColor; //Set our store the old destination color for looping back.
startTime = 0; //Reset out time to 0 so the loop can begin again.
}
else if (startTime > fadeTime) //Check if the overall time is greater than our fade time, and if so, disable it and save the color.
{
FadeToColor = originalColor; //Store the destination color so that when the object is re-enabled, it will fade back.
gameObject.SetActive(false); //Disable the GameObject this script is attached to.
}
#endregion
}
}
and my Inspector portion
#if (UNITY_EDITOR) //Prevent this file from being compiled into your game.
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
[CustomEditor(typeof(colorFader))] //Tell unity that this is an Editor for objects of type colorFader.
public class colorFadeInspector : Editor //We must derive from Editor to use the Editor GUI.
{
public override void OnInspectorGUI() //We are overriding the unity-generated inspector for our colorFader.
{
colorFader fader = target as colorFader; //Getting out object. Checking for null is not necessary, unity will not throw an error if it is destroyed.
GUILayout.BeginVertical(EditorStyles.helpBox); //Begin our layout, with a nice helpbox look.
if (fader.ColorType == colorFader.colorType.Unselected) //The default state of the fader.
{
fader.ColorType = (colorFader.colorType)EditorGUILayout.EnumPopup("Source Type", fader.ColorType); //Display our enums to choose from.
}
if (fader.ColorType == colorFader.colorType.Image) //Check if the chosen Enum is Image.
{
fader.ColorType = (colorFader.colorType)EditorGUILayout.EnumPopup("Source Type", fader.ColorType); //Display our enums to choose from.
fader.objectToColor = fader.gameObject.GetComponent<Image>(); //Let the unity API try to find the Image.
fader.objectToColor = EditorGUILayout.ObjectField("Source", (Object)fader.objectToColor, typeof(Image), true); //Display an object field to select the Image object.
}
if (fader.ColorType == colorFader.colorType.RawImage) //Check if the chosen Enum is RawImage.
{
fader.ColorType = (colorFader.colorType)EditorGUILayout.EnumPopup("Source Type", fader.ColorType); //Display our enums to choose from.
fader.objectToColor = fader.gameObject.GetComponent<RawImage>(); //Let the unity API try to find the RawImage.
fader.objectToColor = EditorGUILayout.ObjectField("Source", (Object)fader.objectToColor, typeof(RawImage), true); //Display an object field to select the RawImage object.
}
if (fader.ColorType == colorFader.colorType.Sprite) //Check if the chosen Enum is Sprite.
{
fader.ColorType = (colorFader.colorType)EditorGUILayout.EnumPopup("Source Type", fader.ColorType); //Display our enums to choose from.
fader.objectToColor = fader.gameObject.GetComponent<SpriteRenderer>(); //Let the unity API try to find the Sprite.
fader.objectToColor = EditorGUILayout.ObjectField("Source", (Object)fader.objectToColor, typeof(SpriteRenderer), true); //Display an object field to select the Sprite object.
}
if (fader.ColorType == colorFader.colorType.Text) //Check if the chosen Enum is Text.
{
fader.ColorType = (colorFader.colorType)EditorGUILayout.EnumPopup("Source Type", fader.ColorType); //Display our enums to choose from.
fader.objectToColor = fader.gameObject.GetComponent<Text>(); //Let the unity API try to find the Text.
fader.objectToColor = EditorGUILayout.ObjectField("Source", (Object)fader.objectToColor, typeof(Text), true); //Display an object field to select the Text object.
}
if (fader.objectToColor != null && fader.ColorType != colorFader.colorType.Unselected) //Check to make sure nothing is null or unselected.
{
fader.FadeToColor = EditorGUILayout.ColorField("Fade Color", fader.FadeToColor); //Display a color field for your fade color.
fader.fadeTime = EditorGUILayout.FloatField("Fade Time", fader.fadeTime); //Display a float field for your fade time.
fader.destroyOnFade = EditorGUILayout.Toggle("Destroy On Fade?", fader.destroyOnFade); //Display a bool for whether or not it gets destroyed on fadeTime being met.
if (!fader.destroyOnFade) //We can't destroy AND loop, so if you chose destroy, loop disappers.
fader.willLoop = EditorGUILayout.Toggle("Loop?", fader.willLoop); //Display the bool for whether or not to loop the object color fade.
}
GUILayout.EndVertical(); //End the GUI.
}
}
#endif