This document is about: PUN 1
SWITCH TO

PUN Classic (v1)、PUN 2 和 Bolt 處於維護模式。 PUN 2 將支援 Unity 2019 至 2022,但不會添加新功能。 當然,您所有的 PUN & Bolt 專案可以用已知性能繼續運行使用。 對於任何即將開始或新的專案:請切換到 Photon Fusion 或 Quantum。

Chat Demo

This Chat Demo aims at showing a simple integration of the chat api.
This document gives a quick overview how it works, based on our Chat Demo, which is in the PUN Package.

Chat Demo
Chat Demo showing friend status update

The Chat API revolves around two important part, the IChatClientListenerInterface and the ChatClient class.
Both are handled in a single Component script named ChatGui.

ChatGui

The ChatGui is a dedicated component for this demo, and is a good starting point for modification to suit your game.
It's important to realize that the Chat API is centralized around a single component implementing the IChatClientListener interface to get all the callbacks from the chat, as well as maintaining a ChatClient instance to run the chat system and keep it alive.

IChatClientListener

With the IChatClientListener Interface, you have access to all the callbacks, like connection states, users status changes, subscriptions and incoming messages.
It's concise so you should have no problem getting your head around it.
Please find below the implementation of this IChatClientListener in a Component

C#

    
using UnityEngine;
using System.Collections;
    
using ExitGames.Client.Photon.Chat;
    
public class ChatTest : MonoBehaviour, IChatClientListener 
{
    
    #region IChatClientListener implementation
    
    public void DebugReturn (ExitGames.Client.Photon.DebugLevel level, string message)
    {
        throw new System.NotImplementedException ();
    }
    
    public void OnDisconnected ()
    {
        throw new System.NotImplementedException ();
    }
    
    public void OnConnected ()
    {
        throw new System.NotImplementedException ();
    }
    
    public void OnChatStateChange (ChatState state)
    {
        throw new System.NotImplementedException ();
    }
    
    public void OnGetMessages (string channelName, string[] senders, object[] messages)
    {
        throw new System.NotImplementedException ();
    }
    
    public void OnPrivateMessage (string sender, object message, string channelName)
    {
        throw new System.NotImplementedException ();
    }
    
    public void OnSubscribed (string[] channels, bool[] results)
    {
        throw new System.NotImplementedException ();
    }
    
    public void OnUnsubscribed (string[] channels)
    {
        throw new System.NotImplementedException ();
    }
    
    public void OnStatusUpdate (string user, int status, bool gotMessage, object message)
    {
        throw new System.NotImplementedException ();
    }
    
    #endregion
}
        

ChatClient

Your component will need to first instantiate a ChatClient and keep a reference of that instance.
Your component is then responsible for calling ChatClientInstance.Service() every update to keep connection alive and process incoming messages.
This is more effective and gives important performance control over the chat behavior in your project.

The ChatClient instance is also going to be the main entry point to connect, disconnect, send messages, subscribe to channels, unsubscribe from channels, add friends and change user status.

Integration with PUN

PUN and Chat SDK are complementary and we encourage using both in your project, there is nothing wrong about doing so, and actually it helps with letting photon players communicate with one another even when they are not in the same Room, this is a good example of how the Chat SDK can be integrated in your PUN game.

Do's and Don'ts

Creating a chat service in your game is simple thanks to this SDK.
Nonetheless, here's a quick list of the important things to consider and explore.

Do's

  • Make sure you properly implement the DebugReturn() API callback to get all the information and logs coming from the Chat system.
  • Make good use of the SetOnlineStatus() object Message to share information to all the player's friend.
    It's very powerful, and a lot more efficient then opening public channels or try to come up with a global communication system.
  • Consider using ChatClient.MessageLimit wisely to limit the memory used by chats.
  • The Chat SDK is not just for sending strings, you can be creative and send complex object for powerful communication from app engine to app engine, not just user to user.

Don'ts

  • Don't forget to run Chatclient.Service() in Update() or as often as you need, your component is responsible for this.
  • On compatible platforms, make sure to enable "Run in Background" in the PhotonServerSettings.
    Otherwise, your connection will drop if your application goes in the background.
Back to top