AGS Game SDK を使用してログインする - デバイス ID でログインする - (Unity モジュール)
Unwrap the wrapper
In this tutorial, you will learn how to implement device ID logins into your game using the AccelByte Gaming Services (AGS) Game SDK.
Byte Wars uses the AuthEssentialsWrapper
class as a wrapper to cache and handle login-related functionalities with the AGS Game SDK. This provides modularity without overriding engine classes.
The AuthEssentialsWrapper
class wraps the ApiClient
and User
class provided by the AGS Game SDK. The User
class handles account-related interactions with AGS that provides the ability to authenticate users and obtain access tokens. After a successful authentication, it will allow you to utilize the AGS features as a user.
The diagram below explains how the AuthEssentialsWrapper
connects the LoginHandler
to use the AGS Game SDK functionalities.
In this tutorial, you will learn how to call the Login
function in User
class from AuthEssentialsWrapper
and set up the OnLoginCompleted
callback to receive the login response from AGS.
What's in the Starter Pack
The starter class AuthEssentialsWrapper_Starter
has been provided for you to modify. It is available in the Resources section.
- AuthEssentialsWrapper_Starter file:
/Assets/Resources/Modules/AuthEssentials/Scripts/AuthEssentialsWrapper_Starter.cs
The AuthEssentialsWrapper_Starter
class already contains the AGS Game SDK libraries.
using AccelByte.Api;
using AccelByte.Core;
using AccelByte.Models;
AuthEssentialsModels
is a file that holds any model data used in AuthEssentialsWrapper_Starter
. It is available in the Resources section.
- AuthEssentialsModels file:
/Assets/Resources/Modules/AuthEssentials/Scripts/AuthEssentialsModels.cs
The AuthEssentialsModels
contains LoginType
, an enum that defines the type of login methods.
public enum LoginType
{
DeviceId
}
Implement logins using the AGS Game SDK
Open Byte Wars in Unity.
In
AuthEssentialsWrapper_Starter.cs
, copy the code below to get the AGS Game SDK'sApiClient
that holds the reference to theUser
class to access login functionalities.// AccelByte's Multi Registry references
private ApiClient apiClient;
private User user;
void Start()
{
apiClient = MultiRegistry.GetApiClient();
user = apiClient.GetApi<User, UserApi>();
}Declare a local property to store data of the current logged-in user, which can be accessed from another script later.
public TokenData userData;
Since the login process is asynchronous, create a callback function called
OnLoginCompleted
to receive the login response from AGS. To keep it modular, add the AGS Game SDK'sResult
andResultCallback
as parameters so theResultCallback
method can later proceed with the login result.private void OnLoginCompleted(Result<TokenData, OAuthError> result, ResultCallback<TokenData, OAuthError> customCallback = null)
{
if (!result.IsError)
{
Debug.Log("Login user successful.");
userData = result.Value;
}
else
{
Debug.Log($"Login user failed. Message: {result.Error.error}");
}
customCallback?.Invoke(result);
}Implement the
Login
method. Add local variables below to store the required values when calling theLogin
method.private PlatformType _platformType;
private string _platformToken;Create a new function called
Login
insideAuthEssentialsWrapper_Starter
with theLoginType
enum and the AGS Game SDK'sResultCallback
as parameters. Inside the function, set the_platformType
toPlatformType.Device
and the_platformToken
toSystemInfo.deviceUniqueIdentifier
. Then, call theLoginWithOtherPlatform()
method in theUser
class with the provided platform type, platform token, and callback function as parameters.public void Login(LoginType loginMethod, ResultCallback<TokenData, OAuthError> resultCallback)
{
switch (loginMethod)
{
case LoginType.DeviceId:
_platformType = PlatformType.Device;
_platformToken = SystemInfo.deviceUniqueIdentifier;
break;
}
Debug.Log("[AuthEssentialsWrapper] Trying to login with device id: "+ _platformToken);
user.LoginWithOtherPlatform(_platformType, _platformToken, result => OnLoginCompleted(result, resultCallback));
}
Resources
- GitHub link to the files in the Unity Byte Wars repository: