Skip to content

Unity3d Client

Installation

Running the demo server

The colyseus-unity3d comes with a usage example, and a simple room handler for basic testing. You can test it locally by running these commands in your commandline:

cd Server
npm install
npm start

The demo server is using @colyseus/social for user authentication. Please download and install MongoDB locally: https://docs.mongodb.com/manual/installation/#mongodb-community-edition-installation-tutorials

Tip

Ensure you have Node v8+ installed locally to run the server.

Usage

Each Client and Room connections need to run on its own Coroutine. See usage example for more details.

Connecting to the Server

Client client = new Colyseus.Client ("ws://localhost:2567");

Joining a Room

See how to generate your RoomState from State Handling

try {
    Room room = await client.JoinOrCreate<RoomState> ("room_name");
    Debug.Log("Joined successfully!");

} catch (ex) {
    Debug.Log("Error joining: " + ex.Message);
}

Getting the full room state from the server.

room.OnStateChange += OnStateChange;

void OnStateChange (State state, bool isFirstState)
{
    if (isFirstState) {
        // First setup of your client state
        Debug.Log(state);
    } else {
        // Further updates on your client state
        Debug.Log(state);
    }
}

Attaching callbacks to your schema structures

See how to generate your RoomState from State Handling

Room room = await client.Join<RoomState> ("room_name");
Debug.Log("Joined room successfully.");

room.State.players.OnAdd += OnPlayerAdd;
room.State.players.OnRemove += OnPlayerRemove;
room.State.players.OnChange += OnPlayerChange;

void OnPlayerAdd(Player player, string key)
{
    Debug.Log("player added!");
    Debug.Log(player); // Here's your `Player` instance
    Debug.Log(key); // Here's your `Player` key
}

void OnPlayerRemove(Player player, string key)
{
    Debug.Log("player removed!");
    Debug.Log(player); // Here's your `Player` instance
    Debug.Log(key); // Here's your `Player` key
}

void OnPlayerChange(Player player, string key)
{
    Debug.Log("player moved!");
    Debug.Log(player); // Here's your `Player` instance
    Debug.Log(key); // Here's your `Player` key
}

Debugging

If you set a breakpoint in your application while the WebSocket connection is open, the connection will be closed automatically after 3 seconds due to inactivity. To prevent the WebSocket connection from dropping, use pingInterval: 0 during development:

import { Server, RedisPresence } from "colyseus";

const gameServer = new Server({
  // ...
  pingInterval: 0 // HERE
});

Make sure to have a pingInterval higher than 0 on production. The default pingInterval value is 3000.