- Home /
Change text color from event trigger
I am trying to change color of a text component when its parent panel has been clicked. After watching this tutorial (especially the part at 3:45) I understand that it is not possible to pass Color parameter via event function call. I was able to work around it creating this simple script:
public class TextColorHandler : MonoBehaviour {
public Color[] colorArray;
public void SetColor(int colorArrayIndex) {
GetComponent<Text> ().color = colorArray [colorArrayIndex];
}
}
I put the script as a sibling component next to the Text component. I feed the array with desired colors in editor and then reference to them with corresponding array index in event function call (since integer type is OK as a event parameter).
Now my question is, what other ways are in Unity do the same thing ? Preferably more elegant/intuitive, or without use of custom scripts. I feel like it's quite over the top to do such simple thing by script. The only other way I can think of are materials, but then again - having the same material stored multiple times that only differ in color ...
Answer by Sabre-Runner · Jan 21, 2018 at 11:39 AM
What you can do is create another component, a sort of GraphicExtender, that will interact with your graphic and set the colour for you. But you can only do it through the colour string. Sorry. Does this help enough?
public string ColourString
{
get { return ColorUtility.ToHtmlStringRGBA(this.graphicToExtend.color); }
set
{
Color colour;
ColorUtility.TryParseHtmlString(value, out colour);
this.graphicToExtend.color = colour;
}
}