using Cysharp.Threading.Tasks; using Gs2.Core.Model; using Gs2.Unity.Core; using Gs2.Unity.Util; using UnityEngine; public sealed class AdventureLogin : MonoBehaviour { [SerializeField] private string clientId = "YOUR_CLIENT_ID"; [SerializeField] private string clientSecret = "YOUR_CLIENT_SECRET"; [SerializeField] private string accountNamespace = "adventure-account"; [SerializeField] private string accountSlot = "main"; public Gs2Domain Gs2 { get; private set; } public GameSession Session { get; private set; } private bool busy; private string message = "ログインしてください"; private string SaveKey => $"adventure.{clientId}.{accountNamespace}.{accountSlot}"; private void OnGUI() { GUILayout.BeginArea(new Rect(20, 20, 650, 170), GUI.skin.box); GUILayout.Label(message); GUI.enabled = !busy && Session == null; GUILayout.Label("保存先(main / practice など)"); accountSlot = GUILayout.TextField(accountSlot); if (GUILayout.Button("ログイン")) Login().Forget(); GUI.enabled = true; GUILayout.EndArea(); } private async UniTask Login() { busy = true; message = "ログイン中です"; try { if (Gs2 == null) { Gs2 = await Gs2Client.CreateAsync( new BasicGs2Credential(clientId, clientSecret), Region.ApNortheast1); } var userId = PlayerPrefs.GetString(SaveKey + ".userId", ""); var password = PlayerPrefs.GetString(SaveKey + ".password", ""); if (string.IsNullOrEmpty(userId) != string.IsNullOrEmpty(password)) { message = "保存したアカウント情報がそろっていません。保存先を確認してください"; return; } if (string.IsNullOrEmpty(userId)) { var create = await Gs2.Account.Namespace(accountNamespace).CreateAsync(); var model = await create.ModelAsync(); userId = model.UserId; password = model.Password; PlayerPrefs.SetString(SaveKey + ".userId", userId); PlayerPrefs.SetString(SaveKey + ".password", password); PlayerPrefs.Save(); } var login = await Gs2.LoginAsync( new Gs2AccountAuthenticator(new AccountSetting { accountNamespaceName = accountNamespace, keyId = "grn:gs2:{region}:{ownerId}:key:default:key:default" }), userId, password); Session = login; message = "ログインしました。プレイヤー ID: " + Session.AccessToken.UserId; } catch (System.Exception error) { message = "接続できませんでした: " + error.GetType().Name; } finally { busy = false; } } }