- Home /
How to store colors for future use
I'm creating a UI for a game in which I change the colors of images quite often. For example, I have a button build from all white images, which I assign different colors. When the button is clickable, I want it to be green, when it can't be clicked, it's grey. Same with background images, health bars and so on. As of now, I use materials to store all colors and assign them to the images once they should change.
For me, this solution seems quite off. I have a ton of materials which are difficult to organize, I have to assign every UI Script the required materials, I can't properly lerp colors and so on.
Is there a better way to store all those colors and assign them quick and easy, like a dedicated color storage script?
Thanks in advance!
Answer by BBIT-SOLUTIONS · Apr 05, 2020 at 12:20 PM
You could write your own static class were you can save your individual colors, e.g. as hex value. Something like:
static class MyColors
{
private static string red = "#ff0000";
public static Color GetMyRed(){
Color tmpColor;
if(ColorUtility.TryParseHtmlString(red, out tmpColor)){
return tmpColor;
}
return Color.red; //just in the case it couldn't be parsed
}
}
And then you can call it so:
Color myIndividualRed = MyColors.GetMyRed();
And if you need only the default Colors like red, green, blue and so on you can take the static Colors class.
You can simply call it in this way:
Color myDefaultRedColor = Color.red;
I had this idea as well. But unlike in your example I think I will save everything as a Color Object directly. This way I can easily edit them in the inspector.
Ok yes, that's also an alternative. But then you still have to set it manually for each object in the inspector. Thought that was your main issue, wasn't it? Because you have lots of UI elements, which are difficult to organize and you want to automate it because of that. Or did i understand it wrong?
But sure, i guess both approaches should work. So just take the one, which fits best for you :-)
Ok my knowledge of Unity seems to be too little :D I do want to set it once for every UI Element but I also want to be able to set it in the Inspector. The latter one isn't possible with a static class as they can't derive from $$anonymous$$onobehaviour. The solution I'm going to use now is a scriptable object with a singleton pattern. This is a really helpful resource for that. That way I can just ask for my colors with ColorStorage.Instance.$$anonymous$$yRed
and so on. Thanks for your help tough!
Answer by dougantor · Apr 05, 2020 at 10:29 AM
You should be able to change the color property of the Image component
I know that I can change that. $$anonymous$$y question is about the best practice to manage and organize a large set of differenct colors.