サンプルコード全文
アカウント発行〜ログインまでを実装するサンプルコード
using System.Collections;
using Gs2.Unity.Util;
using UnityEngine;
public class Tutorial : MonoBehaviour
{
IEnumerator Start()
{
// Setup variables
var clientId = "YourClientId";
var clientSecret = "YourClientSecret";
var accountNamespaceName = "game-0001";
// Initialize GS2 SDK
Gs2Domain gs2;
{
var future = Gs2Client.CreateFuture(
new BasicGs2Credential(
this.clientId,
this.clientSecret
),
Region.ApNortheast1
);
yield return future;
if (future.Error != null) {
throw future.Error;
}
gs2 = future.Result;
}
// define GS2-Account namespace
var gs2Account = gs2.Account.Namespace(
accountNamespaceName
);
// Create an anonymous account
EzAccount account;
{
Debug.Log("Create an anonymous account");
var future = gs2Account.CreateFuture();
yield return future;
if (future.Error != null) {
throw future.Error;
}
var future2 = future.Result.ModelFuture();
yield return future2;
if (future2.Error != null) {
throw future2.Error;
}
account = future2.Result;
}
// Dump anonymous account
Debug.Log($"UserId: {account.UserId}");
Debug.Log($"Password: {account.Password}");
// Log-in created anonymous account
GameSession gameSession;
{
var future = gs2.LoginFuture(
new Gs2AccountAuthenticator(
accountSetting: new AccountSetting {
accountNamespaceName = this.accountNamespaceName,
}
),
account.UserId,
account.Password
);
yield return future;
if (future.Error != null) {
throw future.Error;
}
gameSession = future.Result;
}
// Load TakeOver settings
{
var it = gs2Account.Me(
gameSession
).TakeOvers();
while (it.HasNext()) {
yield return it.Next();
if (it.Error != null)
{
throw it.Error;
}
Debug.Log($"Slot: {it.Current.Slot}");
Debug.Log($"Identifier: {it.Current.UserIdentifier}");
}
}
// Finalize GS2-SDK
{
var future = gs2.DisconnectFuture();
yield return future;
if (future.Error != null) {
throw future.Error;
}
}
}
}
// Gs2Tutorial.cpp
#include "Sample/Gs2Tutorial.h"
#include "Core/Gs2Domain.h"
#include "Util/Gs2AccountAuthenticator.h"
DEFINE_LOG_CATEGORY(GameLog);
class EZGS2_API FSampleTask final : public Gs2::Core::Util::TGs2Future<void*>
{
public:
virtual ~FSampleTask() override = default;
virtual Gs2::Core::Model::FGs2ErrorPtr Action(const TSharedPtr<TSharedPtr<void*>> Result) override
{
// Setup variables
const auto ClientId = "YourClientId";
const auto ClientSecret = "YourClientSecret";
const auto AccountNamespaceName = "game-0001";
// Create GS2 client
const auto InitializeFuture = Gs2::UE5::Core::FGs2Client::Create(
MakeShared<Gs2::Core::Model::FBasicGs2Credential>(
ClientId,
ClientSecret
),
Gs2::Core::Model::ApNorthEast1
);
InitializeFuture->StartSynchronousTask();
if (InitializeFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(InitializeFuture->GetTask().Error()->String()));
return InitializeFuture->GetTask().Error();
}
const auto Gs2 = InitializeFuture->GetTask().Result();
// Create an anonymous account
UE_LOG(GameLog, Display, TEXT("Create anonymous account"));
const auto CreateFuture = Gs2->Account->Namespace(
AccountNamespaceName
)->Create();
CreateFuture->StartSynchronousTask();
if (CreateFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(CreateFuture->GetTask().Error()->String()));
return CreateFuture->GetTask().Error();
}
// Load created account
const auto LoadFuture = CreateFuture->GetTask().Result()->Model();
LoadFuture->StartSynchronousTask();
if (LoadFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(LoadFuture->GetTask().Error()->String()));
return LoadFuture->GetTask().Error();
}
const auto Account = LoadFuture->GetTask().Result();
// Dump anonymous account
UE_LOG(GameLog, Display, TEXT("UserId: %s"), ToCStr(*Account->GetUserId()));
UE_LOG(GameLog, Display, TEXT("Password: %s"), ToCStr(*Account->GetPassword()));
// Log-in created anonymous account
const auto LoginFuture = Gs2->Login(
MakeShareable<Gs2::UE5::Util::IAuthenticator>(
new Gs2::UE5::Util::FGs2AccountAuthenticator(
MakeShared<Gs2::UE5::Util::FAccountSetting>(
AccountNamespaceName
)
)
),
*Account->GetUserId(),
*Account->GetPassword()
);
LoginFuture->StartSynchronousTask();
if (LoginFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(LoginFuture->GetTask().Error()->String()));
return LoginFuture->GetTask().Error();
}
const auto GameSession = LoginFuture->GetTask().Result();
// Load TakeOver settings
const auto It = Gs2->Account->Namespace(
AccountNamespaceName
)->Me(
GameSession
)->TakeOvers();
for (const auto TakeOver : *It)
{
UE_LOG(GameLog, Display, TEXT("Type: %d"), *TakeOver->GetType());
UE_LOG(GameLog, Display, TEXT("Identifier: %s"), ToCStr(*TakeOver->GetUserIdentifier()));
}
// Finalize GS2-SDK
const auto FinalizeFuture = Gs2->Disconnect();
FinalizeFuture->StartSynchronousTask();
if (FinalizeFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(FinalizeFuture->GetTask().Error()->String()));
return FinalizeFuture->GetTask().Error();
}
return nullptr;
}
};
AGs2Tutorial::AGs2Tutorial()
{
PrimaryActorTick.bCanEverTick = true;
}
void AGs2Tutorial::BeginPlay()
{
Super::BeginPlay();
(new FAsyncTask<FSampleTask>())->StartBackgroundTask();
}
void AGs2Tutorial::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Gs2Tutorial.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Gs2Tutorial.generated.h"
DECLARE_LOG_CATEGORY_EXTERN(GameLog, Log, All);
UCLASS()
class EZGS2_API AGs2Tutorial : public AActor
{
GENERATED_BODY()
public:
AGs2Tutorial();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
};