A sample project that demonstrates how to implement a server authoritative asynchronous multiplayer game using Unity Gaming Services, without needing a dedicated game server.
For this sample to work, you would first need to publish, zip and deploy your Cloud Code module via the UGS CLI. Remember to install and configure the CLI first. This requires a project ID and a service account key, both of which can be created and found in the Unity Dashboard.
Once the CLI is set up, open the ChessCloudCode/ChessCloudCode.sln
solution in Rider or Visual studio. Publish the solution:
dotnet publish -c Release -r linux-x64 -p:PublishReadyToRun=true
Package the solution in a zip file (note the .ccm
extension), e.g.:
zip -r ChessCloudCode.ccm ~/ChessCloudCode/bin/Release/net7.0/linux-x64/publish/*
Deploy the zip file using the UGS CLI:
ugs deploy ChessCloudCode.ccm
To run the chess sample, import the Chess folder as a Unity project and open the ChessDemo.unity
scene. Then run the scene and you should be able to play chess against yourself. To run another game client with a different player ID, go to File -> Build and Run
.
To call the clear board endpoint, use a service account's key credentials and exchange that for a stateless token.
First encode the secret in base64
echo -n "<KEY_ID>:<SECRET>" | base64
Then exchange those credentials for a token
curl -X POST -H "Authorization: Basic <ENCODED_SECRET>" 'https://services.api.unity.com/auth/v1/token-exchange?projectId=<PROJECT_ID>&environmentId=<ENVIRONMENT_ID>
Then call the clear board endpoint
curl --request POST 'https://cloud-code.services.api.unity.com/v1/projects/<PROJECT_ID>/modules/ChessCloudCode/ClearBoard' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data-raw '{"params": {"session":"demo-session"}}'
To limit access to specific Cloud Code endpoints from authenticated players (i.e. the game client), have a look at the Access Control documentation.
This project uses the Free Low Poly Chess Set asset for the board and chess pieces, and the Gera Chess Library for validating the moves made by players.
See Third Party Notices for more information.