GUI question: different text colors in the same textarea?

Hi,

In my project there is an Evolver character and the user can talk to the character by typing then the user’ input and text response of the character will both be displayed in a single GUItext area. Is it possible that the user input displayed as one color and response displayed in another color?

Right now I’m using this method for displaying the text in GUI, stringToEdit = GUI.TextArea (Rect (10, 10, 200, 100), stringToEdit, 200). The text is displayed by concatenating input and response into stringToEdit as a single string.

Thanks!!

using UnityEngine;
using System.Collections;

public class MulticolorTextArea : MonoBehaviour 
{
    public string stringToEdit = "Hello World

I’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;
    }
}

Simply use this:
guiText.material.color = Color.green;

Green is an example. You can change that to any color that unity’s javascript recognizes.

It’s not really possible to have two colors in the same GUI.TextArea call. If they could be on separate lines, then you could call two GUI.TextArea calls (or GUI.Label calls) and have one call use a style with one color and another use a style with a different color. This means two draw calls, though.

You could use GUIText objects instead of the Immediate mode UnityGUI, and then the suggestion above (about changing the material) may work for you.

If it’s really critical, there is the 2D Toolkit framework that has the ability to do this in its UI/Font support (can change gradients/colors of text inline).

just use GUI.contentColor.
I didn’t say your script.but i think you have to check what player are “talking” (use a TAG,good way)
and assign a boolean like this:

var isPlayerTalk:boolean=false;
var myText:string="";//this is our text

//..do our check control 

function OnGUI() {
if (isPlayerTalk){
GUI.contentColor = Color.yellow;
GUI.Button(Rect(10,10,70,30), myText);
}

else{
GUI.contentColor = Color.red;
GUI.Button(Rect(10,10,70,30), myText);
}
}

This is a simple example,and thath change only color of the text.