- Home /
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);
}
}
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.
Your answer
Follow this Question
Related Questions
PUN 2 - Attempting to rejoin room and getting "User does not exist in this game". 1 Answer
How do I instantiate and set the parent of a Gameobject using Photon? 0 Answers
Photon Unity Networking - Player Camera Instantiate 0 Answers
[PUN2] Unable To Connect to Master Server 0 Answers
Photon Server, Tutorial? 1 Answer