小編給大家分享一下Unity實現(xiàn)注冊登錄模塊的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計與策劃設(shè)計,烏蘭網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:烏蘭等地區(qū)。烏蘭做網(wǎng)站價格咨詢:18982081108
使用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>(); } }
以上是“Unity實現(xiàn)注冊登錄模塊的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
標(biāo)題名稱:Unity實現(xiàn)注冊登錄模塊的方法
鏈接地址:http://aaarwkj.com/article26/gjdcjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、微信小程序、虛擬主機、網(wǎng)站設(shè)計公司、品牌網(wǎng)站制作、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)