The first System you need to initialize is the NakamaSystem. This System is responsible for handling connections and authorization with the Nakama server and maintaining the Client, Session and Socket objects.
Add a private variable to store a reference to the NakamaSystem:
1
privateNakamaSystem_nakamaSystem;
In order to initialize the NakamaSystem we need to create an AuthorizerFunc which tells the GDK how you want to authorize users against Nakama.
// Authorizer functionsAuthorizerFuncauthorizer=asyncclient=>{conststringppAuthToken="nakama.AuthToken";conststringppRefreshToken="nakama.RefreshToken";conststringppDeviceId="nakama.DeviceId";varauthToken=PlayerPrefs.GetString(ppAuthToken);varrefreshToken=PlayerPrefs.GetString(ppRefreshToken);varserssion=Session.Restore(authToken,refreshToken);// If the session is valid and has not expired, return itvarexpiredDate=DateTime.UtcNow.AddDays(1);if(session!=null&&!session.HasExpired(expiredDate)){returnsession;}vardeviceId=PlayerPrefs.GetString(ppDeviceId,SystemInfo.deviceUniqueIdentifier);if(deviceId==SystemInfo.unsupportedIdentifier){deviceId=System.Guid.NewGuid().ToString();}try{session=awaitclient.AuthenticateDeviceAsync(deviceId);PlayerPrefs.SetString(ppAuthToken,session.AuthToken);PlayerPrefs.SetString(ppRefreshToken,session.RefreshToken);PlayerPrefs.SetString(ppDeviceId,deviceId);returnsession;}catch(Exceptionex){Debug.LogError($"Error authenticating: {ex.Message}");returnnull;}};
Using the AuthorizerFunc you can now initialize the NakamaSystem and add it to the Systems object you defined earlier:
The NakamaSystem provides some additional quality of life properties and functions, such as providing direct access to the IApiAccount via the Account property or retrieving a storage object via the ReadStorageObjectAsync function.
The NakamaSystem comes with several methods to facilitate logging pre-defined events for the authorized User.
For example:
1
2
3
4
5
// Log an event to say the user installed and opened the app for the first timenakamaSystem.Install();// Log an event to say the user viewed an adnakamaSystem.AdImpression("<AdId>","<Placement>","<Type>");