[Unity+MySQL] Realize the registration and login system (package version)


Following the registration and login system in the previous article, this article encapsulates MySQL-related operations, and directly calls the encapsulated method in the Unity interactive script.

1 MySQL package

Write a DBConnector script to encapsulate common operations in MySQL, such as connecting to the database, closing the database, querying the database, inserting, updating, and deleting other than querying.

using System;
using System.Data;
using MySql.Data.MySqlClient;


namespace Utils
{
    
    
    public class DBConnector
    {
    
    
        private string connectionString; // 存储 MySQL 连接字符串
        private MySqlConnection connection; // 存储 MySQL 连接实例

        // 构造函数,接收 MySQL 连接参数
        public DBConnector(string server, string database, string uid, string password)
        {
    
    
            // 创建 MySQL 连接字符串
            connectionString = $"Server={
      
      server};Database={
      
      database};Uid={
      
      uid};Pwd={
      
      password};";
        }

        // 打开 MySQL 连接
        public bool OpenConnection()
        {
    
    
            try
            {
    
    
                // 创建 MySQL 连接实例
                connection = new MySqlConnection(connectionString);
                // 打开 MySQL 连接
                connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
    
    
                // 输出连接错误信息
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        // 关闭 MySQL 连接
        public bool CloseConnection()
        {
    
    
            // 判断连接是否为空
            if (connection == null)
            {
    
    
                return false;
            }
            try
            {
    
    
                // 关闭 MySQL 连接
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
    
    
                // 输出错误信息
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 向指定的 MySQL 数据库发送 SQL 语句并返回结果
        /// </summary>
        /// <param name="query">接受一个字符串作为参数表示查询</param>
        /// <returns></returns>
        public DataTable SelectQuery(string query)
        {
    
    
            // 创建 empty DataTable,存储返回的结果
            var dataTable = new DataTable();
            // 创建一个命令对象,以便在数据库上执行查询
            var command = new MySqlCommand(query, connection);
            // 执行查询并返回结果
            var reader = command.ExecuteReader();
            // 将查询结果加载到 DataTable 中
            dataTable.Load(reader);
            // 关闭数据读取器对象
            reader.Close();
            // 返回查询结果
            return dataTable;
        }

        /// <summary>
        /// 执行非查询语句(如 Insert, Update, Delete)
        /// </summary>
        /// <param name="query">非查询语句</param>
        public void ExecuteNonQuery(string query)
        {
    
    
            // 创建命令对象
            var command = new MySqlCommand(query, connection);
            // 执行非查询语句
            command.ExecuteNonQuery();
        }
    }
}

2 Encapsulation of user registration and login methods

Write a User script to encapsulate user registration and login methods, which can be directly called by Unity interactive scripts.

using System;
using Utils;

public class User
{
    
    
    private DBConnector dbConnector; // 数据库连接器

    // 构造函数,传入用于连接数据库的连接器
    public User(DBConnector dbConn)
    {
    
    
        dbConnector = dbConn; // 保存连接器对象
    }

    // 用户注册
    public int Register(string username, string password)
    {
    
    
        // 查询用户名的数量
        var query1 = $"SELECT COUNT(*) FROM usersinfo WHERE username = '{
      
      username}'";
        // 插入一条新用户记录
        var query2 = $"INSERT INTO usersinfo (username, password) VALUES ('{
      
      username}', '{
      
      password}')";

        // 尝试与数据库建立连接
        if (dbConnector.OpenConnection() == true)
        {
    
    
            try
            {
    
    
                // 执行查询用户名数量的语句
                var dataTable = dbConnector.SelectQuery(query1);
                // 从查询结果中获取数量
                int count = int.Parse(dataTable.Rows[0][0].ToString());

                if (count > 0) // 如果已存在同名用户
                {
    
    
                    dbConnector.CloseConnection(); // 关闭连接
                    return 2; // 用户名已存在
                }

                dbConnector.ExecuteNonQuery(query2); // 执行插入语句插入新用户记录
                dbConnector.CloseConnection(); // 关闭连接
                return 1; // 注册成功
            }
            catch (Exception e)
            {
    
    
                Console.WriteLine(e.Message);
                dbConnector.CloseConnection();
                return 4; //插入数据失败
            }
        }
        else
        {
    
    
            return 3; //连接错误
        }
    }

    // 用户登录
    public int Login(string username, string password)
    {
    
    
        // 查询指定用户名的记录
        var query = $"SELECT * FROM usersinfo WHERE username = '{
      
      username}' LIMIT 1";

        // 尝试与数据库建立连接
        if (dbConnector.OpenConnection() == true)
        {
    
    
            // 执行查询指定用户名记录的语句
            var dataTable = dbConnector.SelectQuery(query);

            // 查询用户输入的用户名是否存在于数据库中
            if (dataTable.Rows.Count == 1)
            {
    
    
                // 获取查询结果中的用户密码
                string storedPassword = dataTable.Rows[0]["password"].ToString();
                // 如果密码与输入密码匹配
                if (storedPassword == password)
                {
    
    
                    dbConnector.CloseConnection(); // 关闭连接
                    return 1; // 登录成功
                }
                else
                {
    
    
                    dbConnector.CloseConnection(); // 关闭连接
                    return 2; // 密码错误
                }
            }
            else
            {
    
    
                dbConnector.CloseConnection(); // 关闭连接
                return 3; // 用户名不存在
            }
        }
        else
        {
    
    
            return 4; // 连接错误
        }
    }
}

