How do I make a RPC Chat

Options
I have been trying to figure this out for a long time now but I am still a complete noob when it comes to photon, how would I make it so when you chat it uses RPC to chat for everyone
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Photon.Pun;

public class GameManager : MonoBehaviour
{
    public int maxMessages = 25;
    public InputField chatBox;

    public GameObject chatPanel, textObject;

    [SerializeField]
    List<Message> messageList = new List<Message>();

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Return))
        {
            if (chatBox.text != "")
            {
                SendMessageToChat(PhotonNetwork.NickName + ": " + chatBox.text);
                chatBox.text = "";
                EventSystem.current.SetSelectedGameObject(null);

            }
            else
            {
                if (!EventSystem.current.currentSelectedGameObject == chatBox.gameObject)
                {
                    chatBox.ActivateInputField();
                }
                else
                {
                    EventSystem.current.SetSelectedGameObject(null);
                }
            }
        }
    }

    public void SendMessageToChat(string text)
    {
        if (messageList.Count >= maxMessages)
        {
            messageList.Remove(messageList[0]);
        }

        Message newMessage = new Message();

        newMessage.text = text;

        GameObject newText = Instantiate(textObject, chatPanel.transform);

        newMessage.textObject = newText.GetComponent<Text>();

        newMessage.textObject.text = newMessage.text;

        messageList.Add(newMessage);
    }
}

[System.Serializable]
public class Message
{
    public string text;
    public Text textObject;
}

Comments

  • AutTheWizard
    Options
    Is there anyone who can please help me with this? I have been working on it for a very long time now and I can't figure it out.
  • You should indicate which PUN you are using. Your example doesn't work with PUN2. Use the example provided with this package PUN2.
  • AutTheWizard
    Options
    What do you mean by that? I am using PUN2, but I don't know what you mean by "Use the example provided with this package PUN2"
  • Klaus Eiperle
    edited March 2020
    Options
    I see... they have removed the Chat example. The old PUN Chat example has to be changed for working with PUN2.
    You need this in the headline: using Photon.Realtime;

    And the script should be a child of Photon. Mine is this:
    public class NetworkInRoomChat : Photon.Pun.MonoBehaviourPun

    Then you should read the RPC instructions in the PUN2 manual and you are done.

    I would not read the input lines in the Update task. Why you don't send the RPC commands by a function like this:
    void OnEndEditInputField(InputField _val) {
    if (_val.text == "") return;
    this.photonView.RPC("Chat", RpcTarget.All, _val.text);
    }