Unity实现注册登录模块

所属分类: 软件编程 / C#教程 阅读数: 155
收藏 0 赞 0 分享

使用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>();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多