NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/UI_StartGameInterfaceContro...

440 lines
12 KiB
C#
Raw Normal View History

2024-07-16 15:24:41 +08:00
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
using Button = UnityEngine.UI.Button;
using Image = UnityEngine.UI.Image;
using Toggle = UnityEngine.UI.Toggle;
using ScrollView = PhxhSDK.PhxhScrollView;
using Framework;
2024-07-15 12:20:03 +08:00
using Gameplay.Net;
using NLDPB;
public class UI_StartGameInterfaceController : UIWindow
{
2024-07-15 12:20:03 +08:00
enum LoginType
{
Account,
PhoneCode
}
#region UI_obj
//UI GameObj
///Auto Gen Start///
public Image Image_Bg;
public Image Image_BgDark;
public Button Button_StartGame;
public Image Image_LoginBg;
public Button Button_AccountLogin_1;
public Image Image_YellowLine_Account_1;
public Button Button_VerifyCodeLogin_1;
public Image Image_YellowLine_VerifyCode_1;
public TMP_InputField InputField_Account;
public Image Image_AccountIcon;
public TMP_InputField InputField_Password;
public Image Image_PasswordIcon;
public Toggle Toggle_RememberPassword;
public TMP_Text Text_RememberPassword;
public Toggle Toggle_Agreement_1;
public TMP_Text Text_Agreement_1;
public Button Button_UnLogin_1;
public Button Button_Login_1;
public TMP_InputField InputField_PhoneNumber;
public Image Image_PhoneIcon;
public TMP_InputField InputField_VerifyCode;
public Image Image_VerifyCodeIcon;
public Button Button_GetVerifyCode;
public Button Button_GetVerifyCodeCooling;
public Toggle Toggle_Agreement_2;
public TMP_Text Text_Agreement_2;
2024-07-15 12:20:03 +08:00
public Button Button_UnLogin_2;
public Button Button_Login_2;
public Image Image_UpdateBg;
public Button Button_Confirm;
public Button Button_Cancle;
public Image Image_BottomLine;
public Button Button_UserCenter;
public Button Button_Notice;
public TMP_Text Text_UID;
public TMP_Text Text_Version;
public Image Image_DarkSide;
2024-07-15 12:20:03 +08:00
public GameObject AccountLoginPanel;
public GameObject VerifyCodeLoginPanel;
///Auto Gen End///
#endregion
LoginType loginType;
LoginData loginSaveData;
LoginData tempSaveData;
bool loginOK = false;
2024-07-15 12:20:03 +08:00
2024-07-17 15:29:10 +08:00
int serverIndex = 1;
// deal with input data
public override void PreLoad(object data = null)
2024-07-15 12:20:03 +08:00
{
loginSaveData = StorageMgr.Instance.GetStorage<LoginData>("LoginData");
if (loginSaveData == null)
loginSaveData = new();
}
// Use this for initialization
public override void OnInit()
{
//bind UI GameObj
UI_StartGameInterfaceBinder.GetComponents(this);
2024-07-15 12:20:03 +08:00
AccountLoginPanel = FindObj("UI_LoginWindow/Image_LoginBg/LoginPanel/AccountLogin");
VerifyCodeLoginPanel = FindObj("UI_LoginWindow/Image_LoginBg/LoginPanel/VerifyCodeLogin");
2024-07-17 15:29:10 +08:00
#if DEVELOPMENT_BUILD || UNITY_EDITOR
FindObj("UI_LoginWindow/Image_LoginBg/ALDropdown").gameObject.SetActive(true);
TMP_Dropdown dropdown = FindObj("UI_LoginWindow/Image_LoginBg/ALDropdown").GetComponent<TMP_Dropdown>();
dropdown.options.Clear();
var serverInfo = AppConfig.Instance.GetServerInfo();
if (serverInfo.Count <= 0)
{
return;
}
foreach (var item in serverInfo)
{
dropdown.options.Add(new TMP_Dropdown.OptionData() { text = item.name });
}
var index = PlayerPrefs.GetInt("ServerIndex", 1);
serverIndex = index;
if (index >= 0 && index < serverInfo.Count)
{
dropdown.captionText.text = serverInfo[index].name;
dropdown.value = index;
}
else
{
dropdown.captionText.text = serverInfo[0].name;
dropdown.value = 0;
}
AppConfig.Instance.Select(index);
dropdown.onValueChanged.AddListener((int index) =>
{
DebugUtil.Log("选择服务器:" + index);
AppConfig.Instance.Select(index);
serverIndex = index;
});
#endif
2024-07-15 12:20:03 +08:00
BindButton(Button_AccountLogin_1, () => SetLoginType(LoginType.Account));
BindButton(Button_VerifyCodeLogin_1, () => SetLoginType(LoginType.PhoneCode));
BindButton(Button_Login_1, OnButtonLogin);
2024-07-17 15:29:10 +08:00
BindButton(Button_Login_2, OnButtonLogin);
2024-07-15 12:20:03 +08:00
BindButton(Button_UserCenter, OnUserCenterButton);
2024-07-16 13:11:18 +08:00
BindButton(Button_Notice, OnNoticeClick);
2024-07-15 12:20:03 +08:00
BindButton(Button_StartGame, OnStartGame);
2024-07-17 15:29:10 +08:00
Toggle_RememberPassword.onValueChanged.AddListener(OnRememberPassword);
2024-07-15 12:20:03 +08:00
Toggle_Agreement_1.onValueChanged.AddListener(OnAgreementAccept);
Toggle_Agreement_2.onValueChanged.AddListener(OnAgreementAccept);
InputField_Account.onValueChanged.AddListener(AccountEdit);
InputField_Password.onValueChanged.AddListener(PasswordEdit);
2024-07-15 12:20:03 +08:00
Text_Version.text = "version:" + Application.version;
2024-07-17 15:29:10 +08:00
Text_UID.gameObject.SetActive(false);
2024-07-15 12:20:03 +08:00
2024-07-17 15:29:10 +08:00
EventManager.Instance.Register(EventManager.EventName.LoginComplete, OnLoginOK);
2024-07-17 15:29:10 +08:00
if (loginSaveData.LoginByPhone)
2024-07-15 12:20:03 +08:00
{
2024-07-17 15:29:10 +08:00
SetLoginType(LoginType.PhoneCode);
}
2024-07-15 12:20:03 +08:00
else
{
2024-07-17 15:29:10 +08:00
SetLoginType(LoginType.Account);
}
if (loginSaveData.LoginByAccount && loginSaveData.rememberPassword)
{
DebugUtil.Log("已有账号密码,自动登录");
LoginAction();
}
}
2024-07-15 12:20:03 +08:00
/// <summary>
2024-07-16 15:24:41 +08:00
/// 进入账号登录界面
2024-07-15 12:20:03 +08:00
/// </summary>
/// <param name="type"></param>
void SetLoginType(LoginType type)
{
loginType = type;
2024-07-16 13:11:18 +08:00
Text_UID.gameObject.SetActive(false);
Button_StartGame.gameObject.SetActive(false);
Button_UserCenter.gameObject.SetActive(false);
Button_Notice.gameObject.SetActive(false);
2024-07-15 12:20:03 +08:00
switch (loginType)
{
2024-07-16 15:24:41 +08:00
case LoginType.Account://账号密码登录
2024-07-15 12:20:03 +08:00
Image_LoginBg.gameObject.SetActive(true);
Image_UpdateBg.gameObject.SetActive(false);
Image_YellowLine_Account_1.gameObject.SetActive(true);
Image_YellowLine_VerifyCode_1.gameObject.SetActive(false);
Toggle_RememberPassword.isOn = loginSaveData.rememberPassword;
Toggle_Agreement_1.isOn = loginSaveData.agreement;
AccountLoginPanel.SetActive(true);
VerifyCodeLoginPanel.SetActive(false);
if (loginSaveData.account != default)
{
InputField_Account.text = loginSaveData.account;
}
if (loginSaveData.password != default && loginSaveData.rememberPassword)
{
InputField_Password.text = loginSaveData.password;
}
else
{
InputField_Password.text = string.Empty;
}
break;
2024-07-16 15:24:41 +08:00
case LoginType.PhoneCode://手机号验证码登录
2024-07-15 12:20:03 +08:00
Image_LoginBg.gameObject.SetActive(true);
Image_UpdateBg.gameObject.SetActive(false);
Image_YellowLine_Account_1.gameObject.SetActive(false);
Image_YellowLine_VerifyCode_1.gameObject.SetActive(true);
Toggle_Agreement_2.isOn = loginSaveData.agreement;
AccountLoginPanel.SetActive(false);
VerifyCodeLoginPanel.SetActive(true);
break;
default:
DebugUtil.LogError("SetLoginType error");
break;
}
CheckLoginButton();
}
void HideLoginPanel()
{
2024-07-15 12:20:03 +08:00
Image_LoginBg.gameObject.SetActive(false);
2024-07-16 13:11:18 +08:00
Button_StartGame.gameObject.SetActive(true);
Button_Notice.gameObject.SetActive(true);
Button_UserCenter.gameObject.SetActive(true);
2024-07-15 12:20:03 +08:00
}
void CheckLoginButton()
{
switch (loginType)
{
case LoginType.Account:
if(InputField_Account.text.Length > 0 && InputField_Password.text.Length > 0 && loginSaveData.agreement)
{
Button_Login_1.gameObject.SetActive(true);
}
else
{
Button_Login_1.gameObject.SetActive(false);
}
break;
}
}
//invoke when open window
protected override void OnShowWindow(object data = null)
{
2024-07-16 15:24:41 +08:00
DebugUtil.Log("新版登录界面!目前仅支持登录测试服,密码随意\n====如需登录其他服务器,请: ====");
DebugUtil.Log("在运行游戏前修改DebugStart节点内CmpFakeStart组件内选项选择旧版登录界面");
}
//invoke when reload window
protected override void OnReloadWindow(object data = null)
{
}
//invoke when close window
protected override void OnHideWindow()
{
}
//invoke when destroy
public override void OnRelease()
{
base.OnRelease();
EventManager.Instance.Unregister(EventManager.EventName.LoginComplete, OnLoginOK);
}
2024-07-15 12:20:03 +08:00
2024-07-17 15:29:10 +08:00
#region Button_Listener
2024-07-15 12:20:03 +08:00
void OnRememberPassword(bool isOn)
{
loginSaveData.rememberPassword = isOn;
}
void OnAgreementAccept(bool isOn)
{
loginSaveData.agreement = isOn;
CheckLoginButton();
}
void AccountEdit(string text)
2024-07-15 12:20:03 +08:00
{
loginSaveData.account = text;
InputField_Password.text = string.Empty;
CheckLoginButton();
}
void PasswordEdit(string text)
2024-07-15 12:20:03 +08:00
{
loginSaveData.password = text;
CheckLoginButton();
}
/// <summary>
2024-07-16 15:24:41 +08:00
/// 尝试登录
/// </summary>
2024-07-15 12:20:03 +08:00
void OnButtonLogin()
{
if (!loginSaveData.rememberPassword)
{
loginSaveData.password = default;
}
2024-07-16 15:24:41 +08:00
tempSaveData = new(loginSaveData);//暂存登录数据
2024-07-15 12:20:03 +08:00
LoginAction();
2024-07-15 12:20:03 +08:00
}
/// <summary>
2024-07-16 15:24:41 +08:00
/// 服务器登录
/// </summary>
void LoginAction()
{
2024-07-17 15:29:10 +08:00
#if DEVELOPMENT_BUILD || UNITY_EDITOR
PlayerPrefs.SetInt("ServerIndex", serverIndex);
#else
2024-07-16 15:24:41 +08:00
DebugUtil.Log("尝试登录测试服,在此处修改登录逻辑,连接超时以及登录失败请后续补充");
var serverInfos = AppConfig.Instance.GetServerInfo();
2024-07-16 15:24:41 +08:00
AppConfig.Instance.Select(1);
for (int i = 0; i < serverInfos.Count; i++)
{
2024-07-16 15:24:41 +08:00
if (serverInfos[i].name == "测试服1")
AppConfig.Instance.Select(i);
}
2024-07-17 15:29:10 +08:00
#endif
Gameplay.Net.Actor.Instance.StartLogin(NLDPB.LoginType.Phxh, loginSaveData.account);
}
void OnLoginOK()
{
loginOK = true;
HideLoginPanel();
loginSaveData = StorageMgr.Instance.GetStorage<LoginData>("LoginData");
2024-07-15 17:07:49 +08:00
if (loginSaveData == null)
loginSaveData = new();
2024-07-15 17:07:49 +08:00
if (tempSaveData != null)
loginSaveData.SetData(tempSaveData);
loginSaveData.LoginByAccount = true;
StorageMgr.Instance.SyncForce = true;
Text_UID.gameObject.SetActive(true);
Text_UID.text = "UID: " + Actor.Instance.Base.GetProp(ActorProp.ActorId);
}
/// <summary>
2024-07-16 15:24:41 +08:00
/// 用户中心按钮
/// </summary>
2024-07-15 12:20:03 +08:00
void OnUserCenterButton()
{
2024-07-16 15:24:41 +08:00
if (loginOK)//TODO 点开用户中心退出原先账号是否需要断开连接
{
2024-07-16 15:24:41 +08:00
DebugUtil.Log("登出!断开连接");
Actor.Instance.OnLogout();
NetManager.Instance.CloseClient();
loginOK = false;
}
2024-07-15 12:20:03 +08:00
if (loginSaveData.LoginByPhone)
{
SetLoginType(LoginType.PhoneCode);
}
else
{
SetLoginType(LoginType.Account);
}
}
2024-07-16 13:11:18 +08:00
async void OnNoticeClick()
{
await UI_NoticeController.Open();
}
2024-07-15 12:20:03 +08:00
void OnStartGame()
{
Actor.Instance.OnEnterGame();
2024-07-15 12:20:03 +08:00
}
2024-07-17 15:29:10 +08:00
#endregion
2024-07-15 12:20:03 +08:00
class LoginData
{
/// <summary>
2024-07-16 15:24:41 +08:00
/// 用账号密码成功登录
/// </summary>
2024-07-15 12:20:03 +08:00
public bool LoginByAccount = false;
/// <summary>
2024-07-16 15:24:41 +08:00
/// 用手机验证成功登录
/// </summary>
2024-07-15 12:20:03 +08:00
public bool LoginByPhone = false;
public string account;
public string password;
public bool rememberPassword;
public bool agreement;
public string phoneNumber;
public LoginData()
{
}
public LoginData(LoginData loginData)
{
this.LoginByAccount = loginData.LoginByAccount;
this.LoginByPhone = loginData.LoginByPhone;
this.account = loginData.account;
this.password = loginData.password;
this.rememberPassword = loginData.rememberPassword;
this.agreement = loginData.agreement;
this.phoneNumber = loginData.phoneNumber;
}
public void SetData(LoginData loginData)
{
this.LoginByAccount = loginData.LoginByAccount;
this.LoginByPhone = loginData.LoginByPhone;
this.account = loginData.account;
this.password = loginData.password;
this.rememberPassword = loginData.rememberPassword;
this.agreement = loginData.agreement;
this.phoneNumber = loginData.phoneNumber;
}
2024-07-15 12:20:03 +08:00
}
}