<>仅个人记录,方便以后忘掉回来看看
服务器买的是腾讯云的,系统是 CentOS。
<>管理工具用的是万胜云面板(好像也叫宝塔),数据库都是在这里面整的,用到的表提前在里面创建好了
using MySql.Data.MySqlClient; using System; using System.Collections; using
System.Collections.Generic; using UnityEngine; public class MySqlManager :
Singleton<MySqlManager> { protected static string m_databaseIP =
"111.50.100.200"; //IP地址 protected static string m_databasePort = "3306"; //端口号
protected static string m_userID = "SrarSharkDataTest"; //MySQL数据库用户名 protected
static string m_password = "StarShark1234"; //MySQL登陆密码 protected static string
m_databaseName= "srarsharkdataall"; //链接的数据库的库名 protected static string
m_connectionString; // 数据库连接字符串 public static MySqlConnection connection; ///
<summary> /// 测试是否链接上数据库 /// </summary> /// <returns></returns> public bool
TestConnection() { bool isConnected = true; //发送数据库连接字段 创建连接通道 using (
MySqlConnection connection= new MySqlConnection(m_connectionString)) { try {
//打开连接通道 connection.Open(); Debug.Log("打开了数据库"); } catch (MySqlException E) {
//如果有异常 则连接失败 isConnected = false; throw new Exception(E.Message); } finally {
//关闭连接通道 connection.Close(); Debug.Log("关闭数据库"); } } return isConnected; } ///
<summary> /// 初始化数据库,利用字符串组拼方式来编写数据库的连接 /// </summary> public static void Init()
{ // m_connectionString = string.Format("Server = {0};port={4};Database = {1};
User ID = {2}; Password = {3};", m_databaseIP, m_databaseName, m_userID,
m_password, "3306"); m_connectionString = string.Format("Server = {0}; port =
{1}; Database = {2}; User ID = {3}; Password = {4}; Pooling=true; Charset =
utf8;", m_databaseIP, m_databasePort, m_databaseName, m_userID, m_password);
//发送数据库连接字段 创建连接通道 using (connection = new MySqlConnection(m_connectionString))
{ try { //打开连接通道 connection.Open(); Debug.Log("打开了数据库"); } catch (MySqlException
E) { //如果有异常 则连接失败 Debug.Log("连接失败"); throw new Exception(E.Message); } finally
{ //关闭连接通道 connection.Close(); Debug.Log("关闭数据库"); } } Debug.Log(
m_connectionString); } //登录验证 public void Click_login(string username, string
password) { //
MySqlManager.Instance.QuerySpacificDataFromTable("StarSharkData",
"UserPassword", "1"); try { connection.Open(); Debug.Log("------输入的用户名---" +
username+ "-密码-" + password); string sqlQuary = "select * from StarSharkData
where UserName ='" + username + "' and UserPassword = '" + password + "'";
MySqlCommand comd= new MySqlCommand(sqlQuary, connection); MySqlDataReader
reader= comd.ExecuteReader(); if (reader.Read()) { Debug.Log(
"------用户存在,登录成功!------"); //进行登入成功后的操作,例如进入新场景。。。 } else { Debug.Log(
"------用户不存在,请注册。或请检查用户名或和密码!------"); } } catch (System.Exception e) { Debug.
Log(e.Message); } finally { connection.Close(); } } //注册验证 public void
Click_register(string username, string password) { if (!(username.Length > 0 &&
password.Length > 0)) { Debug.Log("-----用户名和密码必须>0------"); return; } try {
connection.Open(); Debug.Log("-----连接成功!------"); string sqlQuary = "select *
from StarSharkData where UserName =@paral1 and UserPassword = @paral2";
MySqlCommand comd= new MySqlCommand(sqlQuary, connection); comd.Parameters.
AddWithValue("paral1", username); comd.Parameters.AddWithValue("paral2",
password); //comd.Parameters.AddWithValue("paral0", 0);id=@para10 and
MySqlDataReader reader= comd.ExecuteReader(); if (reader.Read()) { Debug.Log(
"-----用户名已存在,请重新输!------"); connection.Close(); } else { connection.Close();
Insert_User(username, password); } } catch (System.Exception e) { Debug.Log(e.
Message); } finally { connection.Close(); } } }
<>数据库管理脚本,提供的连接服务器,以及登录、注册功能
using System.Collections; using System.Collections.Generic; using UnityEngine;
using UnityEngine.UI; public class MySqlTest : MonoBehaviour { public Button
SignIn;//登录按钮 public Button Register;//注册按钮 public Button Activation; //测试激活按钮
public InputField InputUser;//用户名输入框 public InputField InputPassword;//密码输入框 //
Start is called before the first frame update void Start() { //初始化 MySqlManager.
Init(); //登陆,点击事件 SignIn.onClick.AddListener(()=> { MySqlManager.Instance.
Click_login(InputUser.text, InputPassword.text); }); //注册,点击事件 Register.onClick.
AddListener(()=> { MySqlManager.Instance.Click_register(InputUser.text,
InputPassword.text); }); }
<>测试登录和注册的用户脚本
运行后的结果