【U3D】Unity引擎链接mySQL数据库
2016-04-05 01:04
281 查看
转自 本人此处只是做一个收集,方便自己阅读整理,不作为商业用途,侵删
版权所有
using UnityEngine;
using System;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;
public class CMySql : MonoBehaviour {
// Globalvariables
public staticMySqlConnectiondbConnection;//Just like MyConn.conn in StoryToolsbefore
staticstringhost = "192.168.1.100";
staticstringid = "mysql";
staticstringpwd = "123456";
staticstringdatabase = "test";
staticstringresult = "";
private string strCommand = "Select * from unity3d_test ORDERBYid;";
public static DataSet MyObj;
voidOnGUI()
{
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"))
{
stringconnectionString =string.Format("Server = {0}; Database = {1}; UserID = {2};Password = {3};",host,database,id,pwd);
openSqlConnection(connectionString);
MyObj=GetDataSet(strCommand);
}
GUILayout.Label(result);
}
// Onquit
public staticvoidOnApplicationQuit() {
closeSqlConnection();
}
// Connect todatabase
private staticvoidopenSqlConnection(string connectionString) {
dbConnection=new MySqlConnection(connectionString);
dbConnection.Open();
result=dbConnection.ServerVersion;
//Debug.Log("Connectedtodatabase."+result);
}
// Disconnectfromdatabase
private staticvoidcloseSqlConnection() {
dbConnection.Close();
dbConnection=null;
//Debug.Log("Disconnectedfromdatabase."+result);
}
// MySQLQuery
public staticvoiddoQuery(string sqlQuery) {
IDbCommanddbCommand=dbConnection.CreateCommand();
dbCommand.CommandText=sqlQuery;
IDataReaderreader= dbCommand.ExecuteReader();
reader.Close();
reader=null;
dbCommand.Dispose();
dbCommand=null;
}
#region GetDataSet
public DataSetGetDataSet(stringsqlString)
{
//stringsql= UnicodeAndANSI.UnicodeAndANSI.UnicodeToUtf8(sqlString);
DataSet ds =newDataSet();
try
{
MySqlDataAdapterda = newMySqlDataAdapter(sqlString, dbConnection);
da.Fill(ds);
}
catch(Exceptionee)
{
throw newException("SQL:" + sqlString +"\n" + ee.Message.ToString());
}
returnds;
}
#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 initialization
void Start () {
}
void OnGUI()
{
GUI.skin =myGUISkin;
if(GUI.Button(newRect(100,320,100,100),"Click Me"))
{
foreach(DataRowdr inCMySql.MyObj.Tables[0].Rows)
{
if(Index.ToString() ==dr["ID"].ToString())
{
strID=dr["ID"].ToString();
strName= dr["Name"].ToString();
strSex=dr["Sex"].ToString();
break;
}
}
Index++;
if(Index>5)
{
Index =1;
}
}
GUI.Label(newRect(320,100,150,70),"DataBaseTest");
GUI.Label(newRect(300,210,150,70),strID);
GUI.Label(newRect(300,320,150,70),strName);
GUI.Label(newRect(300,430,150,70),strSex);
}
}
2.導入dll
同先前的帖子 ,將MySql.data.dllImport至Assets底下 ,然後再到Unity\Editor\Data\Frameworks\Mono.framework中
將System.Data.dll 也一起Import至Assets內 , 當然 , 如果想顯示中文的話 , 請參考中文視頻教學,建立一個GUISkin與字型
3.建立數據庫內容
主要是因為代碼中的這段內容
staticstringhost = "192.168.1.100";
staticstringid = "mysql";
staticstringpwd = "123456";
staticstringdatabase = "test";
privatestringstrCommand = "Select * from unity3d_test ORDER BYid;";
其中host ,id , pwd 請自行設定 , 簡單的說就是連進你的MySQL啦~
然後建立一個名為test的Database , 在這個test下建立一張table , 取名為 unity3d_test,
接下來就為這張unity3d_test建立3個欄位 : ID , Name , Sex (記得將ID設定為primarykey且默認值為1)
再來自行填入5筆資料(5筆資料的原因是腳本那邊是設定成5筆資料一個循環 , 使用者可以自行更改腳本試試)
4.建立GameObject
建立完GameObject後將上面兩個腳本掛上去,如果有建立GUISkin , 記得指定GUISkin
5.執行
執行後先按Test按鈕來連接數據庫, 然後再按"ClickMe"來顯示數據庫內的內容
6.總結
以上部份是有關連線至數據庫後,再將資料用DataSet的方式獲得出來再加以顯示至介面上 , 歡迎各位高手能夠多多批評指教
永生猫的博客
http://blog.sina.com.cn/turnbackthetime版权所有
using UnityEngine;
using System;
using System.Collections;
using System.Data;
using MySql.Data.MySqlClient;
public class CMySql : MonoBehaviour {
// Globalvariables
public staticMySqlConnectiondbConnection;//Just like MyConn.conn in StoryToolsbefore
staticstringhost = "192.168.1.100";
staticstringid = "mysql";
staticstringpwd = "123456";
staticstringdatabase = "test";
staticstringresult = "";
private string strCommand = "Select * from unity3d_test ORDERBYid;";
public static DataSet MyObj;
voidOnGUI()
{
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"))
{
stringconnectionString =string.Format("Server = {0}; Database = {1}; UserID = {2};Password = {3};",host,database,id,pwd);
openSqlConnection(connectionString);
MyObj=GetDataSet(strCommand);
}
GUILayout.Label(result);
}
// Onquit
public staticvoidOnApplicationQuit() {
closeSqlConnection();
}
// Connect todatabase
private staticvoidopenSqlConnection(string connectionString) {
dbConnection=new MySqlConnection(connectionString);
dbConnection.Open();
result=dbConnection.ServerVersion;
//Debug.Log("Connectedtodatabase."+result);
}
// Disconnectfromdatabase
private staticvoidcloseSqlConnection() {
dbConnection.Close();
dbConnection=null;
//Debug.Log("Disconnectedfromdatabase."+result);
}
// MySQLQuery
public staticvoiddoQuery(string sqlQuery) {
IDbCommanddbCommand=dbConnection.CreateCommand();
dbCommand.CommandText=sqlQuery;
IDataReaderreader= dbCommand.ExecuteReader();
reader.Close();
reader=null;
dbCommand.Dispose();
dbCommand=null;
}
#region GetDataSet
public DataSetGetDataSet(stringsqlString)
{
//stringsql= UnicodeAndANSI.UnicodeAndANSI.UnicodeToUtf8(sqlString);
DataSet ds =newDataSet();
try
{
MySqlDataAdapterda = newMySqlDataAdapter(sqlString, dbConnection);
da.Fill(ds);
}
catch(Exceptionee)
{
throw newException("SQL:" + sqlString +"\n" + ee.Message.ToString());
}
returnds;
}
#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 initialization
void Start () {
}
void OnGUI()
{
GUI.skin =myGUISkin;
if(GUI.Button(newRect(100,320,100,100),"Click Me"))
{
foreach(DataRowdr inCMySql.MyObj.Tables[0].Rows)
{
if(Index.ToString() ==dr["ID"].ToString())
{
strID=dr["ID"].ToString();
strName= dr["Name"].ToString();
strSex=dr["Sex"].ToString();
break;
}
}
Index++;
if(Index>5)
{
Index =1;
}
}
GUI.Label(newRect(320,100,150,70),"DataBaseTest");
GUI.Label(newRect(300,210,150,70),strID);
GUI.Label(newRect(300,320,150,70),strName);
GUI.Label(newRect(300,430,150,70),strSex);
}
}
2.導入dll
同先前的帖子 ,將MySql.data.dllImport至Assets底下 ,然後再到Unity\Editor\Data\Frameworks\Mono.framework中
將System.Data.dll 也一起Import至Assets內 , 當然 , 如果想顯示中文的話 , 請參考中文視頻教學,建立一個GUISkin與字型
3.建立數據庫內容
主要是因為代碼中的這段內容
staticstringhost = "192.168.1.100";
staticstringid = "mysql";
staticstringpwd = "123456";
staticstringdatabase = "test";
privatestringstrCommand = "Select * from unity3d_test ORDER BYid;";
其中host ,id , pwd 請自行設定 , 簡單的說就是連進你的MySQL啦~
然後建立一個名為test的Database , 在這個test下建立一張table , 取名為 unity3d_test,
接下來就為這張unity3d_test建立3個欄位 : ID , Name , Sex (記得將ID設定為primarykey且默認值為1)
再來自行填入5筆資料(5筆資料的原因是腳本那邊是設定成5筆資料一個循環 , 使用者可以自行更改腳本試試)
4.建立GameObject
建立完GameObject後將上面兩個腳本掛上去,如果有建立GUISkin , 記得指定GUISkin
5.執行
執行後先按Test按鈕來連接數據庫, 然後再按"ClickMe"來顯示數據庫內的內容
6.總結
以上部份是有關連線至數據庫後,再將資料用DataSet的方式獲得出來再加以顯示至介面上 , 歡迎各位高手能夠多多批評指教
相关文章推荐
- iTween基础之Color(变换颜色)
- [Unity游戏开发]向量在游戏开发中的应用(二)
- unity official Networking tutorial总结
- ARToolKit for Unity on Android
- 【Unity&C#】写了一个事件分发器
- unity 3d 导出移动端设置
- unity3D
- [Unity热更新]lua基础(五)
- 关于Untiy中数值传递的问题
- Unity3d push调查
- Unity VR游戏开发干货教程:VR中的交互方式
- unityShader笔记 1 之 边缘色
- Unity5的关卡切换
- Unity5的关卡切换
- TDD在Unity3D游戏项目开发中的实践
- Unity 屏幕震动效果实现
- [Unity] Shader - CG语言 和 HLSL语言
- [Unity] Shader(着色器)输入输出和语义
- Unity3d-Shader-UV移动
- Unity发布IOS在Xcode7.3报错的问题