Unity實現(xiàn)注冊登錄模塊
更新時間:2020年02月26日 07:47:52 作者:DwarfTitan
這篇文章主要為大家詳細(xì)介紹了Unity實現(xiàn)注冊登錄模塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
使用Zenject和UniRx的入門級技術(shù)實現(xiàn)了偽登錄注冊功能。
運行效果

登錄面板
using System;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
using Zenject;
public class LoginPanel : MonoBehaviour
{
public InputField userName;
public InputField password;
public Button LoginBtn;
public Button RegistBtn;
[Inject] private User _user;
[Inject] private TipPanel _tipPanel;
[Inject] private RegistPanel _registPanel;
void Start()
{
//用戶名輸入完成后光標(biāo)自動跳轉(zhuǎn)到密碼輸入框
userName.OnEndEditAsObservable()
.Subscribe((s =>
password.Select()));
//輸入完密碼后敲擊回車鍵或者點擊登錄按鈕 都觸發(fā)登錄事件
var enterDownStream = password.OnEndEditAsObservable()
.Select((s => "回車鍵觸發(fā)登錄"));
var loginBtnStream = LoginBtn.OnClickAsObservable()
.Select((unit => "通過點擊登錄按鈕觸發(fā)的登錄"));
Observable.Merge(enterDownStream, loginBtnStream)
.Subscribe((s =>
{
Debug.Log(s);
if (LoginCheak(userName.text,password.text))
{
userName.text=String.Empty;
password.text=String.Empty;
_tipPanel.Show("登錄成功");
}
else
{
userName.text=String.Empty;
password.text=String.Empty;
_tipPanel.Show("登錄失敗");
}
}));
RegistBtn.OnClickAsObservable()
.Subscribe((unit =>
{
this.gameObject.SetActive(false);
_registPanel.gameObject.SetActive(true);
}));
}
public bool LoginCheak(string username,string password)
{
bool isOK = false;
if (_user._dictionary.ContainsKey(username))
{
if (_user._dictionary[username] == password)
{
isOK = true;
}
}
return isOK;
}
}
注冊面板
using UniRx;
using UnityEngine;
using UnityEngine.UI;
using Zenject;
public class RegistPanel : MonoBehaviour
{
[Inject] private TipPanel _tipPanel;
[Inject] private LoginPanel _loginPanel;
[Inject] private User _user;
public InputField userName;
public InputField password01;
public InputField password02;
public Button Regist;
public Button mainMenu;
void Start()
{
//光標(biāo)跳轉(zhuǎn)
userName.OnEndEditAsObservable()
.Subscribe((s => password01.Select()));
password01.OnEndEditAsObservable()
.Subscribe((s => password02.Select()));
var enterPress=password02.OnEndEditAsObservable()
.Select((s => "回車鍵觸發(fā)注冊"));
var btnClick = Regist.OnClickAsObservable()
.Select((unit => "點擊注冊按鈕觸發(fā)注冊"));
Observable.Merge(enterPress, btnClick)
.Subscribe((s =>
{
Debug.Log(s);
if ((userName.text != null) && (password01.text == password02.text))
{
if (_user._dictionary.ContainsKey(userName.text))
{
_tipPanel.Show("用戶名已存在");
}
else
{
_user._dictionary.Add(userName.text,password01.text);
_loginPanel.userName.text = userName.text;
_loginPanel.password.text = password01.text;
_tipPanel.Show("注冊成功");
}
}
else
{
_tipPanel.Show("注冊失敗");
}
}
));
mainMenu.OnClickAsObservable()
.Subscribe((unit =>
{
this.gameObject.SetActive(false);
_loginPanel.gameObject.SetActive(true);
}));
}
}
提示面板
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class TipPanel : MonoBehaviour
{
public Button CloseBtn;
public Text InfoText;
void Start()
{
CloseBtn.OnClickAsObservable()
.Subscribe(Hide);
}
public void Show(string message)
{
InfoText.text = message;
this.gameObject.SetActive(true);
}
private void Hide(Unit unit)
{
InfoText.text = string.Empty;
this.gameObject.SetActive(false);
}
}
Installer
using System.Collections.Generic;
using Zenject;
public class LoginInstaller : MonoInstaller
{
public LoginPanel _loginPanel;
public RegistPanel _registPanel;
public TipPanel _tipPanel;
public User _user=new User();
public override void InstallBindings()
{
Container.Bind<LoginPanel>().FromInstance(_loginPanel).AsSingle();
Container.Bind<RegistPanel>().FromInstance(_registPanel).AsSingle();
Container.Bind<TipPanel>().FromInstance(_tipPanel).AsSingle();
Container.Bind<User>().FromInstance(_user);
}
}
public class User
{
public Dictionary<string, string> _dictionary;
public User()
{
_dictionary=new Dictionary<string, string>();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
C# 指針內(nèi)存控制Marshal內(nèi)存數(shù)據(jù)存儲原理分析
這篇文章主要介紹了C# 指針 內(nèi)存控制 Marshal 內(nèi)存數(shù)據(jù)存儲原理分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
C#窗口轉(zhuǎn)向方式(由一個窗口,跳轉(zhuǎn)到另一個窗口)
這篇文章主要介紹了C#窗口轉(zhuǎn)向方式(由一個窗口,跳轉(zhuǎn)到另一個窗口)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
C#執(zhí)行Javascript代碼的幾種方法總結(jié)
本篇文章主要是對C#執(zhí)行Javascript代碼的幾種方法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
c#實現(xiàn)一個超實用的證件照換底色小工具(附源碼)
這篇文章主要給大家介紹了關(guān)于利用c#實現(xiàn)一個超實用的證件照換底色小工具的相關(guān)資料,通過這個小工具大家可以很方便的進(jìn)行底色的切換,不用再因為底色的原因頭疼了,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01

