您好,登錄后才能下訂單哦!
小編給大家分享一下Unity實現注冊登錄模塊的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
使用Zenject和UniRx的入門級技術實現了偽登錄注冊功能。
運行效果
登錄面板
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() { //用戶名輸入完成后光標自動跳轉到密碼輸入框 userName.OnEndEditAsObservable() .Subscribe((s => password.Select())); //輸入完密碼后敲擊回車鍵或者點擊登錄按鈕 都觸發登錄事件 var enterDownStream = password.OnEndEditAsObservable() .Select((s => "回車鍵觸發登錄")); var loginBtnStream = LoginBtn.OnClickAsObservable() .Select((unit => "通過點擊登錄按鈕觸發的登錄")); 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() { //光標跳轉 userName.OnEndEditAsObservable() .Subscribe((s => password01.Select())); password01.OnEndEditAsObservable() .Subscribe((s => password02.Select())); var enterPress=password02.OnEndEditAsObservable() .Select((s => "回車鍵觸發注冊")); var btnClick = Regist.OnClickAsObservable() .Select((unit => "點擊注冊按鈕觸發注冊")); 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實現注冊登錄模塊的方法”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。