- Home /
TextArea/Field - Individual Character's foreground and background color.
Hello everyone, I might be missing this in the docs, if so point me at it please.
I would like to find out if it is possible to change the individual foreground and background colors on a per character basis within a TextArea/TextField.
I know that you can change the whole thing with a GUIStyle but I can't seem to find (if it is possible) how to change the individual colors.
thanks.
Update: I added an image as an example of the output that I would need to achieve.
Comment
Best Answer
Answer by Mike 3 · Mar 31, 2011 at 11:53 PM
We were just talking about that forum post in IRC and it won't work for my needs. However it does prove that Unity can't do exactly what I am asking so I will need to roll my own. thanks.
Answer by alpha_wd40 · Dec 09, 2012 at 09:35 AM
using UnityEngine;
using System.Collections;
public class MulticolorTextArea : MonoBehaviour
{
public string stringToEdit = "Hello World\nI've got 2 lines...";
void OnGUI()
{
//backup color
Color backupColor = GUI.color;
Color backupContentColor = GUI.contentColor;
Color backupBackgroundColor = GUI.backgroundColor;
//add textarea with transparent text
GUI.contentColor = new Color(1f, 1f, 1f, 0f);
GUIStyle style = new GUIStyle(GUI.skin.textArea);
Rect bounds = new Rect(10, 20, Screen.width - 10, Screen.height - 20);
stringToEdit = GUI.TextArea(bounds, stringToEdit);
//get the texteditor of the textarea to control selection
int controlID = GUIUtility.GetControlID(bounds.GetHashCode(), FocusType.Keyboard);
TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), controlID -1);
//set background of all textfield transparent
GUI.backgroundColor = new Color(1f, 1f, 1f, 0f);
//backup selection to remake it after process
int backupPos = editor.pos;
int backupSelPos = editor.selectPos;
//get last position in text
editor.MoveTextEnd();
int endpos = editor.pos;
Random.seed = 123;
//draw textfield with color on top of text area
editor.MoveTextStart();
while (editor.pos != endpos)
{
editor.SelectToStartOfNextWord();
string wordtext = editor.SelectedText;
//set word color
GUI.contentColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
//draw each word with a random color
Vector2 pixelselpos = style.GetCursorPixelPosition(editor.position, editor.content, editor.selectPos);
Vector2 pixelpos = style.GetCursorPixelPosition(editor.position, editor.content, editor.pos);
GUI.TextField(new Rect(pixelselpos.x - style.border.left, pixelselpos.y - style.border.top, pixelpos.x, pixelpos.y), wordtext);
editor.MoveToStartOfNextWord();
}
//Reposition selection
Vector2 bkpixelselpos = style.GetCursorPixelPosition(editor.position, editor.content, backupSelPos);
editor.MoveCursorToPosition(bkpixelselpos);
//Remake selection
Vector2 bkpixelpos = style.GetCursorPixelPosition(editor.position, editor.content, backupPos);
editor.SelectToPosition(bkpixelpos);
//Reset color
GUI.color = backupColor;
GUI.contentColor = backupContentColor;
GUI.backgroundColor = backupBackgroundColor;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613120035im_/https://answers.unity.com/themes/thub/images/avi.jpg)