Unity实现注册登录模块
程序员文章站
2022-06-21 08:46:15
使用zenject和unirx的入门级技术实现了伪登录注册功能。运行效果登录面板using system;using unirx;using unityengine;using unityengine...
使用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>(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Ajax使用JSON数据格式案例