x


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.

screen shot of a telnet client with parsed ansi color escape sequences, including background colors.

more ▼

asked Mar 31, 2011 at 08:27 PM

Feltope gravatar image

Feltope
44 2 4 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
more ▼

answered Mar 31, 2011 at 11:53 PM

Mike 3 gravatar image

Mike 3
33.2k 21 90 297

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.

Apr 01, 2011 at 12:02 AM Feltope
(comments are locked)
10|3000 characters needed characters left
 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;
     }
 }
more ▼

answered Dec 09, 2012 at 09:35 AM

alpha_wd40 gravatar image

alpha_wd40
61 1

Brilliant solution! Prehaps put on the wiki?

Jul 07 at 08:34 PM Jamster
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x940
x934
x256
x76

asked: Mar 31, 2011 at 08:27 PM

Seen: 4056 times

Last Updated: Jul 07 at 08:34 PM