Unity实现注册登录模块
作者:DwarfTitan 发布时间:2023-11-24 04:55:22
标签: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>();
}
}
来源:https://blog.csdn.net/weixin_43405845/article/details/104466962


猜你喜欢
- 这两种方式也是大家在日常编码工作当中用的比较多的判断方式、之前在使用的时候也没太关注两者在比较不同类型的时候存在哪些区别今天就和大家一起深入
- import java.io.BufferedInputStream;import java.util.ArrayList;import j
- 1. 前言随着数据量和调用量的增长,用户对应用的性能要求越来越高。另外,在实际的服务中,还存在着这样的场景:系统在组装数据的时候,对于数据的
- Flyweight定义:避免大量拥有相同内容的小类的开销(如耗费内存),使大家共享一个类(元类)。为什么使用共享模式/享元模式面向对象语言的
- 1 问题手写一个程序,完成List集合对象的逆序遍历2 方法创建List接口的多态对象向创建好list集合添加元素使用hasPrevious
- /// <summary> /// 遍历Co
- 这篇文章既介绍一个技术,又记录一个逐渐探索发现的过程,以供大家参考。缘起注意到Java的依赖注入DI规范(起初以为是CDI规范,然后发现是D
- 一、前言现在在我们的项目中,使用多数据源已经是很常见的,下面,这里总结一下springboot整合jdbcTemplate配置多数据源的代码
- 进行软件开发时,通常我们都喜欢使用较新版本的工具,但这里我为什么使
- 常量是在编译时已知并在程序的生存期内不发生更改的不可变值。常量使用 const 修饰符进行声明。只有 C# 内置类型(System.Obje
- 从大学就开始做C#这块,也做C#几年了,最近又从ios转回.Net,继续做C#,之前也没有写博客的习惯,写博客也是从我做ios的时候开始的,
- 前言:如果让大家说出一款国内比较热门的社交软件,那无疑就是QQ和微信了,说到微信,无不例外的会想到微信公众号和小程序,所以现在它们已经是很多
- 介绍随着当今处理器中可用的核心数量的增加, 随着对实现更高吞吐量的需求的不断增长,多线程 API 变得非常流行。 Java 提供了自己的多线
- 本文项目为大家分享了Java实现 * 面五子棋的具体代码,供大家参考,具体内容如下项目介绍:本次设计是基于知识点Java类和对象以及数组开发的
- 写完js倒计时,突然想用java实现倒计时,写了三种实现方式一:设置时长的倒计时;二:设置时间戳的倒计时;三:使用java.util.Tim
- 想要php版的朋友可以到这里下载测试 https://www.jb51.net/codes/83179.htmlimport java.io
- 系统的基本架构 我们假设一个系统System包含Service客户服务中心、Shop网上购物中心和Office网上办公中心三个独立的网站。
- 一、问题反馈今天公司测试向我反馈,系统用户模糊查询功能在用户名称包含特殊字符时(_、\、%)无法正常查询结果。二、问题验证1、当like中包
- 本文详述了android抽奖程序的实现方法,程序为一个抽奖大转盘代码,里面定义了很多图形方法和动画。实现主要功能的SlyderView.ja
- 1.CAS1)CAS概念CAS时Compare And Swap缩写,即比较与交换是用于实现多线程同步的原子指令,它将内存位置的内容与给定值