|
Avatar Chat |
|
Before you read
Overview
In this tutorial we will explain how to customize the Avatar Chat example to fit your own needs: plugin, AS3 client, and Unity client. An avatar chat application is one where users see some type of character on the screen for each user in the room. This character usually has the ability to move, and possibly change the image displayed or switch to a different animation.
Live Example
You can view a live example of the AS3 Avatar Chat and the Unity Avatar Chat. AS3 clients and Unity clients are not in the same room; it is possible to do this but unusual for an application to want to and the X,Y coordinates of the AS3 clients don't match the X,Y,Z coordinates of the Unity clients.
Install the Extension
The example extension is configured for use with the AS3 client. The Unity client can use this, however performance will be improved if you edit Extension.xml and set checkChatForTriggerEvents to false. This was not implemented as a GameManager game (although it would be easy to do so), which means that you do not need any server level components. After installing the extension, the example clients can be used with no modification other than possibly editing the settings.xml file to point to the correct IP and port.
Details of the Extension.xml variables:
Plugin Requests
The AvatarChatPlugin is extremely flexible and can fill the room plugin needs of many rooms that are not games, simply by editing the Extension.xml for it. Here are the plugin requests that it can handle.
AS3 Client
After the client connects and logs in, it sends a CreateRoomRequest for a room that has the AvatarChatPlugin attached. Most of the ES5 code at this point is in ChatRoomScreen.as. After joining the room, the client requests a user list, then uses this to display avatars for each of the users in the room, in the correct location and showing the correct color, etc. The client sends a position update on joining the room and when the user clicks to move, but doesn't use any of the other plugin requests. The avatar state events are triggered by chat messages. User enter and exit messages are broadcast to the room, queued, so that clients know to add or remove avatars.
Unity Client
The Unity client similarly connects, logs in, and sends a CreateRoomRequest for a room that has the AvatarChatPlugin attached. Most of the ES5 code at this point is in NetworkController.cs. After joining the room, the local player is spawned and the client requests a user list. The user list is then used to spawn avatars for the other users, using the remotePlayerPrefab found in the Network Assets folder. Position updates are sent whenever the local player notices that its position or rotation has changed, which means a lot of position updates are sent. (see NetworkTransform.cs) The plugin broadcasts them queued which saves some bandwidth. Avatar state requests are sent when the local player changes its animation state. (see AnimationSynchronizer.cs and ThirdPersonController.js). Chat messages are displayed but if the plugin sends avatar state events triggered by chat the emotion changes are ignored. User enter and exit messages are broadcast to the room, queued, so that clients know to add or remove avatars.
How to Handle Vertical Positions
The Avatar Chat example is flat so there's no need to change the Y coordinate. If you need to be able to send position updates for 3D, it's simple. In NetworkTransform.getPlayerPosition, find the line containing POSITION_Y which is commented out. Uncomment it, and comment the line immediately below it which is a hard coded value for Y.
Choice of Avatars
Most games that have avatars allow users to choose an avatar before joining the game. See Rejoin Game for how to do that.