简单描述一下登陆注册界面的原理:
1.当你需要从一个流程跳转到下一个流程时:
2.当服务器返回信息时,需要在LoginManager.cs中开起事件系统,进行抛事件,在LoginForm.cs中进行监听
如果服务器返回登录成功数据的信息,在LoginForm脚本中监听之后,开始进行跳转场景
LoginForm的脚本
using System;
using GameFramework;
using UnityEngine;
using UnityEngine.UI;
namespace StarForce
{
public class LoginForm : UGuiForm
{
private ProcedureLogin procedureLogin = null;
//需要控制的三个面板
private GameObject loginPageGameObject;//登录界面
private GameObject registPageGameObject;//注册界面
private GameObject totalLoginPageGameObject;//主菜单界面
//totalLoginpage主菜单中按钮
private Button totalLoginPage_LoginBtn;//登录按钮
private Button totalLoginPage_RegistBtn;//注册按钮
//LoginPage登录界面中的按钮
private Button loginPage_ReturnBtn;//返回按钮
private Button loginPage_LoginBtn;//登录按钮
private Button loginPage_RegistBtn;//注册按钮
private InputField loginPage_PasswordIpf;
private InputField loginPage_AccountIpf;
//RegistPage注册界面中的按钮
private Button registPage_ReturnBtn;//返回按钮
private Button registPage_RegistBtn;//注册确认按钮
//注册界面的三个输入框
private InputField registPage_AccountIpf;
private InputField registPage_PasswordIpf;
private InputField registPage_ConfirmPasswordIpf;
protected override void OnInit(object userData)
{
base.OnInit(userData);
InitCom();
}
///
/// 组件初始化
///
void InitCom()
{
loginPageGameObject = transform.Find("LoginPage").gameObject;
registPageGameObject = transform.Find("RegistPage").gameObject;
totalLoginPageGameObject = transform.Find("TotalLoginPage").gameObject;
loginPage_AccountIpf = loginPageGameObject.transform.Find("account/InputField").GetComponent<InputField>();
loginPage_PasswordIpf = loginPageGameObject.transform.Find("password/InputField").GetComponent<InputField>();
registPage_AccountIpf = registPageGameObject.transform.Find("account/InputField").GetComponent<InputField>();
registPage_PasswordIpf = registPageGameObject.transform.Find("password/InputField").GetComponent<InputField>();
registPage_ConfirmPasswordIpf = registPageGameObject.transform.Find("confirmPassword/InputField").GetComponent<InputField>();
totalLoginPage_LoginBtn = totalLoginPageGameObject.transform.Find("LoginBtn").GetComponent<Button>();
if (totalLoginPage_LoginBtn != null)
{
totalLoginPage_LoginBtn.onClick.AddListener(OnTotalLoginPageLoginBtnClick);
}
totalLoginPage_RegistBtn = totalLoginPageGameObject.transform.Find("registBtn").GetComponent<Button>();
if (totalLoginPage_RegistBtn != null)
{
totalLoginPage_RegistBtn.onClick.AddListener(OnTotalLoginPageRegistBtnClick);
}
loginPage_ReturnBtn = loginPageGameObject.transform.Find("closeBtn").GetComponent<Button>();
if (loginPage_ReturnBtn != null)
{
loginPage_ReturnBtn.onClick.AddListener(ReturnToTotalLoginPage);
}
loginPage_LoginBtn = loginPageGameObject.transform.Find("loginBtn").GetComponent<Button>();
if (loginPage_LoginBtn != null)
{
loginPage_LoginBtn.onClick.AddListener(OnClickLoginPageLoginBtn);
}
loginPage_RegistBtn = loginPageGameObject.transform.Find("registBtn").GetComponent<Button>();
if (loginPage_RegistBtn != null)
{
loginPage_RegistBtn.onClick.AddListener(OnTotalLoginPageRegistBtnClick);
}
registPage_ReturnBtn = registPageGameObject.transform.Find("closeBtn").GetComponent<Button>();
if (registPage_ReturnBtn != null)
{
registPage_ReturnBtn.onClick.AddListener(ReturnToTotalLoginPage);
}
registPage_RegistBtn = registPageGameObject.transform.Find("registBtn").GetComponent<Button>();
if (registPage_RegistBtn != null)
{
registPage_RegistBtn.onClick.AddListener(OnRegistPageRegistBtnClick);
}
}
///
/// 注册监听
///
///
protected override void OnOpen(object userData)
{
if (userData != null)
{
procedureLogin = (ProcedureLogin)userData;
}
base.OnOpen(userData);
//添加注册时间成功的监听
EventDispatcher.AddEventListener<UserAccountInfo>(EventKey.OnPlayerRegistSuccess, OnRegistSuceess);
EventDispatcher.AddEventListener<bool>(EventKey.OnPlayerLoginSuccess, OnLoginSuccess);
}
///
/// 登录成功的回调
///
///
private void OnLoginSuccess(bool obj)
{
SetThreePageShowOrHide(false, false, false);
Close();
//跳转流程
procedureLogin.ChangeToCreateRoleRocedure();
}
///
/// 注册成功的回调
///
///
private void OnRegistSuceess(UserAccountInfo obj)
{
GameEntry.UI.OpenDialog(new DialogParams()
{
Mode = 1,
Title = GameEntry.Localization.GetString("Login.RegistSuccess"),
Message = GameEntry.Localization.GetString("Login.RegistSuccessContent"),
});
}
///
/// 移除监听
///
///
protected override void OnClose(object userData)
{
base.OnClose(userData);
EventDispatcher.RemoveEventListener<UserAccountInfo>(EventKey.OnPlayerRegistSuccess, OnRegistSuceess);
EventDispatcher.RemoveEventListener<bool>(EventKey.OnPlayerLoginSuccess, OnLoginSuccess);
}
#region 总界面
///
/// 总的login界面 中的登录btn点击的时候
///
private void OnTotalLoginPageLoginBtnClick()
{
SetThreePageShowOrHide(false, true, false);
}
///
/// 总的login界面 中的注册btn点击的时候
///
private void OnTotalLoginPageRegistBtnClick()
{
SetThreePageShowOrHide(false, false, true);
}
#endregion
#region 登录
#endregion
private void OnClickLoginPageLoginBtn()
{
LoginManger.instance.Account = loginPage_AccountIpf.text;
LoginManger.instance.Password = loginPage_PasswordIpf.text;
LoginManger.instance.SendToServerLoginRequst();
}
#region 注册
void OnRegistPageRegistBtnClick()
{
bool isCanRegist = CheckRegist();
if (isCanRegist)
{
//检验无误
//发送账号密码信息到服务器
UserAccountInfo userRegistInfo = new UserAccountInfo() { account = registPage_AccountIpf.text, password = registPage_PasswordIpf.text };
//把ui中的注册账户信息 暂存Loginmanger中的RegistAccountInfo属性访问器中
LoginManger.instance.RegistAccountInfo = userRegistInfo;
//组装完数据 发送给服务器
LoginManger.instance.SendToServerRegistRequst();
}
else
{
}
}
///
/// 注册之前本地检验
///
///
private bool CheckRegist()
{
bool isCanRegist = true;
if (registPage_AccountIpf.text == string.Empty || registPage_PasswordIpf.text == string.Empty || registPage_ConfirmPasswordIpf.text == string.Empty)
{
isCanRegist = false;
//通用弹框 可以自定义弹框标题 信息 点击之后得执行的方法
GameEntry.UI.OpenDialog(new DialogParams()
{
Mode = 1,
Title = GameEntry.Localization.GetString("Login.Error"),
Message = GameEntry.Localization.GetString("Login.AccountOrPasswordError"),
});
}
if (registPage_AccountIpf.text != string.Empty && registPage_PasswordIpf.text != registPage_ConfirmPasswordIpf.text && registPage_PasswordIpf.text != string.Empty)
{
isCanRegist = false;
//通用弹框 可以自定义弹框标题 信息 点击之后得执行的方法
GameEntry.UI.OpenDialog(new DialogParams()
{
Mode = 1,
Title = GameEntry.Localization.GetString("Login.Error"),
Message = GameEntry.Localization.GetString("Login.passwordDiffer"),
});
}
return isCanRegist;
}
#endregion
///
/// 关闭loginpage 返回TotalLogin
///
private void ReturnToTotalLoginPage()
{
SetThreePageShowOrHide(true, false, false);
}
///
/// 控制三个页面的显示和 隐藏
///
void SetThreePageShowOrHide(bool totalPage, bool loginPage, bool registPage)
{
totalLoginPageGameObject.SetActive(totalPage);
loginPageGameObject.SetActive(loginPage);
registPageGameObject.SetActive(registPage);
}
}
}
LoginManager的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoginManger : Singleton<LoginManger>
{
private string account;
public string Account
{
get { return account; }
set { account = value; }
}
private string password;
public string Password
{
get { return password; }
set { password = value; }
}
///
///暂存发送前玩家注册账户的信息
///
private UserAccountInfo registAccountInfo;
public UserAccountInfo RegistAccountInfo
{
get { return registAccountInfo; }
set { registAccountInfo = value; }
}
///
///暂存玩家注册账户成功后的信息
///
private UserAccountInfo registSuccessAccountInfo;
public UserAccountInfo RegistSuccessAccountInfo
{
get { return registSuccessAccountInfo; }
set
{
if (registSuccessAccountInfo != value)
{
registSuccessAccountInfo = value;
EventDispatcher.TriggerEvent<UserAccountInfo>(EventKey.OnPlayerRegistSuccess, registSuccessAccountInfo);
}
}
}
//是否登录成功
private bool isLoginSuccess = false;
public bool IsLoginSuccess
{
get { return isLoginSuccess; }
set
{
if (isLoginSuccess != value)
{
isLoginSuccess = value;
EventDispatcher.TriggerEvent<bool>(EventKey.OnPlayerLoginSuccess, isLoginSuccess);
}
}
}
#region 注册
///
/// 发送给服务器 注册一个账户
///
public void SendToServerRegistRequst()
{
LoginNetHandler.instance.SentToServerRegistAccountRequest(1000, RegistAccountInfo);
}
///
/// 服务器返回注册结果
///
///
public void OnReceiveServerBackRegistAccountResponse(UserAccountInfo info)
{
RegistSuccessAccountInfo = info;
}
#endregion
#region 登录
///
/// 发送给服务器 登录一个账户
///
public void SendToServerLoginRequst()
{
UserAccountInfo userAccountInfo = new UserAccountInfo() { account = Account, password = Password };
LoginNetHandler.instance.SentToServerLoginAccountRequest(1003, userAccountInfo);
}
///
/// 服务器 返回登录结果
///
public void OnReceiveServerBackLoginAccountResponse(bool info)
{
IsLoginSuccess = info;
}
#endregion
}
public class UserAccountInfo
{
//账户
public string account;
//密码
public string password;
}
LoginNetHandler 的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 和服务器网络连接 数据传输
///
public class LoginNetHandler : Singleton<LoginNetHandler>
{
#region 注册
public void SentToServerRegistAccountRequest(int MsgId,UserAccountInfo registAccountInfo)
{
//发送给服务器
SimulateServerCallBackData.Instance.SimulateRegistBackDate();
}
///
/// 服务器发送回 注册结果 result有信息:注册成功 null: 注册失败
///
///
///
public void OnReceiveServerBackRegistAccountResponse(int MsgId, UserAccountInfo result)
{
//服务器回传数据暂存manager中
LoginManger.instance.OnReceiveServerBackRegistAccountResponse(result);
}
#endregion
#region 登录
public void SentToServerLoginAccountRequest(int MsgId, UserAccountInfo registAccountInfo)
{
//发送给服务器
//OnReceiveServerBackRegistAccountResponse(MsgId, registAccountInfo);
SimulateServerCallBackData.Instance.SimulateLoginBackDate();
}
public void OnReceiveServerBackLoginAccountResponse(int MsgId, bool result)
{
//服务器回传数据暂存manager中
LoginManger.instance.OnReceiveServerBackLoginAccountResponse(result);
}
#endregion
}
SimulateServerCallBackData 简单服务器的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 模拟服务器后端逻辑
///
public class SimulateServerCallBackData : MonoBehaviour {
private static SimulateServerCallBackData instance;
public static SimulateServerCallBackData Instance
{
get { return instance; }
}
private void Awake()
{
instance = this;
}
private Dictionary<string, string> allUser = new Dictionary<string, string>();
void Start () {
for (int i = 10000; i < 10086; i++)
{
allUser.Add(i.ToString(), i.ToString());
}
}
// Update is called once per frame
void Update () {
}
///
/// 服务器返回注册信息
///
public void SimulateRegistBackDate()
{
//把注册的玩家 加到所有玩家的字典中
allUser.Add(LoginManger.instance.RegistAccountInfo.account, LoginManger.instance.RegistAccountInfo.password);
//开始给客户端回数据
LoginNetHandler.instance.OnReceiveServerBackRegistAccountResponse(1000, LoginManger.instance.RegistAccountInfo);
}
///
/// 服务器返回登录信息
///
public void SimulateLoginBackDate()
{
bool isLoginSuccess = false;
if (allUser.ContainsKey(LoginManger.instance.Account) && allUser[LoginManger.instance.Account] == LoginManger.instance.Password)
{
isLoginSuccess = true;
}
LoginNetHandler.instance.OnReceiveServerBackLoginAccountResponse(1003,isLoginSuccess);
}
}