Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 epicVoodoo · Jul 09, 2015 at 09:13 AM · networkingmultiplayerrpcchatmessage

CHAT Multiplayer Problem

Hi there!

I've faced with annoying problem wich happens when players try to send message via chat. Every time when players send message, other's can't synhronize message typer's data like his name.. and their's own position, where this message is going to be located in chat. So here's full script of my chat system and Screenshot representing my errors:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ChatMaster : MonoBehaviour
 {
     public GUISkin skin;
     bool enabled;
 
     class ChatEntry
     {
         public string name = "";
         public string message = "";
     }
 
     ArrayList entries;
     Vector2 currentScrollPos = new Vector2();
     string inputField = "";
     bool chatInFocus = false;
     string inputFieldFocus = "CIFT";
     bool absPos = false;
 
     void Awake()
     {
         InitializeChat();
         AddChatEntry("-SystemMessage-");
     }
 
     void InitializeChat()
     {
         entries = new ArrayList();
         unfocusChat();
     }
 
     void Update()
     {
         if (Input.GetKeyUp(KeyCode.C))
             enabled = !enabled;
     }
 
     void OnGUI()
     {
         GUI.skin = skin;
 
         if (enabled)
         {
             Draw();
         }
     }
 
     //draw the chat box in size relative to your GUIlayout
     public void Draw()
     {
         ChatWindow();
     }
 
     void ChatWindow()
     {
         if (networkView.isMine)
         {
             GUI.BeginGroup(new Rect(0, 100, 500, 750));
 
             GUILayout.BeginVertical(skin.FindStyle("Window"));
             GUILayout.Label("Спутниковая связь", skin.FindStyle("textStyle2"));
             currentScrollPos = GUILayout.BeginScrollView(currentScrollPos, skin.FindStyle("scrollview"), GUILayout.MaxWidth(300), GUILayout.MinWidth(100), GUILayout.MaxHeight(500)); //limits the chat window size to max 1000x1000, remove the restraints if you want
             foreach (ChatEntry ent in entries)
             {
                 GUILayout.BeginHorizontal(skin.FindStyle("Box"));
                 GUI.skin.label.wordWrap = true;
                 GUILayout.Label(ent.name + ent.message);
                 GUILayout.EndHorizontal();
                 GUILayout.Space(3);
             }
         }
         GUILayout.EndScrollView();
         if (chatInFocus)
         {
             GUI.SetNextControlName(inputFieldFocus);
             inputField = GUILayout.TextField(inputField, skin.FindStyle("textfield"), GUILayout.MaxWidth(300), GUILayout.MinWidth(100));
             GUI.FocusControl(inputFieldFocus);
         }
 
         GUILayout.EndVertical();
         GUI.EndGroup();
 
             if (chatInFocus)
             {
                 HandleNewEntries();
             }
         else
         {
             checkForInput();
         }
     }
 
     void unfocusChat()
     {
         //Debug.Log("unfocusing chat");
         inputField = "";
         chatInFocus = false;
     }
 
     void checkForInput(){
         if(Event.current.type == EventType.KeyDown &&  Event.current.character == '\n' && !chatInFocus){
             GUI.FocusControl(inputFieldFocus);
             chatInFocus = true;
             currentScrollPos.y = float.PositiveInfinity;
         }
     }
 
     void HandleNewEntries()
     {
         if (Event.current.type == EventType.KeyDown && Event.current.character == '\n')
         {
             if (inputField.Length <= 0)
             {
                 unfocusChat();
                 return;
             }
             if (inputField.Length < 90)
             {
                 networkView.RPC("AddChatEntry", RPCMode.All, this.GetComponent<Player>().characterName, inputField);
             }
             //AddChatEntry("Cookie monster", inputField); //for offline testing
             unfocusChat();
             //Debug.Log("unfocusing chat and entry sent");
         }
     }
 
     [RPC]
     public void AddChatEntry(string name, string msg)
     {
         HandleRemovingEntries();
         ChatEntry newEntry = new ChatEntry();
         newEntry.name = name + ": "; //For futher displaying in chat
         newEntry.message = msg;
         entries.Add(newEntry);
         currentScrollPos.y = float.PositiveInfinity;
     }
 
     public void AddChatEntry(string msg)
     {
         HandleRemovingEntries();
         ChatEntry newEntry = new ChatEntry();
         newEntry.name = "";
         newEntry.message = msg;
         entries.Add(newEntry);
         currentScrollPos.y = float.PositiveInfinity;
     }
 
     [RPC]
     void RemoveChatEntry()
     {
         entries.RemoveAt(0);
     }
 
     
     void HandleRemovingEntries()
     {
         if (entries.Count > 300)
         {
             networkView.RPC("RemoveChatEntry", RPCMode.All);
 
         }
     }
 }

SCREEN SHOT WITH CHAT PROBLEM

Help me please to locate "if(networkdView.IsMine)" and ".RPCs()" where it's need. I've tried a lot of combinations of it, yet it've brought no results. Hope very much for your responds...

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 tobiass · Jul 10, 2015 at 09:06 AM

With RPCs, you can implement in-room chat only.

That said, please take a look at the Demo "Worker" in the PUN package for a simple but working implementation of RPCs for Chat.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Multiplayer - Opposite names above players 1 Answer

Network Chat not sending messages to other clients 1 Answer

networkView not called on Child objects? 0 Answers

Networking Unknown message ID 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