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

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");
StartCoroutine(client.Connect());

Joining a Room

Room room = client.Join ("room_name");
room.OnReadyToConnect += (sender, e) => {
    StartCoroutine(room.Connect());
};

Getting the full room state

room.OnStateChange += OnStateChange;

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

Listening to add/remove on a specific key on the room state

room.Listen ("players/:id", OnPlayerChange);

void OnPlayerChange (DataChange change)
{
    Debug.Log (change.path["id"]);
    Debug.Log (change.operation); // "add" or "remove"
    Debug.Log (change.value); // the player object
}

Listening to specific data changes in the state

room.Listen ("players/:id/:axis", OnPlayerMove);

void OnPlayerMove (DataChange change)
{
    Debug.Log ("OnPlayerMove");
    Debug.Log ("playerId: " + change.path["id"] + ", axis: " + change.path["axis"]);
    Debug.Log (change.value);
}