- Home /
GUILayout Sort Problem
Hello Everyone I Was Doing a C# Game Chat [All - Team - Whisper] and its work fine. but there is problem with the Sorting
Lemme Show You The Picture i Will Explain More
Here We Go :
The [All] Chat Togather And The Team Chat Togather and Whisper Chat Togather
its not sorted by Newest!
Its Shows This Way 2 To All , 5 To All , 3 To Team , 4 To Team
It Should Be : 2 To All , 3 To Team , 4 To Team , 5 To All
i hope You Got It
And Here is the Code i Use
myStyle.normal.textColor = Color.magenta;
GUILayout.Label(communicationWhisper,myStyle);
myStyle.normal.textColor = Color.cyan;
GUILayout.Label(communicationTeam,myStyle);
myStyle.normal.textColor = Color.white;
GUILayout.Label(communication,myStyle);
The TeamChat have communicationWhisper,, and the Whisper have communicationTeam and ToAll > communicationAll ,,,
What is stored in the communicationWhisper
when you add it the chat? It is a single line of message, or you are still appending all your whisper
messages to it?
Answer by Chronos-L · Feb 27, 2013 at 08:01 AM
I am just going to give this a shot as I do not fully understand your explanation.
From your code fragment, you are just outputting the 3 sets of communication separately. From what I can see, I think that in your current script, you just append the latest message to a string variable or gui content ( communicationWhisper, communicationTeam, communication ). By doing so, you can only sort them by separately, not as a whole chat.
What you need to do is to create a new class to store the message.
public enum CommType{ NORMAL, WHISPER, TEAM };
public class Message {
public string content;
public CommType commType;
public Message( string cont, CommType ct ) {
//Standard constructor
}
}
Then, in your chat script
List <Message> chat = new List<Message>();
All message will go to this chat variable. Whenever there is a new piece coming in:
chat.Add( new Message("My Message", CommType.WHISPER ) );
On output, do this:
//We are iterating the list in reverse order, as the newest message starts from the back
for(int i = chat.Count - 1; i >= 0; --i ) {
//Use either a function or a if-else structure to determine the textColor,
//I am using a function in this example
myStyle.normal.textColor = CommColor( chat[i].commType );
GUILayout.Label( chat[i].content, myStyle );
}
Hope that this is what you are looking for.
Code Test
This is what I got after implementing the codes. By the way, I wrote a test class that send messages to the chatControl
object by detecting different Input.GetKeyDown(KeyCode.??)
I hope that this code is what you are looking for.
i added this codes and no error shows :)
but also no chat shows xDD
so its didnt work
thanks anyway .........
There is Easier Way By $$anonymous$$ake all Chat in string "communicationChat"
its works fine. but the problem in Color
If I made Team Chat. all Chat will be in blue If I $$anonymous$$ade Whisper , all Chat will be in Red because it going to be in 1 label
$$anonymous$$y script foreach(int i = chat.Count - 1; i >= 0; --i ) {...}
is outputting each individual chat item, so it should be colored according to their type.
To mix color, you have to use different label for each individual message.
I don't understand why no chat message is shown when you run the script? Do you insert each new messages using chat.Add( new $$anonymous$$essage( ... ) );
?
yes
chat.Add(new $$anonymous$$essage(communicationTeam, CommType.Team));
In TeamChat RPC . and
chat.Add(new $$anonymous$$essage(communicationWhisper, CommType.Whisper));
in WhisperChat Rpc ..
Etc
Does TeamChat RPC and WhisperChat RPC refers to the same chat variable? There should be one and just one chat variable, otherwise, messages of different CommType will not mixed together.
I am talking about the chat
variable, are your RPC (I assumed that this is a class) uses the same chat variable?
Up to this point, it would be better if you post the communication codes here (Don't do it in the comment section, edit your question and add the codes there)
Your answer
Follow this Question
Related Questions
How to destroy a chat message (GUI label)? 1 Answer
Fade out GUILayout Area? 1 Answer
Change Color For One Line? 1 Answer
Why does old text in GUILayout not disappear? 1 Answer
GUILayout Icon + Text 3 Answers