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; } } }