Chat Room Demo

Hierarchy Worth Noting

  • Playmaker Photon Chat Proxy-This prefab is what makes the magic of Photon Chat happen with Playmaker. This must be in your chat room scene if you want to use any of my Photon Chat actions or Global Events.

  • Logic-This GameObject has two FSMs. One is the 'Chat Manager'. The other just sends new chats when you press the 'enter' key on your keyboard.

  • Canvas-This has the demo stuff in it. There is a panel to capture the user nickname. Then a panel activates for inputting new chats.

Logic

The first two states are just for capturing the nickname typed into the input field. They are pretty self explanatory.

In the 'Photon Chat Connect' state, we use the new custom action 'Photon Chat Connect'. The only variable this requires is a unique username. Photon Chat API doesn't actually check for username duplicates though. Which is odd.

The rest of our FSM relies on Photon Chat callbacks that I've set up with Global Playmaker Events.

After we get our 'OnConnected' callback, I have a state that you may be able to simplify if you want. In the example (pictured below), I'm trying to show that you can subscribe to multiple channels with one action. But, as a developer, you may want the user to be able to choose which channels. So, you'd set up some logic to store each channel as a string variable, add each of those variables into a string Array, and then use that string Array in the action 'Photon Chat Subscribe to Public Channels'.

An Array is required, so if you only plan to have one chat channel for your game, you can prebuild the Array with that name of the single channel.

The 'Photon Chat Subscribe to Public Channels' action also gives you the ability to pull up to 100 past room messages that Photon Engine saves in the cloud. Keep in mind, if all players leave a room, no past room messages are saved.

Once we get our 'OnSubscribed' callback, we move into our next state. In this state, we have another clever Photon Chat action called 'Photon Chat Print Chat History'. You can drop in a TextMeshProUGUI or UI.Text gameobject you are using for your Chat Body, and the action will automatically populate it with the amount of chat history you selected when you subscribed to the channel. Currently this action will pull the history for all channels the user is subscribed to. If you want the ability to only pull history for specific channels, let me know in my discord, and I'll try adding it on a future update.

Anytime anyone in the room sends a chat message, the player will receive the 'OnGetMessages' callback. The callback will also send the message as an event string. So, the first action you need when this global event triggers is 'Get Event String Data'. Then, you can do whatever you want with it. In this demo, I also created a fancy little action, that will automatically take any string, and append it to a TextMeshProUGUI object. This action saves you from having to Get the current text value, building a new string with the event string data, and then setting the text again.

Upcoming Features:

  • Setting sender and message colors on printing chat history.

  • Option to get users in channel.

Last updated