您好,登錄后才能下訂單哦!
小編給大家分享一下Unity實(shí)現(xiàn)注冊登錄模塊的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
使用Zenject和UniRx的入門級技術(shù)實(shí)現(xiàn)了偽登錄注冊功能。
運(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)自動跳轉(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; } }
注冊面板
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 => "點(diǎn)擊注冊按鈕觸發(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>(); } }
以上是“Unity實(shí)現(xiàn)注冊登錄模塊的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。