PUN - Chat in game (room)

Options
I've checked the demo's and I've checked the documentation but I must have missed something.
I want to figure out how I can let each player talk.

The set up should be:
Person running around, stops to type -> presses enter -> speech bubble comes up above their head with the text in it (this gets shown to everyone in the vicinity).

I figured I'd have to add a Chat Manager empty gameObject in the scene with the necessary script on it?
Or should I add it on the player or on a GUI element which then goes with the player?

If anyone can share some decent documentation on how to do this (I don't think I need the chat API for this right?) I'd be very very grateful!

Example on how I would want it (I can figure out how to make it follow and show on the GUI but i'm for a loss in terms with the code!)

EXAMPLE

Comments

  • Icezman
    Icezman
    edited November 2018
    Options

    THIS IS FOR PUN2

    After a lot of digging around and finding out that there's no real example at ALL for version 2 I got this working:

    Come on Photon Engine team, this is pretty important stuff you should have added or atleast made an example of with the tutorial.... People want to be able to communicate with each other!!

    Just add an empty GameObject to your scene where you want people to talk to each other.
    Then add a photonview component to it and this script. (I called it ChatManager).
    It should work out of the gate!
    (with trouble, just remove the namespace and the final }



    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    using Photon.Pun;

    namespace Be.CoDek.Nyx
    {
    [RequireComponent(typeof(PhotonView))]
    public class ChatManager : MonoBehaviourPunCallbacks, IPunObservable
    {
    public Rect GuiRect = new Rect(0, 0, 250, 300);
    public bool IsVisible = true;
    public bool AlignBottom = false;
    public List messages = new List();
    private string inputLine = "";
    private Vector2 scrollPos = Vector2.zero;

    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", RpcTarget.AllViaServer, 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();

    //for (int i = messages.Count - 1; i >= 0; i--)
    //{
    // GUILayout.Label(messages[i]);
    //}

    for (int i = 0; i < messages.Count; i++)
    {
    GUILayout.Label(messages[i]);

    if (messages.Count >= 10)
    {
    this.messages.RemoveAt(0);
    }
    }

    GUILayout.EndScrollView();

    GUILayout.BeginHorizontal();
    GUI.SetNextControlName("ChatInput");
    inputLine = GUILayout.TextField(inputLine);
    if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
    {
    this.photonView.RPC("Chat", RpcTarget.AllViaServer, 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.NickName;
    }
    }

    this.messages.Add(senderName + ": " + newLine);
    }

    [PunRPC]
    public void Broadcast(string broadcast)
    {
    AddLine(broadcast);
    }

    public void AddLine(string newLine)
    {
    this.messages.Add(newLine);
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    throw new System.NotImplementedException();
    }
    }
    }