1、unity3d 中连接数据库.txt38 当乌云布满天空时,悲观的人看到的是“黑云压城城欲摧” ,乐观的人看到的是“甲光向日金鳞开” 。无论处在什么厄运中,只要保持乐观的心态,总能找到这样奇特的草莓。/*在 unity3d 中连接数据库 收藏1.C#代码:*/using UnityEngine;using System;using System.Collections;using System.Data;using MySql.Data.MySqlClient;public class CMySql : MonoBehaviour / Global variablespublic static
2、 MySqlConnection dbConnection;/Just like MyConn.conn in StoryTools beforestatic string host = “192.168.1.100“;static string id = “mysql“;/这里是你自己的数据库的用户名字,我一开始想用root,发现不行,后来添加了新的用户才可以static string pwd = “123456“;static string database = “test“;static string result = “;private string strCommand = “Sel
3、ect * from unity3d_test ORDER BY id;“;public static DataSet MyObj;void OnGUI()host = GUILayout.TextField( host, 200, GUILayout.Width(200);id = GUILayout.TextField( id, 200, GUILayout.Width(200);pwd = GUILayout.TextField( pwd, 200, GUILayout.Width(200);if(GUILayout.Button(“Test“)string connectionStri
4、ng = string.Format(“Server = 0; Database = 1; User ID = 2; Password = 3;“,host,database,id,pwd);openSqlConnection(connectionString);MyObj = GetDataSet(strCommand); GUILayout.Label(result); / On quitpublic static void OnApplicationQuit() closeSqlConnection();/ Connect to databaseprivate static void o
5、penSqlConnection(string connectionString) dbConnection = new MySqlConnection(connectionString);dbConnection.Open();result = dbConnection.ServerVersion;/Debug.Log(“Connected to database.“+result);/ Disconnect from databaseprivate static void closeSqlConnection() dbConnection.Close();dbConnection = nu
6、ll;/Debug.Log(“Disconnected from database.“+result);/ MySQL Querypublic static void doQuery(string sqlQuery) IDbCommand dbCommand = dbConnection.CreateCommand(); dbCommand.CommandText = sqlQuery;IDataReader reader = dbCommand.ExecuteReader();reader.Close();reader = null;dbCommand.Dispose();dbCommand
7、 = null;#region Get DataSetpublic DataSet GetDataSet(string sqlString)/string sql = UnicodeAndANSI.UnicodeAndANSI.UnicodeToUtf8(sqlString);DataSet ds = new DataSet();tryMySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);da.Fill(ds);catch (Exception ee)throw new Exception(“SQL:“ + sq
8、lString + “n“ + ee.Message.ToString();return ds;#endregion using UnityEngine;using System;using System.Collections;using System.Data;public class DataBaseTest : MonoBehaviour public GUISkin myGUISkin = new GUISkin();string strID = “;string strName = “;string strSex = “;int Index = 1;/ Use this for i
9、nitializationvoid Start () void OnGUI()GUI.skin = myGUISkin;if (GUI.Button(new Rect(100,320,100,100),“Click Me“)foreach(DataRow dr in CMySql.MyObj.Tables0.Rows)if (Index.ToString() = dr“ID“.ToString()strID = dr“ID“.ToString();strName = dr“Name“.ToString();strSex = dr“Sex“.ToString();break; Index+;if
10、(Index 5)Index = 1; GUI.Label(new Rect(320,100,150,70),“DataBaseTest“);GUI.Label(new Rect(300,210,150,70),strID);GUI.Label(new Rect(300,320,150,70),strName);GUI.Label(new Rect(300,430,150,70),strSex);2.導入 dll同先前的帖子 , 將 MySql.data.dll Import 至 Assets 底下 , 然後再到UnityEditorDataFrameworksMono.framework 中
11、將 System.Data.dll 也一起 Import 至 Assets 內 , 當然 , 如果想顯示中文的話 , 請參考中文視頻教學 , 建立一個 GUISkin 與字型3.建立數據庫內容主要是因為代碼中的這段內容static string host = “192.168.1.100“;static string id = “mysql“;static string pwd = “123456“;static string database = “test“;private string strCommand = “Select * from unity3d_test ORDER BY i
12、d;“;其中 host ,id , pwd 請自行設定 , 簡單的說就是連進你的 MySQL 啦然後建立一個名為 test 的 Database , 在這個 test 下建立一張 table , 取名為 unity3d_test ,接下來就為這張 unity3d_test 建立 3 個欄位 : ID , Name , Sex (記得將 ID 設定為primary key 且默認值為 1)再來自行填入 5 筆資料(5 筆資料的原因是腳本那邊是設定成 5 筆資料一個循環 , 使用者可以自行更改腳本試試)4.建立 GameObject建立完 GameObject 後將上面兩個腳本掛上去 , 如果有建立 GUISkin , 記得指定 GUISkin5.執行執行後先按 Test 按鈕來連接數據庫 , 然後再按“Click Me“來顯示數據庫內的內容