Multiplayer chat with color

Hello everyone !

I have a multiplayer chat working, but I want to enhance it with different colors for different users. For now what I have is this :

class ChatEntry
{
	var sender:String = "";
	var text:String = "";	
	var theColor:Vector3 = Vector3.zero;
}

to handle the data of each phrase, and then this method called into OnGUI :

function GlobalChatWindow (id : int) {
	scrollPosition = GUILayout.BeginScrollView (scrollPosition);

	for (var entry : ChatEntry in entries)
	{
		//GUI.color = Color(entry.theColor.x, entry.theColor.y, entry.theColor.z);
		//GUI.skin.font.material.color = Color(entry.theColor.x, entry.theColor.y, entry.theColor.z);
		GUILayout.BeginHorizontal();
		GUILayout.Label(entry.text);
		GUILayout.FlexibleSpace ();
		
		GUILayout.EndHorizontal();
		GUILayout.Space(3);
	}
}

The two commented lines are what I tested and obviously don’t work (GUI methods inside GUILayout methods usually don’t work, I understand why), I also tried to apply the color only on the layout event (didn’t work as well…)

I cannot apply the color outside of the BeginScrollView because all player would have the same color.

Now I am out of options. So if anyone as even just a little new idea, I’d be glad to test it !

If it doesn’t help, that is absolutely working method. Create GUISkin, in it in the section Custom create style for Label (certainly, it is possible to create separately only style, but I give the common decision). Style Label is name, for example, myLabel. Further, in your script give the reference to GUISkin and use as Start () color initialization (there can be some errors in a code as I don’t write on Java):

 var mySkin:GUISkin = null; //reference on your skin

 function Start() {
  mySkin.GetStyle("myLabel").normal.textColor = entry.theColor;
 }

 function OnGUI() {
  GUI.skin = mySkin; //apply skin for gui
  ...
 }

 function GlobalChatWindow (id : int) {
  ...
  GUILayout.Label(entry.text, "myLabel"); //apply our style
  ...
 }