Unity uGUI 登录及注册功能
2014-12-04 21:17 wuzhang 阅读(16159) 评论(4) 编辑 收藏 举报上次我们已经完成了登录界面的模拟功能,今天咱们把上次没做完的继续完善下!那么废话少说直接开始吧!
PS:本次完善的功能有:
1,增加对数据库的操作。
2,使用了MD5Key值加密
3,完善登录和组测功能
4,环境Win8.1+Unity4.6+mysql
1,咱们就接着上次的工程继续吧,所以就不再新建工程了。
2,引入对数据库的操作,因此我们封装了一个SqlAccess类。
SqlAccess.cs
/****************************** * 对数据库操作的封装 * @Author wuzhang * 2014/12/4 *****************************/ using UnityEngine; using System; using System.Data; using System.Collections; using MySql.Data.MySqlClient; using MySql.Data; using System.IO; public class SqlAccess { public static MySqlConnection dbConnection; //如果只是在本地的话,写localhost就可以。 // static string host = "localhost"; //如果是局域网,那么写上本机的局域网IP static string host = "127.0.0.1"; static string id = "root"; static string pwd = ""; static string database = "login"; public SqlAccess() { OpenSql(); } public static void OpenSql() { try { string connectionString = string.Format("Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};",host,database,id,pwd,"3306"); dbConnection = new MySqlConnection(connectionString); dbConnection.Open(); } catch (Exception e) { throw new Exception("服务器连接失败,请重新检查是否打开MySql服务。" + e.Message.ToString()); } } public DataSet CreateTable (string name, string[] col, string[] colType) { if (col.Length != colType.Length) { throw new Exception ("columns.Length != colType.Length"); } string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0]; for (int i = 1; i < col.Length; ++i) { query += ", " + col[i] + " " + colType[i]; } query += ")"; return ExecuteQuery(query); } public DataSet CreateTableAutoID (string name, string[] col, string[] colType) { if (col.Length != colType.Length) { throw new Exception ("columns.Length != colType.Length"); } string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0] + " NOT NULL AUTO_INCREMENT"; for (int i = 1; i < col.Length; ++i) { query += ", " + col[i] + " " + colType[i]; } query += ", PRIMARY KEY ("+ col[0] +")" + ")"; Debug.Log(query); return ExecuteQuery(query); } //插入一条数据,包括所有,不适用自动累加ID。 public DataSet InsertInto (string tableName, string[] values) { string query = "INSERT INTO " + tableName + " VALUES (" + "'"+ values[0]+ "'"; for (int i = 1; i < values.Length; ++i) { query += ", " + "'"+values[i]+ "'"; } query += ")"; Debug.Log(query); return ExecuteQuery (query); } //插入部分ID public DataSet InsertInto (string tableName, string[] col,string[] values) { if (col.Length != values.Length) { throw new Exception ("columns.Length != colType.Length"); } string query = "INSERT INTO " + tableName + " (" + col[0]; for (int i = 1; i < col.Length; ++i) { query += ", "+col[i]; } query += ") VALUES (" + "'"+ values[0]+ "'"; for (int i = 1; i < values.Length; ++i) { query += ", " + "'"+values[i]+ "'"; } query += ")"; Debug.Log(query); return ExecuteQuery (query); } /// <summary> /// 返回表的查询结果 /// </summary> /// <returns>The all.</returns> /// <param name="Name">Name.</param> public DataSet SelectAll(string Name) { string query ="select * from "+" "+Name; return ExecuteQuery (query); } /// <summary> /// 条件查找 /// </summary> /// <returns>The where.</returns> /// <param name="tableName">Table name.</param> /// <param name="items">Items.</param> /// <param name="col">Col.</param> /// <param name="operation">Operation.</param> /// <param name="values">Values.</param> public DataSet SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values) { if (col.Length != operation.Length || operation.Length != values.Length) { throw new Exception ("col.Length != operation.Length != values.Length"); } string query = "SELECT " + items[0]; for (int i = 1; i < items.Length; ++i) { query += ", " + items[i]; } query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' "; for (int i = 1; i < col.Length; ++i) { query += " AND " + col[i] + operation[i] + "'" + values[0] + "' "; } return ExecuteQuery (query); } /// <summary> /// 更新表信息 /// </summary> /// <returns>The into.</returns> /// <param name="tableName">Table name.</param> /// <param name="cols">Cols.</param> /// <param name="colsvalues">Colsvalues.</param> /// <param name="selectkey">Selectkey.</param> /// <param name="selectvalue">Selectvalue.</param> public DataSet UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue) { string query = "UPDATE "+tableName+" SET "+cols[0]+" = "+colsvalues[0]; for (int i = 1; i < colsvalues.Length; ++i) { query += ", " +cols[i]+" ="+ colsvalues[i]; } query += " WHERE "+selectkey+" = "+selectvalue+" "; return ExecuteQuery (query); } /// <summary> /// 条件删除 /// </summary> /// <param name="tableName">Table name.</param> /// <param name="cols">Cols.</param> /// <param name="colsvalues">Colsvalues.</param> public DataSet Delete(string tableName,string []cols,string []colsvalues) { string query = "DELETE FROM "+tableName + " WHERE " +cols[0] +" = " + colsvalues[0]; for (int i = 1; i < colsvalues.Length; ++i) { query += " or " +cols[i]+" = "+ colsvalues[i]; } Debug.Log(query); return ExecuteQuery (query); } /// <summary> /// 关闭数据库 /// </summary> public void Close() { if(dbConnection != null) { dbConnection.Close(); dbConnection.Dispose(); dbConnection = null; } } public DataSet ExistOrNot(string sql) { if(dbConnection.State==ConnectionState.Open) { Debug.Log (sql); DataSet ds = new DataSet(); //表的集合 try { MySqlDataAdapter da = new MySqlDataAdapter(sql, dbConnection); da.Fill(ds); } catch (Exception ee) { throw new Exception("SQL:" + sql + "/n" + ee.Message.ToString()); } finally { } return ds; } return null; } /// <summary> /// 返回检索结果 /// </summary> /// <returns>The query.</returns> /// <param name="sqlString">Sql string.</param> public static DataSet ExecuteQuery(string sqlString) { if(dbConnection.State==ConnectionState.Open) { DataSet ds = new DataSet(); //表的集合 try { MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection); da.Fill(ds); } catch (Exception ee) { throw new Exception("SQL:" + sqlString + "/n" + ee.Message.ToString()); } finally { } return ds; } return null; } /// <summary> /// 读取数据集 /// </summary> /// <param name="ds">Ds.</param> public void ReadDs(DataSet ds) { if(ds!=null) { DataTable user = ds.Tables[0]; foreach(DataRow row in user.Rows) { foreach(DataColumn colum in user.Columns) { Debug.Log (row[colum]); } } } } }
3,对用户注册的密码采用加密存储,所以还有一个加密类。
Encryption.cs
//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.34014 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ using System; using System.Security.Cryptography; using System.Text; namespace AssemblyCSharp { public class Encryption { public Encryption () { } /// <summary> /// 内容加密 /// </summary> /// <param name="ContentInfo">要加密内容</param> /// <param name="strkey">key值</param> /// <returns></returns> public string encryptionContent(string ContentInfo,string strkey) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strkey); RijndaelManaged encryption = new RijndaelManaged(); encryption.Key = keyArray; encryption.Mode = CipherMode.ECB; encryption.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = encryption.CreateEncryptor(); byte[] _EncryptArray = UTF8Encoding.UTF8.GetBytes(ContentInfo); byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length); return System.Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// 内容解密 /// </summary> /// <param name="encryptionContent">被加密内容</param> /// <param name="strkey">key值</param> /// <returns></returns> public string decipheringContent(string encryptionContent,string strkey) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strkey); RijndaelManaged decipher = new RijndaelManaged(); decipher.Key = keyArray; decipher.Mode = CipherMode.ECB; decipher.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = decipher.CreateDecryptor(); byte[] _EncryptArray =System.Convert.FromBase64String(encryptionContent); byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } } }
3,准备工作基本已经完成,由于代码还需要对数据库的操作,所以我们还要建数据库。
3.1,打开mysql的服务。
3.2 登录root用户
由于我没有设置密码,所以下一步直接回车就OK了
OK 已经成功进入了。
使用Login数据库
目前状态标为空吧,那么我们就去登录试试吧!
为什么登陆失败呢?呵呵,没有为什么,还没注册。
那就去注册个用户吧。
好吧,原来真的要注册,信息还不能为空。
注册成功了吗?我们有两个方法可以知道。
1,最简单的就是直接登录。
哎,跳转了啊,居然登录成功了!
那注册信息成功了。
2,去数据库查看注册的信息吧!Come On,Go!
好吧,为什么呢?原来咱们使用了Md5 key值加密过了!神奇吧!
咱们再注册一个相同的信息
不好意思,用户已存在了!
再次查看数据库:
还是原来的信息,那个点击密码我还没做,因为没有找到好的思路,假如我们当输入用户信息时就去数据库匹配相同的感觉效率太差了。
假如我我们在Awake()时把数据库的所有密码全从数据库读出来开并解密再进行行匹配,原则上是可行的,希望大家提供好的建议!!!
OK,附上工程源码: http://pan.baidu.com/s/1hq1k6zi