• 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
Question by Khaledhn1 · Feb 02, 2017 at 08:08 AM · networkingphotonpunchat

Pun In Room Chat Script

hello i am trying to add chat to my game but to avoid random errors i decided to use the pun included chat scrip called "in room chat" i added it and i did some minor adjustments to it but as anyone with eyes will see it calls network functions and RPCs but it has nothing that sends or receives the messages so how do i add that here is the scrips with all the edits i made hope someone can help me. oh and do i add the chat script to the player or an empty game object

 using System.Collections.Generic;
 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(PhotonView))]
 public class InRoomChat : Photon.MonoBehaviour
 {
     public Rect GuiRect = new Rect(0,0, 250,300);
     public bool IsVisible = true;
     public bool AlignBottom = false;
     public List<string> messages = new List<string>();
     private string inputLine = "";
     private Vector2 scrollPos = Vector2.zero;
     public float MAXLINES = 5f;
 
     public static readonly string ChatRPC = "Chat";
 
     public void Start()
     {
         if (this.AlignBottom)
         {
             this.GuiRect.y = Screen.height - this.GuiRect.height;
         }
     }
 
     public void OnGUI()
     {
         if (!this.IsVisible || !PhotonNetwork.inRoom)
         {
             return;
         }
 
         if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
         {
             if (!string.IsNullOrEmpty(this.inputLine))
             {
                 this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
                 this.inputLine = "";
                 GUI.FocusControl("");
                 return; // printing the now modified list would result in an error. to avoid this, we just skip this single frame
             }
             else
             {
                 GUI.FocusControl("ChatInput");
             }
         }
 
         GUI.SetNextControlName("");
         GUILayout.BeginArea(this.GuiRect);
 
         scrollPos = GUILayout.BeginScrollView(scrollPos);
         GUILayout.FlexibleSpace();
          GUI.backgroundColor = new Color(100, 100, 255, 0.5f);
             for (int i = 0; i < messages.Count; i++)
         {
             GUILayout.Label(messages[i]);
         }
         GUILayout.EndScrollView();
 
         GUILayout.BeginHorizontal();
         GUI.SetNextControlName("ChatInput");
         inputLine = GUILayout.TextField(inputLine);
         if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
         {
             this.photonView.RPC("Chat", PhotonTargets.All, this.inputLine);
             this.inputLine = "";
             GUI.FocusControl("");
         }
 
         GUILayout.EndHorizontal();
         GUILayout.EndArea();
     }
 
     [PunRPC]
     public void Chat(string newLine, PhotonMessageInfo mi)
     {
         string senderName = "anonymous";
 
         if (mi.sender != null)
         {
             if (!string.IsNullOrEmpty(mi.sender.NickName))
             {
                 senderName = mi.sender.NickName;
             }
             else
             {
                 senderName = "player " + mi.sender.ID;
             }
         }
 
         this.messages.Add(senderName +": " + newLine);
         while (messages.Count > MAXLINES)
             messages.RemoveAt(0);
     }
 
     public void AddLine(string newLine)
     {
         this.messages.Add(newLine);
     }
 }
 

Comment

People who like this

0 Show 0
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

Answer by ChristianSimon · Feb 02, 2017 at 08:43 AM

Attaching this component to a player you instantiate using PhotonNetwork.Instantiate(...) call is a possibility, you can also use a new or another game object for this.

but it has nothing that sends or receives the messages

I don't understand this, since the script works out of the box. It uses the OnGUI() function to display the chat box UI. Having it attached to a game object and running the game (connected to Photon and joined a room is a requirement), you should at least see an input field and a send button. When messages have been sent, these are displayed above the input field.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

[PUN2] Unable To Connect to Master Server 0 Answers

Photon Server, Tutorial? 1 Answer

The other client does not receive chat messages.(Photon Networking) 1 Answer

PUN - Unity 2D Synchroniztion 0 Answers

OnRoomsListUpdate() is not being called.,OnRoomsListUpdate not being called 1 Answer


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