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 '11 at 08:27 PM

Feltope gravatar image

Feltope
44 1 1 7

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

2 answers: sort voted first
more ▼

answered Mar 31 '11 at 11:53 PM

Mike 3 gravatar image

Mike 3
32.4k 17 78 278

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 '11 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 '12 at 09:35 AM

alpha_wd40 gravatar image

alpha_wd40
46 1

(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:

x776
x765
x209
x70

asked: Mar 31 '11 at 08:27 PM

Seen: 3033 times

Last Updated: Dec 09 '12 at 09:35 AM