Steam ログインに SDK を使用する - Steam でログインする - (Unity モジュール)
注釈:本資料はAI技術を用いて翻訳されています。
このチュートリアルモジュールは、Steamworks の制限により WebGL ビルドには適用されません。
ラッパーを開く
このチュートリアルでは、AccelByte Gaming Services (AGS) Software Development Kit (SDK) を使用して、Steam などのシングルプラットフォーム認証でログインする方法を学びます。
Byte Wars には、SinglePlatformAuthWrapper クラスで定義されたゲームインスタンスラッパーがあります。このラッパーは、AGS にログインリクエストを送信するために使用されます。このチュートリアルでは、SinglePlatformAuthWrapper クラスのスターターバージョンである SinglePlatformAuthWrapper_Starter ラッパーを使用して、機能をゼロから実装します。
スターターパックの内容
このチュートリアルに従うには、SinglePlatformAuthWrapper_Starter というスターターラッパークラスを使用します。このラッパーは次のファイルで定義されています。
C# ファイル: Assets/Resources/Modules/Access/SinglePlatformAuth/Scripts/SinglePlatformAuthWrapper_Starter.cs
SinglePlatformAuthWrapper_Starter クラスには、いくつかの事前定義されたコンポーネントが含まれています。
-
AGS SDK インターフェースを参照するためのヘルパー変数。これらの変数は、ラッパーが初期化されるときに割り当てられます。
private User user;
private UserProfiles userProfiles;
private Lobby lobby;
private void Start()
{
...
user = AccelByteSDK.GetClientRegistry().GetApi().GetUser();
userProfiles = AccelByteSDK.GetClientRegistry().GetApi().GetUserProfiles();
lobby = AccelByteSDK.GetClientRegistry().GetApi().GetLobby();
} -
ログイン関数を呼び出すときに使用されるオプションパラメータを保持する変数。これは、後でログインキューを統合する予定がある場合に便利ですが、統合しない場合でも使用できます。パラメータを未設定のままにしておくだけです。
// Optional Parameters
public LoginV4OptionalParameters OptionalParameters = new();
AGS Game SDK を使用したプラットフォームによるログインの実装
-
SinglePlatformAuthWrapper_Starterクラスを開き、プラットフォームトークンを使用してプラットフォームによるログインリクエストを AGS に送信する新しい関数を作成します。リクエストが完了すると、コールバック関数が呼び出されます。public void LoginWithOtherPlatform(
string platformToken,
PlatformType platformType,
ResultCallback<TokenData, OAuthError> resultCallback)
{
LoginWithOtherPlatformV4OptionalParameters optionalParams = new LoginWithOtherPlatformV4OptionalParameters
{
OnQueueUpdatedEvent = OptionalParameters.OnQueueUpdatedEvent,
OnCancelledEvent = OptionalParameters.OnCancelledEvent,
CancellationToken = OptionalParameters.CancellationToken
};
user.LoginWithOtherPlatformV4(
new LoginPlatformType(platformType),
platformToken,
optionalParams,
result => OnLoginWithOtherPlatformCompleted(result, resultCallback));
} -
アカウントが新規の場合にユーザープロファイルを作成または取得する新しい関数を作成します。このプロファイルは、アカウント設定を完了するために必要です。
private void CreateOrGetUserProfile(ResultCallback<UserProfile> resultCallback)
{
userProfiles.GetUserProfile((Result<UserProfile> getUserProfileResult) =>
{
// If not found because it is not yet created, then try to create one.
if (getUserProfileResult.IsError &&
getUserProfileResult.Error.Code == ErrorCode.UserProfileNotFoundException)
{
CreateUserProfileRequest request = new CreateUserProfileRequest()
{
language = System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName,
timeZone = TutorialModuleUtil.GetLocalTimeOffsetFromUTC(),
};
userProfiles.CreateUserProfile(request, resultCallback);
return;
}
resultCallback.Invoke(getUserProfileResult);
});
} -
ユーザー ID、表示名、アバター URL などの公開ユーザーデータを取得する新しい関数を作成します。
private void GetUserPublicData(string userId, ResultCallback<AccountUserPlatformInfosResponse> resultCallback)
{
user.GetUserOtherPlatformBasicPublicInfo("ACCELBYTE", new string[] { userId }, resultCallback);
} -
最後に、ログイン結果を処理するコールバック関数を作成します。ログインが成功した場合、いくつかのアクションを実行します。AGS ロビーに接続し、ユーザープロファイルを取得または作成し、ローカルにキャッシュされる公開ユーザーデータを取得します。
private void OnLoginWithOtherPlatformCompleted(
Result<TokenData, OAuthError> loginResult,
ResultCallback<TokenData, OAuthError> resultCallback = null)
{
// Abort if failed to login.
if (loginResult.IsError)
{
BytewarsLogger.Log($"Failed to login with other platform. Error: {loginResult.Error.error}");
resultCallback?.Invoke(loginResult);
return;
}
// Connect to lobby if haven't.
if (!lobby.IsConnected)
{
lobby.Connect();
}
// Get user profile.
TokenData tokenData = loginResult.Value;
BytewarsLogger.Log("Success to login with other platform. Querying the user profile and user info.");
CreateOrGetUserProfile((Result<UserProfile> userProfileResult) =>
{
// Abort if failed to create or get user profile.
if (userProfileResult.IsError)
{
BytewarsLogger.LogWarning($"Failed to create or get user profile. Error: {userProfileResult.Error.Message}");
resultCallback?.Invoke(Result<TokenData, OAuthError>.CreateError(new OAuthError() { error = userProfileResult.Error.Message }));
return;
}
// Get user public info.
GetUserPublicData(tokenData.user_id, (Result<AccountUserPlatformInfosResponse> userInfoResult) =>
{
// Abort if failed to get user info.
if (userInfoResult.IsError || userInfoResult.Value.Data.Length <= 0)
{
BytewarsLogger.LogWarning($"Failed to get user info. Error: {userInfoResult.Error.Message}");
resultCallback?.Invoke(Result<TokenData, OAuthError>.CreateError(new OAuthError() { error = userInfoResult.Error.Message }));
return;
}
// Save the public user info in the game cache.
AccountUserPlatformData publicUserData = userInfoResult.Value.Data[0];
GameData.CachedPlayerState.PlayerId = publicUserData.UserId;
GameData.CachedPlayerState.AvatarUrl = publicUserData.AvatarUrl;
GameData.CachedPlayerState.PlayerName = AccelByteWarsOnlineUtility.GetDisplayName(publicUserData);
GameData.CachedPlayerState.PlatformId =
string.IsNullOrEmpty(GameData.CachedPlayerState.PlatformId) ?
tokenData.platform_id : GameData.CachedPlayerState.PlatformId;
resultCallback.Invoke(loginResult);
});
});
}
リソース
-
このチュートリアルで使用されるファイルは、Unity Byte Wars GitHub リポジトリで入手できます。