Unity3D for iOS: UIToolkit & Setting a Custom RGB Color Value for UITextInstance

Using Color.red or Color.green is quick and easy in a lot of cases, but if you’re working with a Designer, you often have to choose a more customized color value based on what’s in a .PSD file.

Basic UIToolkit text set up (assuming the UI and UIToolkit objects are properly set up in the Hierarchy & Inspector):

var font1 = new UIText( textToolkit, "arialregular", "arialregular.png" );			
var myText = font1.addTextInstance( "SCORE 0", 0.0f, 0.0f, 0.6f, 0, Color.green, UITextAlignMode.Center, UITextVerticalAlignMode.Middle );							

Using R,G,B values as params for Color class’ constructor

Tried a bunch of variations and they didn’t work:

myText.setColorForAllLetters( new Color( 169, 238, 3 ) ); //RGB: 169, 238, 3		
myText.setColorForAllLetters( new Color( 0.78f, 0.99f, 0.93f ) ); //HSB: 78, 99%, 93%

This didn’t work either:

private Color cGreen;

void Start() {
	//cGreen = new Color( 0.6f, 0.2f, 0.03f); //169.0f, 238.0f, 3.0f
	//cGreen = new Color( 169.0f, 238.0f, 3.0f, 255.0f); //169.0f, 238.0f, 3.0f
	cGreen = new Color( 0.5f, 0.25f, 0.25f );
myText.setColorForAllLetters( (Color)cGreen );

Convert the R, G and B to individual Percentage values

This finally worked:

myText.setColorForAllLetters( new Color( 0.69f, 0.93f, 0.01f) ); 

I had to take the RGB value from the .PSD and convert it to percentages. There are sites that already have this conversion for you, like this one.

This works too:

var myText = font1.addTextInstance( "SCORE 0", 0.0f, 0.0f, 0.6f, 0, new Color( 0.69f, 0.93f, 0.01f), UITextAlignMode.Center, UITextVerticalAlignMode.Middle );

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s