I had started out using a static class to represent usages of colors, if not the technical color name, and indeed expanded color reference is something I had seen in forums where people would create a massive list of colors to then reference. I didn’t like this, and I also thought I’d like to try to package this up. But I needed a friendly way to expand colors & functionality in Unity, not just how to type boiler plate code for yourself.

After looking at code generation, I found there were several paths I could follow. One of them that I found was using a template with keywords that could be replaced with the user input entries. This was perfect! I’ll link the awesome how-to blog that I used for putting this together.

The Easy Power of Code Generation [by Template] – A Blog 

Essentially, you create a generic version of the script you want to use. With keywords inserted. Read all the text from that script, replace the keywords with the user inputs, write that back to a file, and voilà! Done. That’s the structure of it at leaset, and of course there is a bit more to it per your application. I decided to use two templates together and I’m happy with the results.

I think there could be many uses for this strategy and colors seems to be the perfect fit. I wanted to solve some of the shortcomings based on what people were looking for i.e. a way to get the color name, a way to get the hex code, a way to have expanded colors, a way not to have to write and maintain boilerplate code. And for me, a way to name colors for their use to be much more readable,  separate usage so that I could change colors for a specific usage in the future and nothing breaks, and a way to be globally accessible and as easy as the Color API.

Using this tutorial another benefit is provided by grouping into essentially libraries by namespace and folder structure. All of these are accomplished automatically. I could have continuously expanded a single static script per library, but I think that individual access from within Unity to each color to delete of modify is better and actually much faster to generate. Since there is no real undo functionality, or a way to transfer multiple colors. I also added a list that tracks validated entries as a group that can be modified and then re-built as a whole, skipping the ones that already exist and providing logs for the actions taken. The static scripts can be deleted and regenerated very easily with the new changes.

I used the free Bare Button package available from this site to add the simple user interactions. Another interesting note is my previous scripts, that I mentioned in the beginning, used ColorUtilty.TryParseHtmlString to get the color from a hex code. This does not work when displaying the color in the inspector in the same way as when using the Color API. As that method can only be run at runtime in Unity, and causes an error. Thank you internet for the bit shifting logic for conversion that pulled this together, and makes it work seamlessly. 

Use your favorite color palette site to gather your colors. There are a ton of awesome ones out there, and keeps this tool simple. Just plug in your color in the inspector, generate your scripts and start using them. I hope I can help fill some gaps for your game design. My assets can also be found through the Unity Asset Store.


Try out my new Color Pick asset!

You can find the free Bare Button Here.