新浪博客

Unity使用UGUI实现登录注册(二)

2017-09-25 22:09阅读:
继上一篇登录注册界面完成,此篇使用代码实现登录、注册功能
共三个脚本:DataStore(存储用户名和密码)、Login(实现登录功能)、Register(实现注册功能)
Unity使用UGUI实现登录注册(二)
创建第一个脚本:DataStore,代码如下:
C# Code
1
2
3
4
5
6
7
8
9
10
11
12


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//存储用户名和密码
public class DataSt
ore : MonoBehaviour
{
//用户名
public const string name = 'name';
//密码
public const string password = '12345678';
}


创建第二个脚本:Login,代码如下:
C# Code
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


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//用于登录判断和注册场景跳转
public class Login : DataStore //继承'DataStore'里的用户名和密码
{
//注册信息
public InputField inputName;//用户名输入框
public InputField inputPaswd;//密码输入框

// 提示信息
public Text ToolTip;
private string stringMassage = '提示信息:';
void Update()
{
ToolTip.text = stringMassage;
}
//点击登录按钮
public void login()
{
string name = inputName.text.Trim(); //保存输入的用户名,后面加以判断
string oldname = PlayerPrefs.GetString(DataStore.name);
string passWord = inputPaswd.text.Trim();//保存输入的密码,后面加以判断
string oldPassWord = PlayerPrefs.GetString(DataStore.password);
//用户名密码输入判断
if (string.IsNullOrEmpty(name))
{
print(stringMassage =
'提示信息:请输入用户名');
return;
}
if (string.IsNullOrEmpty(passWord))
{
print(stringMassage =
'提示信息:请输入密码');
return;
}
if (oldname != name || oldPassWord != passWord)
{
print(stringMassage =
'提示信息:用户名或密码错误');
return;
}
//用户名密码正确则跳转场景
print(stringMassage = '提示信息:登录成功');
SceneManager.LoadScene(
'Play');
}
//点击注册按钮
public void Register()
{
// print(stringmassage='提示信息:注册成功');
SceneManager.LoadScene('Register'); //跳转到注册场景
}
}


第三个脚本:Register,代码如下:
C# Code
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


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//注册
public class Register : DataStore //继承'DataStore'里的用户名和密码
{
//注册信息
public InputField inputName;
public InputField inputPaswd;
public InputField inputPaswd2;
//提示信息
public Text ToolTip;
private string stringmassage;
void Update()
{
ToolTip.text = stringmassage;
}
//点击注册按钮
public void register()
{
string name = inputName.text.Trim();
string password = inputPaswd.text.Trim();
string password2 = inputPaswd2.text.Trim();
//正确注册则跳转场景
if (string.IsNullOrEmpty(name)) //为空
{
print(stringmassage =
'提示信息:请输入用户名');
return;
}
if (string.IsNullOrEmpty(password) || inputPaswd.text.Length != 8) //为空或者长度不等于8
{
print(stringmassage =
'提示信息:请输入8位密码');
return;
}
if (string.IsNullOrEmpty(password2)) //为空
{
print(stringmassage =
'提示信息:请输入确认密码');
return;
}
if (password != password2) //两次密码不一致
{
print(stringmassage =
'提示信息:两次密码输入不正确');
return;
}
print(stringmassage =
'提示信息:注册成功');
//存储用户名和密码
PlayerPrefs.SetString(DataStore.name, name);
PlayerPrefs.SetString(DataStore.password, password);
}
//点击返回登录按钮
public void ReturnLogin()
{
SceneManager.LoadScene(
'Login');
}
}


将Login脚本挂到'Login'场景的相机上:
Unity使用UGUI实现登录注册(二)
将'Login'和'Register'按钮响应方法添加,如图:
Unity使用UGUI实现登录注册(二)
Unity使用UGUI实现登录注册(二)
然后将Register脚本挂到'Register'场景中的相机上:
Unity使用UGUI实现登录注册(二)
同样调整'Register'和'ReturnLogin'按钮的响应方法,如图:
Unity使用UGUI实现登录注册(二)
Unity使用UGUI实现登录注册(二)
最后运行检测OK:
Unity使用UGUI实现登录注册(二)

我的更多文章

下载客户端阅读体验更佳

APP专享