服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity实现注册登录模块

Unity实现注册登录模块

2022-08-28 16:27DwarfTitan C#

这篇文章主要为大家详细介绍了Unity实现注册登录模块,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

使用Zenject和UniRx的入门级技术实现了伪登录注册功能。

运行效果

Unity实现注册登录模块

登录面板

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
 }
 
}

注册面板

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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);
 }));
 }
}

提示面板

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

延伸 · 阅读

精彩推荐
  • C#C#实现Ruby的负数索引器

    C#实现Ruby的负数索引器

    这篇文章主要介绍了C#实现Ruby的负数索引器的相关代码和使用方法,非常简单实用,需要的朋友可以参考下...

    C#教程网5802021-12-02
  • C#c# 实现子窗口关闭父窗口也关闭的方法

    c# 实现子窗口关闭父窗口也关闭的方法

    下面小编就为大家带来一篇c# 实现子窗口关闭父窗口也关闭的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网11652021-12-20
  • C#C#Winform窗口移动方法

    C#Winform窗口移动方法

    今天小编就为大家分享一篇C#Winform窗口移动方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Maybe_ch8032022-03-06
  • C#C#中DataBindings用法实例分析

    C#中DataBindings用法实例分析

    这篇文章主要介绍了C#中DataBindings用法,结合实例形式详细分析了DataBindings绑定数据源及刷新数据的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    aparche8182021-11-24
  • C#c# 实现子窗口关闭父窗口也关闭的简单实例

    c# 实现子窗口关闭父窗口也关闭的简单实例

    下面小编就为大家带来一篇c# 实现子窗口关闭父窗口也关闭的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看...

    C#教程网6502021-12-27
  • C#几分钟搞懂c#之FileStream对象读写大文件(推荐)

    几分钟搞懂c#之FileStream对象读写大文件(推荐)

    这篇文章主要介绍了c#之FileStream对象读写大文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面...

    牛掰是怎么形成的7752022-07-21
  • C#c# GridControl的模糊查询实现代码

    c# GridControl的模糊查询实现代码

    这篇文章主要介绍了c# GridControl的模糊查询实现代码,需要的朋友可以参考下...

    C#教程网6582021-12-24
  • C#C#和lua相互调用的方法教程

    C#和lua相互调用的方法教程

    lua是一种脚本语言,可以方便的移植到各种宿主语言中,并且可以支持热更新,在游戏开发中也能当做主要的语言来编写游戏的逻辑,所以这篇文章主要给...

    Mr.小Y5042022-01-25