一.创建用户操作、用户注册、用户登录脚本
把脚本挂到对应的面板中
二、脚本
2.1 UserOperationPanel
public class UserOperation : Tools
{//单例模式
// 1.私有的构造方法
private UserOperation() { }
//2.私有的静态对象以及初始化
private static UserOperation uo = null;
//3公共的静态的 返回值自身的方法
public static UserOperation GetInstance()
{
if (uo == null)
{
uo = new UserOperation();
}
return uo;
}
//查询当前用户是否已经注册
public Users RrgisterSelect(string name)
{
Users u = null;
string sql = "select account from user where account='" + name + "'";
MySqlDataReader reader = PublicSelect(sql);
while (reader.Read())
{
u = new Users();
u.Name = reader.GetString("account");
}
Close(reader);//关闭资源
return u;
}
}
2.2UserRegPanel
public class UsersRegPanel : MonoBehaviour
{
InputField nameTxt;
InputField passwordTxt;
InputField passwordsTxt;
Text tap;
Button reg;
GameObject userLogin;
void Start()
{
userLogin=GameObject.Find("Canvas").transform.GetChild(11).gameObject;///获取面板
nameTxt = transform.GetChild(3).GetComponent<InputField>();//获取InputField组件,位置如上图(从0开始)
passwordTxt = transform.GetChild(4).GetComponent<InputField>();
passwordsTxt = transform.GetChild(7).GetComponent<InputField>();
tap = transform.GetChild(8).GetComponent<Text>();
reg = transform.GetChild(5).GetComponent<Button>();
reg.onClick.AddListener(RegisterBtn);
}
IEnumerator Span()
{
yield return new WaitForSeconds(3f);
transform.gameObject.SetActive(false);
userLogin.gameObject.SetActive(true);
}
//注册方法
public void RegisterBtn()
{
string name = nameTxt.text;
string password = passwordTxt.text;
string passwords = passwordsTxt.text;
if (name.Equals("") || password.Equals(""))
{
tap.text = "账号或密码不能为空";
}
else
{
if (password.Equals(passwords))//确认密码与密码是否一致
{
UserOperation.GetInstance().RrgisterSelect(name); //根据账号查询用户是否已经存在
int i = UserOperation.GetInstance().Register(name, password);//调用注册方法
if (i > 0)
{
tap.text = "注册成功,请您登录";
StartCoroutine("Span");
}
else
{
tap.text = "该账号已注册!";
}
}
else
{
tap.text = "你输入的两次密码不一致";
}
}
}
}
2.3UserLoginPanel
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MySql.Data.MySqlClient;
/*
*文件描述:
*创始人:
*创建时间:
*修改时间:
*版本:1.0
*/
public class UserLoginPanel : MonoBehaviour {
Transform canvas, loginPanel,Shopping;
InputField nameTxt, passwordTxt;
Button loginBtn, cancelBtn;
Text tap;//提示文本
void Start() {
canvas = GameObject.Find("Canvas").transform;
loginPanel = canvas.GetChild(11);
nameTxt = loginPanel.GetChild(3).GetComponent<InputField>();
passwordTxt = loginPanel.GetChild(4).GetComponent<InputField>();
loginBtn = loginPanel.GetChild(5).GetComponent<Button>();
loginBtn.onClick.AddListener(LoginBtn);//按钮点击
cancelBtn = loginPanel.GetChild(6).GetComponent<Button>();
cancelBtn.onClick.AddListener(CancleBtn);
Shopping = canvas.GetChild(9);
tap = loginPanel.GetChild(7).GetComponent<Text>();
}
private void CancleBtn()
{
nameTxt = null;
passwordTxt = null;
}
private void LoginBtn()
{
string name = nameTxt.text;
string password = passwordTxt.text;
Users u = UserOperation.GetInstance().Login(name, password);
PlayerPrefs.SetInt("uid",u.Id);//把用户信息存起来
PlayerPrefs.SetString("name1",u.Name);
if (u != null)
{
//切换到购物页面
loginPanel.gameObject.SetActive(false);
Shopping.gameObject.SetActive(true);
}
else
{ //提示账号或密码错误
tap.text = "账号或者密码错误";
}
}
}