![]() |
CotC C# SDK for Unity
v1.4.0.1
Making social games is easy !
|
The Unity SDK is available at the following URL: https://github.com/clanofthecloud/unity-sdk/releases.
We decided to cut it in several parts:
Packages are distributed separately, although we zipped them together due to their relatively small size. Apart from facebook, they also include all their respective dependencies.
You can deselect SampleScript.cs
and SampleScene.unity
if you do not need the sample code. After having imported the package, you just need to configure the SDK settings. For that, open the SampleScene and select the Clan of the Cloud SDK object. In the inspector, set the appropriate values (API Key and API Secret) as configured in the backoffice.
Note: if you want to start from zero, you may simply add a new scene and drag & drop the Clan of the Cloud SDK
prefab object from the CotC/Prefabs
folder into your scene. This object needs to be placed on any scene where you want to use Clan of the Cloud functionality.
Note: if you include any package which provides additional functionality, you need to proceed to its configuration prior to the first compilation. For instance, if you are importing the facebook related package, you will need to import the facebook package as described in the Facebook section. Importing only the core package is the best way to quickly test SDK functionality in your project.
Basic usage is provided by the Clan of the Cloud SDK prefab object. You just have to put it on your scene(s). When creating your project, you can select this object (any instance on any scene) and use the interface under the inspector to set up the CotC credentials as shown below. These credentials identify your game and are used to scope data to your game. You may also configure additional settings.
The available settings are:
Functionality is provided through the Clan of the Cloud SDK prefab object, that should be present on every scene. It is not visible, therefore the position does not matter. Invoke the GetCloud method on it to fetch a CotcSdk.Cloud object allowing to use most features. For that, you may simply use FindObjectOfType<CotcGameObject>.
This code will fetch a cloud object. This operation is asynchronous but will usually take only a very small amount of time, since it only waits for the CotcGameObject to be initialized. You should do it at startup as shown above and keep it as a member of your class.
Another very important object is the CotcSdk.Gamer object. This represents a signed in user, and provides all functionality that requires to be logged in. You will obtain it by logging in through the cloud object.
Please read the section after promises for information about anonymous login, and what to set up next once logged in.
All functions are asynchronous. Due to the current lack of async/await functionality in Unity, we introduced an asynchronous programming pattern borrowed from Javascript, called promises. You can get more info by clicking here.
Getting used to promises can take a bit of time, but in a nutshell, you may simply use them as an optional callback as shown below.
The basic principle means that any method of the API will return an Promise<Type> object, which promises to give a result of that type in the future. The Promise
object provides a few methods which will help:
Let's take an example:
Note that the Then and Catch block return a new promise that can itself be linked to another Then/Catch block. However you should provide a Catch block only after all Then blocks or just before a Done block. The Done method returns no promise, and just tells that it is the last thing that you will do with the result. If you provide a Then block, you may then:
Let's show an example:
Providing a Done block at the end is not mandatory. Doing so will just ensure that the Unhandled exception handler is called in case you do not provide a Catch block. That is why we prefer the use of Done over Then in our examples: it will prevent errors from being eaten up silently. We recommend that you always provide a Catch block to handle the exceptional behaviour, or end your chain with .Done()
as shown above.
But then there is better, let us say that we want to log in the user and then get his profile. We can return another promise from the Then block and provide a Catch block that will be invoked if either of the calls failed.
Note that the above result could technically be achieved the following way as well:
The exceptions provided by the API in Catch blocks are always of type CotcException
. However, note that if an exception happens in one of the Then blocks (e.g. error in your handling code), the exception will be reported to the next Catch block.
Logging in anonymously allows the current user to get access to the CotC functionality without providing any credential, as typically happens the first time. The constructed CotcSdk.Gamer object will contain credentials (GamerId, GamerSecret) which allow to log the gamer back using the CotcSdk.Cloud.Login call. Thus, we recommend you to log in anonymously the first time, and then store the credentials, via PlayerPrefs
for example.
Once logged in, you should also start a domain event loop, which consists of a background network thread to receive network events (messages from other gamers, match events, etc.). You will also likely attach a delegate to the CotcSdk.DomainEventLoop.ReceivedEvent event, raised when an event is received (such as a message from another player).
Since you may log in as many times as you want, what really makes a gamer "active" is the fact that an event loop is running for him. If you want to dismiss (log out) a gamer, you can simply stop the loop and drop your reference to the gamer object.
In case a network error happens, the request is not retried by default. But there is a HttpRequestFailedHandler
member on Cloud which can be set to an user defined callback. This callback tells what to do with the error (retry it, cancel it). The following code retries any failed request twice, once after 100ms, once after 5s, then aborts it.
This should be done at the very startup, after the cloud has been received. The handler is chosen when an HTTP request is built, so if you change it while an HTTP request is running, it will have no effect.
Some function calls use bundles. They act as a generic, typed dictionary. Read more at CotcSdk.Bundle.