小編給大家分享一下Unity實(shí)現(xiàn)注冊(cè)登錄模塊的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),烏蘭網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:烏蘭等地區(qū)。烏蘭做網(wǎng)站價(jià)格咨詢:18982081108
使用Zenject和UniRx的入門級(jí)技術(shù)實(shí)現(xiàn)了偽登錄注冊(cè)功能。
運(yù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)自動(dòng)跳轉(zhuǎn)到密碼輸入框 userName.OnEndEditAsObservable() .Subscribe((s => password.Select())); //輸入完密碼后敲擊回車鍵或者點(diǎn)擊登錄按鈕 都觸發(fā)登錄事件 var enterDownStream = password.OnEndEditAsObservable() .Select((s => "回車鍵觸發(fā)登錄")); var loginBtnStream = LoginBtn.OnClickAsObservable() .Select((unit => "通過點(diǎn)擊登錄按鈕觸發(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; } }
注冊(cè)面板
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ā)注冊(cè)")); var btnClick = Regist.OnClickAsObservable() .Select((unit => "點(diǎn)擊注冊(cè)按鈕觸發(fā)注冊(cè)")); 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("注冊(cè)成功"); } } else { _tipPanel.Show("注冊(cè)失敗"); } } )); 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().FromInstance(_loginPanel).AsSingle(); Container.Bind ().FromInstance(_registPanel).AsSingle(); Container.Bind ().FromInstance(_tipPanel).AsSingle(); Container.Bind ().FromInstance(_user); } } public class User { public Dictionary _dictionary; public User() { _dictionary=new Dictionary (); } }
以上是“Unity實(shí)現(xiàn)注冊(cè)登錄模塊的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!