398 lines
11 KiB
C#
398 lines
11 KiB
C#
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;
|
||
using Gameplay.Net;
|
||
using NLDPB;
|
||
|
||
public class UI_StartGameInterfaceController : UIWindow
|
||
{
|
||
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;
|
||
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;
|
||
|
||
public GameObject AccountLoginPanel;
|
||
public GameObject VerifyCodeLoginPanel;
|
||
///Auto Gen End///
|
||
|
||
#endregion
|
||
|
||
LoginType loginType;
|
||
LoginData loginSaveData;
|
||
LoginData tempSaveData;
|
||
bool loginOK = false;
|
||
|
||
// deal with input data
|
||
public override void PreLoad(object data = null)
|
||
{
|
||
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);
|
||
AccountLoginPanel = FindObj("UI_LoginWindow/Image_LoginBg/LoginPanel/AccountLogin");
|
||
VerifyCodeLoginPanel = FindObj("UI_LoginWindow/Image_LoginBg/LoginPanel/VerifyCodeLogin");
|
||
|
||
BindButton(Button_AccountLogin_1, () => SetLoginType(LoginType.Account));
|
||
BindButton(Button_VerifyCodeLogin_1, () => SetLoginType(LoginType.PhoneCode));
|
||
BindButton(Button_Login_1, OnButtonLogin);
|
||
BindButton(Button_Login_2, OnButtonLogin);
|
||
BindButton(Button_UserCenter, OnUserCenterButton);
|
||
BindButton(Button_Notice, OnNoticeClick);
|
||
|
||
BindButton(Button_StartGame, OnStartGame);
|
||
|
||
Toggle_RememberPassword.onValueChanged.AddListener(OnRememberPassword);
|
||
Toggle_Agreement_1.onValueChanged.AddListener(OnAgreementAccept);
|
||
Toggle_Agreement_2.onValueChanged.AddListener(OnAgreementAccept);
|
||
|
||
InputField_Account.onValueChanged.AddListener(AccountEdit);
|
||
InputField_Password.onValueChanged.AddListener(PasswordEdit);
|
||
|
||
Text_Version.text = "version:" + Application.version;
|
||
Text_UID.gameObject.SetActive(false);
|
||
|
||
EventManager.Instance.Register(EventManager.EventName.LoginComplete,OnLoginOK);
|
||
|
||
if (loginSaveData.LoginByAccount && loginSaveData.rememberPassword)
|
||
{
|
||
DebugUtil.Log("已有账号密码,自动登录");
|
||
LoginAction();
|
||
}
|
||
else
|
||
{
|
||
if (loginSaveData.LoginByPhone)
|
||
{
|
||
SetLoginType(LoginType.PhoneCode);
|
||
}
|
||
else
|
||
{
|
||
SetLoginType(LoginType.Account);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 进入账号登录界面
|
||
/// </summary>
|
||
/// <param name="type"></param>
|
||
void SetLoginType(LoginType type)
|
||
{
|
||
loginType = type;
|
||
|
||
Text_UID.gameObject.SetActive(false);
|
||
Button_StartGame.gameObject.SetActive(false);
|
||
Button_UserCenter.gameObject.SetActive(false);
|
||
Button_Notice.gameObject.SetActive(false);
|
||
switch (loginType)
|
||
{
|
||
case LoginType.Account://账号密码登录
|
||
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;
|
||
|
||
case LoginType.PhoneCode://手机号验证码登录
|
||
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()
|
||
{
|
||
Image_LoginBg.gameObject.SetActive(false);
|
||
|
||
Button_StartGame.gameObject.SetActive(true);
|
||
Button_Notice.gameObject.SetActive(true);
|
||
Button_UserCenter.gameObject.SetActive(true);
|
||
}
|
||
|
||
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)
|
||
{
|
||
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);
|
||
}
|
||
|
||
#region Button_Listener
|
||
|
||
void OnRememberPassword(bool isOn)
|
||
{
|
||
loginSaveData.rememberPassword = isOn;
|
||
}
|
||
void OnAgreementAccept(bool isOn)
|
||
{
|
||
loginSaveData.agreement = isOn;
|
||
CheckLoginButton();
|
||
}
|
||
|
||
void AccountEdit(string text)
|
||
{
|
||
loginSaveData.account = text;
|
||
InputField_Password.text = string.Empty;
|
||
CheckLoginButton();
|
||
}
|
||
void PasswordEdit(string text)
|
||
{
|
||
loginSaveData.password = text;
|
||
CheckLoginButton();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 尝试登录
|
||
/// </summary>
|
||
void OnButtonLogin()
|
||
{
|
||
if (!loginSaveData.rememberPassword)
|
||
{
|
||
loginSaveData.password = default;
|
||
}
|
||
tempSaveData = new(loginSaveData);//暂存登录数据
|
||
|
||
LoginAction();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 服务器登录
|
||
/// </summary>
|
||
void LoginAction()
|
||
{
|
||
DebugUtil.Log("尝试登录测试服,在此处修改登录逻辑,连接超时以及登录失败请后续补充");
|
||
var serverInfos = AppConfig.Instance.GetServerInfo();
|
||
AppConfig.Instance.Select(1);
|
||
for (int i = 0; i < serverInfos.Count; i++)
|
||
{
|
||
if (serverInfos[i].name == "测试服1")
|
||
AppConfig.Instance.Select(i);
|
||
}
|
||
Gameplay.Net.Actor.Instance.StartLogin(NLDPB.LoginType.Phxh, loginSaveData.account);
|
||
}
|
||
|
||
void OnLoginOK()
|
||
{
|
||
loginOK = true;
|
||
HideLoginPanel();
|
||
|
||
loginSaveData = StorageMgr.Instance.GetStorage<LoginData>("LoginData");
|
||
if (loginSaveData == null)
|
||
loginSaveData = new();
|
||
|
||
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>
|
||
/// 用户中心按钮
|
||
/// </summary>
|
||
void OnUserCenterButton()
|
||
{
|
||
if (loginOK)//TODO 点开用户中心退出原先账号是否需要断开连接
|
||
{
|
||
DebugUtil.Log("登出!断开连接");
|
||
Actor.Instance.OnLogout();
|
||
NetManager.Instance.CloseClient();
|
||
loginOK = false;
|
||
}
|
||
if (loginSaveData.LoginByPhone)
|
||
{
|
||
SetLoginType(LoginType.PhoneCode);
|
||
}
|
||
else
|
||
{
|
||
SetLoginType(LoginType.Account);
|
||
}
|
||
}
|
||
|
||
async void OnNoticeClick()
|
||
{
|
||
await UI_NoticeController.Open();
|
||
}
|
||
|
||
void OnStartGame()
|
||
{
|
||
Actor.Instance.OnEnterGame();
|
||
}
|
||
|
||
#endregion
|
||
|
||
class LoginData
|
||
{
|
||
/// <summary>
|
||
/// 用账号密码成功登录
|
||
/// </summary>
|
||
public bool LoginByAccount = false;
|
||
/// <summary>
|
||
/// 用手机验证成功登录
|
||
/// </summary>
|
||
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;
|
||
}
|
||
}
|
||
}
|