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.
Update: I added an image as an example of the output that I would need to achieve.
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
public class MulticolorTextArea : MonoBehaviour
public string stringToEdit = "Hello World\nI've got 2 lines...";
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
int endpos = editor.pos;
Random.seed = 123;
//draw textfield with color on top of text area
while (editor.pos != endpos)
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);
Vector2 bkpixelselpos = style.GetCursorPixelPosition(editor.position, editor.content, backupSelPos);
Vector2 bkpixelpos = style.GetCursorPixelPosition(editor.position, editor.content, backupPos);
GUI.color = backupColor;
GUI.contentColor = backupContentColor;
GUI.backgroundColor = backupBackgroundColor;
Brilliant solution! Prehaps put on the wiki?
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
1 Person is following this question.
Attach Text data to gameobjects by player
Color.white not so much white...
changing text color in GUI.Label
Having proper text areas in unity?
Disable left/right cursor movement in TextField when pressing up/down arrow keys