1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 关于unity连接MySQL数据库做一个简单的登陆注册系统

关于unity连接MySQL数据库做一个简单的登陆注册系统

时间:2020-09-28 08:10:48

相关推荐

关于unity连接MySQL数据库做一个简单的登陆注册系统

这段时间为了大作业,作为一个菜鸟研究了好久的Unity连接数据库,那就整理一下然后写下来当作记录吧。

首先,unity连接MySQL要先在Assets板上创建pluging文件夹再导入一些必要的dll文件,大部分的资料都说要在unity的安装目录(Unity\Editor\Data\Mono\lib\mono\2.0)下找到以下5个ddl文件一起放进文件夹里。

还有对应的mysql.Data.dll,这个可以去MySql官网下载Connector安装,但是需要注意的是MySql.Data.dll支持的版本要小于或等于你的Unity支持的版本,否则会报错。

需要注意的是,在实际操作的过程中,如果我往pluging文件夹中导入System.data.dll时会报错重复,因为是从安装目录复制进去的。所以经实际操作验证,只需要放进MySQL.Data.dll就可以连接成功了,我猜是因为其余5个文件我这个版本的unity可以每个项目都自行调用。我用的unity是.2.6的版本。当然最好还是先全部放进去,然后看看报不报错,如果不报错那就没关系。

我的习惯是先铺设好UI界面再进行脚本的构写。这是我的登陆界面,后面的动画是用particle system挂在摄像机上面做的

然后开始构建我的数据库跟表,也可以在前面设置一个ID列,但是要注意的是必须要把ID列设置成自增,要不然注册的时候会报错因为没有值。我为了方便就只设了用户名跟密码。

好了接下来就可以开始写脚本了,参考了这个博主的脚本/qq_39646949/article/details/99334589

这是注册的脚本,可以自行学习以下mysql的基本语句和与c#的连接

using MySql.Data.MySqlClient;using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class register : MonoBehaviour{// Start is called before the first frame updatepublic InputField userNameInput;public InputField passwordInput;public GameObject sueccedUI;public GameObject failUI;void Start(){}public void OnClickedButton(){Register(new string[] { userNameInput.text, passwordInput.text });}private void Register(string[] strRegister){string connStr = "Database=cubestreet;datasource=localhost;port=3306;user=root;pwd=123456;";MySqlConnection conn = new MySqlConnection(connStr);conn.Open();Debug.Log("数据库连接成功");//先要查询一下目前数据库是否有重复的数据。MySqlCommand myCommand = new MySqlCommand("select*from user", conn);MySqlDataReader reader = myCommand.ExecuteReader();List<string> user = new List<string>();while (reader.Read()){string username = reader.GetString("name");string password = reader.GetString("password");user.Add(username);}//**避免账号重复。**foreach (var item in user){if (user.Contains(strRegister[0])){Debug.Log("账号已存在!");failUI.SetActive(true);//这个是我的提示界面break;}else{reader.Close();//**先将查询的功能关闭。**MySqlCommand cmd = new MySqlCommand("insert into user set name ='" + strRegister[0] + "'" + ",password='" + strRegister[1] + "'", conn);cmd.Parameters.AddWithValue("name", strRegister[0]);cmd.Parameters.AddWithValue("password", strRegister[1]);cmd.ExecuteNonQuery();Debug.Log("注册成功!");sueccedUI.SetActive(true);//也是提示界面break;}}}}

把这个脚本挂在按钮上测试一下,欸成功了!太棒了!好的那我们开始登陆脚本吧

using MySql.Data.MySqlClient;using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;using UnityEngine.UI;public class logtest : MonoBehaviour{public InputField userNameInput;public InputField passwordInput;public GameObject failUI;public GameObject loseUI;private AsyncOperation async;[HideInInspector] public static int count = 0;// Start is called before the first frame updatevoid Start(){}public void OnClickedButton(){Login(new string[] { userNameInput.text, passwordInput.text });}// Update is called once per frameprivate void Login(string[] str){Dictionary<string, string> myDic = new Dictionary<string, string>();myDic.Clear();string connStr = "Database=cubestreet;datasource=localhost;port=3306;user=root;pwd=123456;";MySqlConnection conn = new MySqlConnection(connStr);Debug.Log("数据库连接成功");conn.Open();#region 查询MySqlCommand cmd = new MySqlCommand("select * from user", conn);MySqlDataReader reader = cmd.ExecuteReader();while (reader.Read()){string username = reader.GetString("name");string password = reader.GetString("password");myDic.Add(username, password);}if (myDic.ContainsKey(str[0])){Debug.Log("账号存在!");string vale;if (myDic.TryGetValue(str[0], out vale)){if (vale == str[1]){Debug.Log("登录成功!");PlayerPrefs.SetString("username", userNameInput.text);Debug.Log(PlayerPrefs.GetString("username"));//判断是否跳转成功还有记录次数,不需要的可以不写下面直接用SceneManager.loadlevel来跳转async = SceneManager.LoadSceneAsync("LevelMenu");if (async.isDone){count++;}PlayerPrefs.SetInt("menuload", count);Debug.Log(PlayerPrefs.GetInt("menuload"));}else{Debug.Log("密码错误,请重新输入!");failUI.SetActive(true);}}}else{Debug.Log("账号不存在!");loseUI.SetActive(true);}#endregionreader.Close();//关闭读取conn.Close();//关闭与数据库的连接}}

挂在注册按钮上试一下,又成功了!太开心了吧

这样就完成了一个很简单的登陆注册功能,其实可以用另外的类来封装MySQL的语句,需要的时候直接调用也行。

用了之后才发现用mySql来做这个其实并不好,游戏如果发给别人,要是其他人没有mysql或者他的连接密码不一样,那这个功能也是无法实现的。

如果我的记录还能帮到别人那就太棒啦

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。