Unity实现注册登录模块
作者:DwarfTitan 时间:2023-11-24 04:55:22
使用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>();
}
}
来源:https://blog.csdn.net/weixin_43405845/article/details/104466962
标签:Unity,注册,登录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
获取Android手机中所有短信的实现代码
2023-08-04 16:55:30
妙解Java中的回调机制(CallBack)
2022-07-15 15:25:31
微信跳一跳辅助Java代码实现
2022-03-31 19:12:03
Java基础篇之反射机制示例详解
2021-12-08 04:05:25
![](https://img.aspxhome.com/file/2023/9/113379_0s.png)
Unity实现图片轮播组件
2023-03-19 05:12:33
![](https://img.aspxhome.com/file/2023/9/100159_0s.gif)
Android Q适配之IMEI替换为Android_id
2022-08-08 00:46:01
![](https://img.aspxhome.com/file/2023/6/138626_0s.jpg)
C#进制之间的相互转换详解
2022-07-24 11:47:53
全面解析Android应用开发中Activity类的用法
2022-03-11 00:48:53
![](https://img.aspxhome.com/file/2023/3/137863_0s.png)
Java中类的加载顺序执行结果
2022-03-12 18:12:35
C# dataset存放多张表的实例
2022-10-10 14:08:13
Android Studio 3.5版本JNI生成SO文件详解
2022-06-11 19:34:11
![](https://img.aspxhome.com/file/2023/0/137910_0s.png)
Java类的定义以及执行顺序学习教程
2023-01-14 10:44:29
SpringBoot实现本地存储文件上传及提供HTTP访问服务的方法
2022-09-14 19:09:12
![](https://img.aspxhome.com/file/2023/9/63749_0s.png)
Android 正则表达式验证手机号、姓名(包含少数民族)、身份证号
2022-05-26 02:17:11
Android Studio 新建项目通过git上传到码云图文教程详解
2021-08-09 12:12:05
![](https://img.aspxhome.com/file/2023/5/92595_0s.png)
Android开源AndroidSideMenu实现抽屉和侧滑菜单
2023-10-09 09:24:51
![](https://img.aspxhome.com/file/2023/2/111722_0s.jpg)
如何使用IDEA的groovy脚本文件生成带JPA注解的实体类(图文详解)
2022-02-04 02:42:04
![](https://img.aspxhome.com/file/2023/1/129981_0s.jpg)
C#设计模式之简单工厂模式
2023-10-19 21:57:30
![](https://img.aspxhome.com/file/2023/1/106341_0s.jpg)
JAVA使用POI获取Excel的列数与行数
2021-10-03 12:53:37
SpringBoot优雅地实现全局异常处理的方法详解
2023-09-06 09:17:02