添加TapTap登录
This commit is contained in:
176
Assets/Scripts/UI/LoginPage/LoginPage.cs
Normal file
176
Assets/Scripts/UI/LoginPage/LoginPage.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
using System;
|
||||
using IchniOnline.Online.Logic;
|
||||
using TapSDK.Login;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Ichni.UI
|
||||
{
|
||||
public class LoginPage : UIPageBase
|
||||
{
|
||||
[Header("Button References")]
|
||||
public Button closeButton;
|
||||
public Button tapTapButton;
|
||||
|
||||
[Header("Page References")]
|
||||
public StartUIPage startUIPage;
|
||||
|
||||
private bool _isLoggingIn;
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
|
||||
// 如果 mainCanvasGroup 没有在 Inspector 中赋值,递归查找子物体
|
||||
if (mainCanvasGroup == null)
|
||||
{
|
||||
mainCanvasGroup = GetComponentInChildren<CanvasGroup>(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// 确保初始状态:alpha=0, 不可交互, 不可射线检测
|
||||
// (Canvas 处于 inactive 状态,这些值在激活后生效)
|
||||
if (mainCanvasGroup != null)
|
||||
{
|
||||
mainCanvasGroup.alpha = 0f;
|
||||
mainCanvasGroup.interactable = false;
|
||||
mainCanvasGroup.blocksRaycasts = false;
|
||||
}
|
||||
|
||||
// 在 Start 中绑定按钮事件(确保序列化引用已就绪)
|
||||
if (closeButton != null)
|
||||
{
|
||||
closeButton.onClick.AddListener(OnCloseClicked);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("[LoginPage] closeButton 未赋值");
|
||||
}
|
||||
|
||||
if (tapTapButton != null)
|
||||
{
|
||||
tapTapButton.onClick.AddListener(OnTapTapClicked);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("[LoginPage] tapTapButton 未赋值");
|
||||
}
|
||||
|
||||
// 在 Start 中订阅事件(确保 ThirdPartyServiceManager.Instance 已初始化)
|
||||
SubscribeThirdPartyEvents();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
UnsubscribeThirdPartyEvents();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
// 每次启用时重新订阅(处理 DontDestroyOnLoad 场景切换等情况)
|
||||
SubscribeThirdPartyEvents();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
UnsubscribeThirdPartyEvents();
|
||||
}
|
||||
|
||||
#region ThirdPartyServiceManager Event Subscription
|
||||
|
||||
private void SubscribeThirdPartyEvents()
|
||||
{
|
||||
if (ThirdPartyServiceManager.Instance == null) return;
|
||||
ThirdPartyServiceManager.Instance.OnLoginSuccess -= OnTapTapLoginSuccess;
|
||||
ThirdPartyServiceManager.Instance.OnLoginCanceled -= OnTapTapLoginCanceled;
|
||||
ThirdPartyServiceManager.Instance.OnLoginFailed -= OnTapTapLoginFailed;
|
||||
|
||||
ThirdPartyServiceManager.Instance.OnLoginSuccess += OnTapTapLoginSuccess;
|
||||
ThirdPartyServiceManager.Instance.OnLoginCanceled += OnTapTapLoginCanceled;
|
||||
ThirdPartyServiceManager.Instance.OnLoginFailed += OnTapTapLoginFailed;
|
||||
}
|
||||
|
||||
private void UnsubscribeThirdPartyEvents()
|
||||
{
|
||||
if (ThirdPartyServiceManager.Instance == null) return;
|
||||
ThirdPartyServiceManager.Instance.OnLoginSuccess -= OnTapTapLoginSuccess;
|
||||
ThirdPartyServiceManager.Instance.OnLoginCanceled -= OnTapTapLoginCanceled;
|
||||
ThirdPartyServiceManager.Instance.OnLoginFailed -= OnTapTapLoginFailed;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 点击 Close 按钮:淡出登录页,恢复 StartPage 交互
|
||||
/// </summary>
|
||||
private void OnCloseClicked()
|
||||
{
|
||||
FadeOut(0.5f, false, RestoreStartPage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 点击 TapTap 按钮:发起 TapTap 登录
|
||||
/// </summary>
|
||||
private void OnTapTapClicked()
|
||||
{
|
||||
if (_isLoggingIn) return;
|
||||
|
||||
_isLoggingIn = true;
|
||||
tapTapButton.interactable = false;
|
||||
if (closeButton != null) closeButton.interactable = false;
|
||||
|
||||
ThirdPartyServiceManager.Instance?.StartTapTapLogin();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TapTap 登录成功回调
|
||||
/// </summary>
|
||||
private void OnTapTapLoginSuccess(TapTapAccount account)
|
||||
{
|
||||
_isLoggingIn = false;
|
||||
Debug.Log($"[LoginPage] TapTap 登录成功,用户:{account.name}");
|
||||
|
||||
// 登录成功后淡出登录页
|
||||
FadeOut(0.5f, false, RestoreStartPage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TapTap 登录取消回调
|
||||
/// </summary>
|
||||
private void OnTapTapLoginCanceled()
|
||||
{
|
||||
_isLoggingIn = false;
|
||||
RestoreButtons();
|
||||
Debug.Log("[LoginPage] 用户取消了 TapTap 登录");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TapTap 登录失败回调
|
||||
/// </summary>
|
||||
private void OnTapTapLoginFailed(string error)
|
||||
{
|
||||
_isLoggingIn = false;
|
||||
RestoreButtons();
|
||||
Debug.LogError($"[LoginPage] TapTap 登录失败:{error}");
|
||||
}
|
||||
|
||||
private void RestoreButtons()
|
||||
{
|
||||
if (tapTapButton != null) tapTapButton.interactable = true;
|
||||
if (closeButton != null) closeButton.interactable = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 恢复 StartPage 的交互能力
|
||||
/// </summary>
|
||||
private void RestoreStartPage()
|
||||
{
|
||||
if (startUIPage != null)
|
||||
{
|
||||
startUIPage.RestoreInteraction();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user