Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cube Boy · Jul 27, 2013 at 08:51 AM · guimenulabelchat

How to destroy a chat message (GUI label)?

Basically i'm working on a GUI and it has a lobby with networking, so others can connect. I used snippets of code from other places to make an addition to my networking lobby that allows people to type and chat between themselves, also notifying when people join or leave the lobby. The only problem is that once you leave the lobby or close the server, then find a new lobby or create a new server, every bit of chat from the last one is still there inside the scrollview. I want each message to either delete or fade away after about 15 seconds so that this problem doesn't occur, and so that the scrollview doesn't get very full. Here is a bit of my code...

 private void ChatFunc(int id)
 {
     if(mySkin) 
     {
         GUI.skin = mySkin;
     }
     
     Scrollposition = GUILayout.BeginScrollView(Scrollposition);
     GUILayout.Label(OutputText);
     GUILayout.EndScrollView();
     
     GUILayout.Space(50);
     
     GUILayout.BeginHorizontal();
     InputText = GUILayout.TextField(InputText, 50);
     if (Event.current.keyCode == KeyCode.Return)
     {
         if(InputText.Length > 0)
         {
             networkView.RPC("SendMsg", RPCMode.All, MultiplayerManager.instance.PlayerName + ": " + InputText);
             Scrollposition.y += 100;
             InputText = "";
         }
     }
     GUILayout.EndHorizontal();
 }
 
 [RPC]
 private void SendMsg(string msg)
 {
     OutputText += msg + "\n";
 }

`

Here is also the line that initialises the chat when you join the lobby (inside ongui)...

 if(CurrentMenu == "Lobby")
 {
     ChatRect = GUI.Window(0,ChatRect,ChatFunc, "");
 }

Finally here are the other bits that are useful...

 private Vector2 Scrollposition;
 private Rect ChatRect = new Rect(1200,710,1300,700);

Ideas anyone? This would really mean a lot if you provide a solution!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Chronos-L · Jul 27, 2013 at 12:17 PM

The only problem is that once you leave the lobby or close the server,

Clean your OutputText right before you do either of the following:

  • leave the lobby or close the server

  • find a new lobby or create a new server (You will have the same chat if you go back to the last lobby you are in)

    I want each message to either delete or fade away after about 15 seconds so that this problem doesn't occur

A better approach would be limiting the number of message-piece you have in the chat-box.

 private Queue<string> chat;
 
 ...
 //Put this in an appropriate location
 if( chat != null ) {
    chat.Clear();
 }
 else {
    chat = new Queue<string>();
 }

 [RPC]
 private void SendMsg(string msg)
 {
     //Insert the message into a Queue (First in first out)
     chat.Enqueue( msg );

     while( chat.Count > 10 ) {
        //Delete the older message when there are
        //more than 10 messages
        chat.Dequeue();
     }
 }

 private void ChatFunc(int id)
 {
    ...
 
    Scrollposition = GUILayout.BeginScrollView(Scrollposition);
    foreach( string s in chat ) {
       GUILayout.Label( s );
    }
    GUILayout.EndScrollView();
 
    ...
 }
 
Comment
Add comment · Show 17 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cube Boy · Jul 28, 2013 at 01:58 AM 0
Share

Thanks for the in-depth response! Could you maybe sort of explain how I would implement this into my code? I would like it to be in a certain position in the lobby as well.

avatar image Jamora · Jul 28, 2013 at 02:24 AM 0
Share

But he did tell you where to implement it into your code, just look above the code snippet.

avatar image Chronos-L · Jul 28, 2013 at 03:11 AM 0
Share

Just to clarify, Queue chat is equivalent to your OutputText in your older code.

You should put this part (it is just cleaning the variable storing the messages, it doesn't matter whether it is a string or a queue) whenever you join/create a new server:

 //Put this in an appropriate location
 if( chat != null ) {
    chat.Clear();
 }
 else {
    chat = new Queue<string>();
 }

All these in the below are just a simple modification of your current code, just keep them where they were:

 [RPC]
 private void Send$$anonymous$$sg(string msg)
 {
    ...
 }
  
 private void ChatFunc(int id)
 {
    ...
 }
avatar image Cube Boy · Jul 28, 2013 at 08:47 AM 0
Share

Cool, so far no errors. The only thing now is how to make it visible again. Before, I had a window that contained the whole thing, which was set by a private Rect, and was what showed the whole thing when the lobby opened. Now with your new bit //Put in the appropriate location it doesn't show up because there is nothing to contain the contents. Any ideas?

avatar image Chronos-L · Jul 28, 2013 at 09:48 AM 0
Share

Please note that these modification/addition will not affect your previous working code, the answer I provided will enhance the behaviour while building on your working code. Regarding the missing GUI element, this should do:

 private void ChatFunc(int id)
 {
    ...
  
    Scrollposition = GUILayout.BeginScrollView(Scrollposition);
       
    foreach( string s in chat ) {
       GUILayout.Label( s );
    }
 
    if( chat.Count == 0 ) {
       //You will get a empty label when you have nothing in the queue
       GUILayout.Label( "" );
    }
 
    GUILayout.EndScrollView();
  
    ...
 }
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

BeginScrollView question on auto increasing 1 Answer

GUILayout Sort Problem 1 Answer

Trouble using C# tooltips 1 Answer

Change Color For One Line? 1 Answer

How to Use Numerous Colors on a GUI Label 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges