2021.11.20网上商城02

一.创建用户操作、用户注册、用户登录脚本
把脚本挂到对应的面板中
在这里插入图片描述
在这里插入图片描述
二、脚本
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 = "账号或者密码错误";
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值