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.
The Chat API revolves around two important part, the
IChatClientListenerInterface and the
Both are handled in a single Component script named
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 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
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.
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.
- 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
Messageto 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.MessageLimitwisely 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't forget to run
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.