2026-01-30 17:23:26 +08:00

399 lines
9.9 KiB
C#

using Unity.Services.Core;
using Unity.Services.Authentication.PlayerAccounts;
using Unity.Services.Authentication;
using UnityEngine;
using UnityEngine.UI;
using System;
using TMPro;
public enum UnitySDK_Type
{
none, Anonymous, Google, FaceBook, un
}
public class UnitySDK : SDKBase
{
UnitySDK_Type type = UnitySDK_Type.none;
public UnitySDKCBListener mListener;
public UnitySDK()
{
mListener = SDKMgr.Instance.gameObject.AddComponent<UnitySDKCBListener>();
SDKName = "Unity";
}
#region
public override void Init()
{
base.Init();
UnityServices.Instance.Initialized += () =>
{
Debug.Log("初始化");
mListener.InitSuccessCB(null);
ChooseSDK();
};
UnityServices.InitializeAsync();
}
public override void Login()
{
switch (type)
{
case UnitySDK_Type.Anonymous:
UGS_Anonymous_Start_SignIn_Asnyc();
break;
case UnitySDK_Type.Google:
case UnitySDK_Type.FaceBook:
UGS_UPA_Start_SignIn_Asnyc();
break;
case UnitySDK_Type.un:
UGS_un_Start_SignIn_Asnyc(UGS_un_username, UGS_un_password);
break;
default: break;
}
}
public override void Logout()
{
UGS_SignOut();
}
public override void SwitchAccount()
{
}
public override void Pay(int goodsId, string goodsName, string goodsDesc, int count, float amount, string cpOrderId,
string extrasParams)
{
}
public override void CreateRole()
{
}
public override void EnterGame()
{
}
public override void UpdateRoleLv()
{
}
public override void ExitGame()
{
}
public override bool Exit()
{
return true;
}
public override bool Quit()
{
return true;
}
public override void CallInitSuccessCB()
{
Login();
}
public override void CallLoginSuccessCB()
{
}
#endregion
void ChooseSDK()
{
Transform loginpad = GameObject.Find("UIRoot").transform.Find("UILanuch").Find("Root").Find("Login");
Button btn_anonymous = loginpad.Find("Btn_Login_Anonymous").GetComponent<Button>();
Button btn_google = loginpad.Find("Btn_Login_Google").GetComponent<Button>();
Button btn_facebook = loginpad.Find("Btn_Login_FaceBook").GetComponent<Button>();
Button btn_un = loginpad.Find("Btn_Login_un").GetComponent<Button>();
btn_anonymous.onClick.RemoveAllListeners();
btn_google.onClick.RemoveAllListeners();
btn_facebook.onClick.RemoveAllListeners();
btn_un.onClick.RemoveAllListeners();
btn_anonymous.onClick.AddListener(() => SetType(UnitySDK_Type.Anonymous));
btn_google.onClick.AddListener(() => SetType(UnitySDK_Type.Google));
btn_facebook.onClick.AddListener(() => SetType(UnitySDK_Type.FaceBook));
btn_un.onClick.AddListener(() => SetType(UnitySDK_Type.un));
loginpad.gameObject.SetActive(true);
void SetType(UnitySDK_Type type)
{
this.type = type;
if (type != UnitySDK_Type.un) loginpad.gameObject.SetActive(false);
if (type == UnitySDK_Type.un)
{
UGS_un_InputInfo();
}
SetSignIn();
}
}
void SetSignIn()
{
//设置Signin 挂载
//mListener.InitSuccessCB(null);
Debug.Log($"tzy_{Time.realtimeSinceStartup}");
switch (type)
{
case UnitySDK_Type.Anonymous:
AuthenticationService.Instance.SignedIn += () =>
{
base.SetLogined(true);
Debug.Log($"tzy___LoginSucessCB");
Debug.Log($"tzy_{Time.realtimeSinceStartup}");
mListener.LoginSucessCB(UGS_Anonymous_SetInfo());
};
break;
case UnitySDK_Type.Google:
case UnitySDK_Type.FaceBook:
PlayerAccountService.Instance.SignedIn += UGS_UPA_SignIn_Unity_Asnyc;
break;
case UnitySDK_Type.un:
AuthenticationService.Instance.SignedIn += () =>
{
base.SetLogined(true);
mListener.LoginSucessCB(UGS_un_SetInfo());
};
break;
default: break;
}
}
void UGS_SignOut()
{
AuthenticationService.Instance.SignOut();
base.SetInited(false);
base.SetLogined(false);
//UGS_UpdateUI();
}
#region UGS Unity Player Account
async void UGS_UPA_Start_SignIn_Asnyc()
{
if (PlayerAccountService.Instance.IsSignedIn)
{
base.SetInited(true);
UGS_UPA_SignIn_Unity_Asnyc();
return;
}
try
{
await PlayerAccountService.Instance.StartSignInAsync();
}
catch (RequestFailedException ex)
{
Debug.LogException(ex);
}
}
async void UGS_UPA_SignIn_Unity_Asnyc()
{
try
{
await AuthenticationService.Instance.SignInWithUnityAsync(PlayerAccountService.Instance.AccessToken);
//成功后
base.SetLogined(true);
mListener.LoginSucessCB(UGS_UPA_SetInfo());
}
catch (RequestFailedException ex)
{
Debug.LogException(ex);
}
}
UserInfo UGS_UPA_SetInfo()
{
UserInfo userInfo;
var player = PlayerAccountService.Instance;
if (player.IsSignedIn)
{
Debug.Log("tzy_send userinfo");
userInfo = new UserInfo()
{
uid = player.IdToken,
token = player.AccessToken,
userName = "?"
};
return userInfo;
}
else return null;
}
#endregion UGS Unity Player Account
#region UGS Anonymous
async void UGS_Anonymous_Start_SignIn_Asnyc()
{
try
{
base.SetInited(true);
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
catch (RequestFailedException ex)
{
Debug.LogException(ex);
}
}
UserInfo UGS_Anonymous_SetInfo()
{
UserInfo userInfo;
//var player = PlayerAccountService.Instance;
var auth = AuthenticationService.Instance;
Debug.Log("tzy_send userinfo1");
if (auth.IsSignedIn)
{
Debug.Log("tzy_send userinfo2");
userInfo = new UserInfo()
{
uid = auth.PlayerId,
token = auth.AccessToken,
userName = "?"
};
return userInfo;
}
else return null;
}
#endregion UGS Anonymous
#region UGS un
string UGS_un_username, UGS_un_password;
void UGS_un_InputInfo()
{
Transform loginpad = GameObject.Find("UIRoot").transform.Find("UILanuch").Find("Root").Find("Login");
Transform unpad = loginpad.Find("Rect_un");
Button btn_cancel = unpad.Find("Btn_Group").Find("Btn_Cancel").GetComponent<Button>();
Button btn_confirm = unpad.Find("Btn_Group").Find("Btn_Confirm").GetComponent<Button>();
TMP_InputField input1 = unpad.Find("Content").Find("Input1").GetComponent<TMP_InputField>();
TMP_InputField input2 = unpad.Find("Content").Find("Input2").GetComponent<TMP_InputField>();
input1.text = ""; input2.text = "";
btn_cancel.onClick.RemoveAllListeners(); btn_confirm.onClick.RemoveAllListeners();
btn_cancel.onClick.AddListener(() =>
{
unpad.gameObject.SetActive(false);
});
btn_confirm.onClick.AddListener(() =>
{
string username = input1.text;
string password = input2.text;
UGS_un_SignUp_Async(username, password);
});
unpad.gameObject.SetActive(true);
}
async void UGS_un_SignUp_Async(string username, string password)
{
try
{
await AuthenticationService.Instance.SignUpWithUsernamePasswordAsync(username, password);
Debug.Log("SignUp is successful.");
//UGS_un_SignIn_Async(username, password);
}
catch (AuthenticationException ex)
{
Debug.Log(ex);
UGS_un_SignIn_Async(username, password);
}
}
async void UGS_un_SignIn_Async(string username, string password)
{
try
{
await AuthenticationService.Instance.SignInWithUsernamePasswordAsync(username, password);
Debug.Log("SignIn is successful.");
base.SetLogined(true);
Transform loginpad = GameObject.Find("UIRoot").transform.Find("UILanuch").Find("Root").Find("Login");
loginpad.gameObject.SetActive(false);
}
catch (AuthenticationException ex)
{
Debug.Log(ex);
}
}
async void UGS_un_Start_SignIn_Asnyc(string username, string password)
{
try
{
base.SetInited(true);
await AuthenticationService.Instance.SignInWithUsernamePasswordAsync(username, password);
}
catch (RequestFailedException ex)
{
Debug.LogException(ex);
}
}
UserInfo UGS_un_SetInfo()
{
UserInfo userInfo;
//var player = PlayerAccountService.Instance;
var auth = AuthenticationService.Instance;
if (auth.IsSignedIn)
{
Debug.Log("tzy_send userinfo");
userInfo = new UserInfo()
{
uid = auth.PlayerId,
token = auth.AccessToken,
userName = "?"
};
return userInfo;
}
else return null;
}
#endregion UGS un
}