Among them, because there are various situations when registering and logging in, enumeration is used here to list these situations:

namespace Static
{
    
    
    public class StaticData
    {
    
    
        public enum RegisterCode
        {
    
    
            RegisterSuccess = 1, // 注册成功
            UsernameDoesExist = 2, // 用户名已存在
            ConnectionError = 3, // 连接错误
            InsertDataError = 4, // 插入数据失败
        };
        public enum LoginCode
        {
    
    
            LoginSuccess = 1, // 登录成功
            IncorrectPassword = 2, // 密码不正确
            UsernameDoesNotExist = 3, // 用户名不存在
            ConnectionError = 4, //连接错误
        };
    }
}

3 Unity interaction

When interacting with Unity, the UI interface remains the same as before without any changes.
Write a UsersManager script here inherited from MonoBehaviour to manage user registration and login.

using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using Utils;

public class UsersManager : MonoBehaviour
{
    
    
    // 注册UI和登录UI
    public GameObject RegisterUI;
    public GameObject LoginUI;

    // 用户名输入框和密码输入框
    public InputField usernameInputField;
    public InputField passwordInputfield;

    // 注册消息和登录消息
    public Text registerMessage;
    public Text loginMessage;

    // DBConnector类实例化
    public DBConnector connector = new DBConnector("localhost","unitygame","root","123456");

    // User类实例化
    public User user;
    void Start()
    {
    
    
        // 初始化UI状态
        LoginUI.SetActive(true);
        RegisterUI.SetActive(false);

        //连接数据库
        user = new User(connector);
    }

    // 加密密码
    private static string HashPassword(string password)
    {
    
    
        SHA256Managed crypt = new SHA256Managed();
        StringBuilder hash = new StringBuilder();
        byte[] crypto = crypt.ComputeHash(Encoding.UTF8.GetBytes(password));
        foreach (byte theByte in crypto)
        {
    
    
            hash.Append(theByte.ToString("x2"));
        }
        return hash.ToString();
    }

    // 注册逻辑
    public void OnRegister()
    {
    
    
        // 从输入框获取用户名和密码
        string username = usernameInputField.text;
        //使用哈希进行加密
        string password = HashPassword(passwordInputfield.text);

        if (username == "" || password == "")
        {
    
    
            registerMessage.text = "账号或密码不能为空";
        }

        else
        {
    
    
            int code = user.Register(username, password);
            if (code == 1)
            {
    
    
                Debug.Log("注册成功");
                registerMessage.text = "注册成功";
            }
            else if(code == 2)
            {
    
    
                Debug.Log("用户名已存在,请选择不同的用户名!");
                registerMessage.text = "用户名已存在,请选择不同的用户名!";
            }
            else
            {
    
    
                Debug.Log("注册失败");
                registerMessage.text = "注册失败";
            }
        }
        //清空输入框
        usernameInputField.text = "";
        passwordInputfield.text = "";
    }

    // 登录逻辑
    public void OnLogin()
    {
    
    
        // 从输入框获取用户名和密码
        string username = usernameInputField.text;
        //使用哈希进行加密
        string password = HashPassword(passwordInputfield.text);

        if (username == "" || password == "")
        {
    
    
            loginMessage.text = "账号或密码不能为空";
        }
        else
        {
    
    
            int code = user.Login(username, password);
            if (code == 1)
            {
    
    
                Debug.Log("登录成功");
                loginMessage.text = "登录成功";
            }
            else if(code == 2)
            {
    
    
                Debug.Log("登录失败:密码错误");
                loginMessage.text = "登录失败:密码错误";
            }
            else if(code == 3)
            {
    
    
                Debug.Log("登录失败:用户名不存在");
                loginMessage.text = "登录失败:用户名不存在";
            }
            else
            {
    
    
                Debug.Log("登录失败");
                loginMessage.text = "登录失败";
            }
        }
        //清空输入框
        usernameInputField.text = "";
        passwordInputfield.text = "";
    }
}

Create a new empty object UsersManager in Unity, mount the UsersManager script on the object, and drag the corresponding variable:
insert image description here
delete or hide the previous DataManager object, and modify the event binding on the registration and login buttons to The corresponding registration and login methods in UserManager are sufficient.
insert image description here
insert image description here
Next, you can test run, the result is the same as before.

Encapsulation makes the logical structure of the entire program clearer.

Guess you like

Origin blog.csdn.net/qq_41084756/article/details/130363620