欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

Unity实现注册登录模块

时间:2021-05-16 09:37:16|栏目:.NET代码|点击:

使用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#测试本机sql运算速度的代码示例分享

栏    目:.NET代码

下一篇:C# 获取 PC 序列号的方法示例

本文标题:Unity实现注册登录模块

本文地址:http://www.codeinn.net/misctech/122334.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有