- Home /
Converting Type to SpriteRenderer/Image/Text
Basically I am trying to make some visual effects like fading out and stuff, and I am using coroutines to run those effects.
When I start the coroutine, I passed down arguments with an object[] like the following:
if (Input.GetMouseButtonDown (1)) {
object[] tempParmArray = new object[2];
tempParmArray [0] = gameObject;
tempParmArray [1] = false;
effectScript.StartCoroutine ("flashEffect", tempParmArray);
}
Then the couroutine will just read each of the elements in the array. However, these visual effects are applied to different components, mostly the UI Image, Text and the standard SpriteRenderer.
Normally when I change the color of an object will do something like this:
gameObject.GetComponent<SpriteRenderer>().color = "some color";
However, given that the components will switch up from time to time, this line won't all the time, so I am trying to do something like this:
gameObject.GetComponent<Type.GetType("component name")>().color = "some color";
where component name is a parameter that I will pass onto the coroutine running the code. However, Unity won't let me compile this because it doesn't know what type it will get, and the type it gets might not have ".color".
I tried tricking Unity with something like this:
Type componentType = Type.GetType (componentTypeString);
SpriteRenderer r = null;
if (componentType == typeof(SpriteRenderer)) {
r = target.GetComponent<SpriteRenderer> ();
} else if (componentType == typeof(Image)) {
(Image) r = target.GetComponent<Image> ();
} else if (componentType == typeof(Text)) {
(Text) r = target.GetComponent<Text> ();
}
. . . . r.color = "some color";
and this:
Type componentType = Type.GetType (componentTypeString);
ovject r = null;
if (componentType == typeof(SpriteRenderer)) {
r = target.GetComponent<SpriteRenderer> ();
} else if (componentType == typeof(Image)) {
r = target.GetComponent<Image> ();
} else if (componentType == typeof(Text)) {
r = target.GetComponent<Text> ();
}
.
.
.
.
r.color = "some color";
and ofcourse neither works because the first one I can't convert SpriteRenderer to Image or Text, while the other runes the some problem where Unity won't know if "r" will have the ".color" option or not.
It had been suggested to me to do this:
Type componentType = Type.GetType (componentTypeString);
object r = null;
if (componentType == typeof(SpriteRenderer)) {
r = target.GetComponent<SpriteRenderer> ();
} else if (componentType == typeof(Image)) {
r = target.GetComponent<Image> ();
} else if (componentType == typeof(Text)) {
r = target.GetComponent<Text> ();
}
if (componentType == typeof(SpriteRenderer)) {
(r as SpriteRenderer).color = "some color";
} else if (componentType == typeof(Image)) {
(r as Image).color = "some color";
} else if (componentType == typeof(Text)) {
(r as Text).color = "some color";
}
Which will probably work, but that means I will need to replace every code I have GetComponent with a 3 part if statement, which I think might be demanding on performance (building this on a tablet, so ya :l)
Any ideas?
Answer by fafase · Nov 04, 2015 at 07:12 AM
Text and Image both inherits from Graphic, so you can use that for your method to take any UI element dealing with color.
SpriteRenderer is not from the same namespace so you would have to come up with an overload of your method.
My advice, make one method that takes color, modifies it by a value and return the new color. Then make two methods, one taking Graphic the other taking Material and internally use your color changing method.
public static IEnumerator FadeColor (Material material);
public static IEnumerator FadeColor (Graphic graphic);
private static Color SetColor(Color color, float value);
// or this way
private static void SetColor(ref Color color, float value);
for instance:
public static IEnumerator FadeColor (Material material){
while(material.color.a>0){
material.color = SetColor(material.color, value);
yield return null;
}
}