最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

Unity3D與Sqlite數(shù)據(jù)庫(kù)直連

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-09 07:31:45
文檔

Unity3D與Sqlite數(shù)據(jù)庫(kù)直連

Unity3D與Sqlite數(shù)據(jù)庫(kù)直連:網(wǎng)址:http://bbs.9ria.com/forum.phpmod=viewthreadtid=168629fromuid=308561 這幾天也順便研究了下Unity3D 如何與 Sqlite 數(shù)據(jù)庫(kù)進(jìn)行直連的問(wèn)題。不廢話了,直接介紹吧。原文來(lái)自于: http://wiki.unity3d.com/index.ph
推薦度:
導(dǎo)讀Unity3D與Sqlite數(shù)據(jù)庫(kù)直連:網(wǎng)址:http://bbs.9ria.com/forum.phpmod=viewthreadtid=168629fromuid=308561 這幾天也順便研究了下Unity3D 如何與 Sqlite 數(shù)據(jù)庫(kù)進(jìn)行直連的問(wèn)題。不廢話了,直接介紹吧。原文來(lái)自于: http://wiki.unity3d.com/index.ph

網(wǎng)址:http://bbs.9ria.com/forum.php?mod=viewthreadtid=168629fromuid=308561 這幾天也順便研究了下Unity3D 如何與 Sqlite 數(shù)據(jù)庫(kù)進(jìn)行直連的問(wèn)題。不廢話了,直接介紹吧。原文來(lái)自于: http://wiki.unity3d.com/index.php/Sqlite ,大家可以去學(xué)習(xí)下。 1、

網(wǎng)址:http://bbs.9ria.com/forum.php?mod=viewthread&tid=168629&fromuid=308561

這幾天也順便研究了下Unity3D如何與Sqlite數(shù)據(jù)庫(kù)進(jìn)行直連的問(wèn)題。不廢話了,直接介紹吧。原文來(lái)自于:http://wiki.unity3d.com/index.php/Sqlite,大家可以去學(xué)習(xí)下。

1、環(huán)境介紹:

Windows7,Unity3D,SQLite Expert Personal 3

2、開發(fā)語(yǔ)言:

JavaScript

3、需要的dll文件:

Mono.Data.Sqlite.dll和sqlite3.dll,稍后我會(huì)將所有文件打包在一起供大家討論下,先看下這些dll文件應(yīng)該被放在哪里,看下面的截圖:

,一定要在這個(gè)目錄下,請(qǐng)跟我保持一致。

4、如果需要將編譯好的程序發(fā)布成功的話,需要改一些地方,具體見(jiàn)下面的截圖:

要改動(dòng)的地方我已經(jīng)用紅色標(biāo)記出來(lái)了,注意這個(gè)要改成.NET2.0,這樣才能夠發(fā)布的。系統(tǒng)默認(rèn)的不是.NET2.0,大家這一點(diǎn)要注意?。?!

5、下面來(lái)看下代碼吧,先看下如何創(chuàng)建數(shù)據(jù)庫(kù)的代碼,這一篇代碼是不用掛到任何對(duì)象上面去的,你只用把它當(dāng)成一個(gè)工具即可。如下所示:

/* Javascript class for accessing SQLite objects.

To use it, you need to make sure you COPY Mono.Data.SQLiteClient.dll from wherever it lives in your Unity directory

to your project's Assets folder

Originally created by dklompmaker in 2009

http://forum.unity3d.com/threads ... sier-Database-Stuff

Modified 2011 by Alan Chatham */

//#pragma strict

/*代碼描述

*本代碼是為了在Windows環(huán)境下運(yùn)行unity3d和Sqlite數(shù)據(jù)庫(kù)而寫的;實(shí)現(xiàn)的基本功能是unity3d能夠與數(shù)據(jù)庫(kù)之間進(jìn)行基本的通信,比如說(shuō)

在數(shù)據(jù)庫(kù)中的數(shù)據(jù)被改變了以后,unity3d中得到的數(shù)據(jù)也會(huì)在刷新了之后跟著改變;這只是一個(gè)基本的核心的技術(shù),為的是能夠應(yīng)用在大型的unity3d

項(xiàng)目中,能夠存儲(chǔ)場(chǎng)景中的項(xiàng)目的屬性,在需要改變對(duì)象的屬性或增加、減少等對(duì)象時(shí)能夠很方便的用得上。

要實(shí)現(xiàn)本代碼。首先需要一些dll文件,一個(gè)是Mono.Data.SQLiteClient.dll,另外一個(gè)是sqlite3.dll,這些文件都能夠在unity3d的安裝目錄中找得到。

除此之外,還需要把這兩個(gè)文件放在你的項(xiàng)目的這個(gè)路徑下面:\Assets\Plugins\,沒(méi)有Plugins文件夾就必須創(chuàng)建這個(gè)文件夾,然后將這兩個(gè)dll文件放在該文件夾寫。

當(dāng)然,如果你想能夠在PC上面發(fā)布成可執(zhí)行文件,還需要改動(dòng)一些地方。在unity3d中的Play Setting ->Other Setting 中將Api Compatibility的等級(jí)改為

.NET 2.0;那么這些操作做完了以后,如果你的代碼寫得沒(méi)有問(wèn)題,那么你就可以成功了。

好了,下面咱們來(lái)詳細(xì)解釋下代碼吧。

  1. *
  2. */
  3. import System.Data; // we import our data class 我們先導(dǎo)入我們的數(shù)據(jù)集
  4. import Mono.Data.Sqlite; // we import sqlite 我們導(dǎo)入sqlite數(shù)據(jù)集,也就是Plugins文件夾下的那個(gè)dll文件

  5. class dbAccess {
  6. // variables for basic query access
  7. private var connection : String; //數(shù)據(jù)庫(kù)的連接字符串,用于建立與特定數(shù)據(jù)源的連接
  8. private var dbcon : IDbConnection; //IDbConnection的連接對(duì)象,其實(shí)就是一個(gè)類對(duì)象
  9. private var dbcmd : IDbCommand; //IDbCommand類對(duì)象,用來(lái)實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的命令:注解:我在網(wǎng)上資料看到的如何實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)執(zhí)行命令:
  10. //首先創(chuàng)建一個(gè)IDbConnection連接對(duì)象,然后將一條數(shù)據(jù)庫(kù)命令賦值給一個(gè)字符串,利用這個(gè)字符串和連接對(duì)象
  11. //就可以創(chuàng)建(new)一個(gè)IDbCommand對(duì)象了,然后使用提供的方法就可以執(zhí)行這個(gè)命令了。
  12. private var reader : IDataReader; //reader的作用就是讀取結(jié)果集的一個(gè)或多個(gè)只進(jìn)結(jié)果流

  13. function OpenDB(p : String){
  14. connection = "URI=file:" + p; // we set the connection to our database
  15. dbcon = new SqliteConnection(connection);
  16. dbcon.Open(); //打開數(shù)據(jù)庫(kù)連接操作
  17. }

  18. function BasicQuery(q : String, r : boolean){ // run a baic Sqlite query
  19. dbcmd = dbcon.CreateCommand(); // create empty command
  20. dbcmd.CommandText = q; // fill the command
  21. reader = dbcmd.ExecuteReader(); // execute command which returns a reader 返回IDataReader的對(duì)象,創(chuàng)建IDataReader的對(duì)象
  22. if(r){ // if we want to return the reader
  23. return reader; // return the reader 返回讀取的對(duì)象,就是讀到了什么東西
  24. }
  25. }

  26. // This returns a 2 dimensional ArrayList with all the
  27. // data from the table requested
  28. function ReadFullTable(tableName : String){
  29. var query : String;
  30. query = "SELECT * FROM " + tableName;
  31. dbcmd = dbcon.CreateCommand();
  32. dbcmd.CommandText = query;
  33. reader = dbcmd.ExecuteReader();
  34. var readArray = new ArrayList();
  35. while(reader.Read()){
  36. var lineArray = new ArrayList();
  37. for (var i = 0; i < reader.FieldCount; i++)
  38. lineArray.Add(reader.GetValue(i)); // This reads the entries in a row
  39. readArray.Add(lineArray); // This makes an array of all the rows
  40. }
  41. return readArray; // return matches
  42. }

  43. // This function deletes all the data in the given table. Forever. WATCH OUT! Use sparingly, if at all
  44. function DeleteTableContents(tableName : String){
  45. var query : String;
  46. query = "DELETE FROM " + tableName;
  47. dbcmd = dbcon.CreateCommand();
  48. dbcmd.CommandText = query;
  49. reader = dbcmd.ExecuteReader();
  50. }

  51. function CreateTable(name : String, col : Array, colType : Array){ // Create a table, name, column array, column type array
  52. var query : String;
  53. query = "CREATE TABLE " + name + "(" + col[0] + " " + colType[0];
  54. for(var i=1; i
  55. query += ", " + col + " " + colType;
  56. }
  57. query += ")";
  58. dbcmd = dbcon.CreateCommand(); // create empty command
  59. dbcmd.CommandText = query; // fill the command
  60. reader = dbcmd.ExecuteReader(); // execute command which returns a reader
  61. }
  62. function InsertIntoSingle(tableName : String, colName : String, value : String){ // single insert
  63. var query : String;
  64. query = "INSERT INTO " + tableName + "(" + colName + ") " + "VALUES (" + value + ")";
  65. dbcmd = dbcon.CreateCommand(); // create empty command
  66. dbcmd.CommandText = query; // fill the command
  67. reader = dbcmd.ExecuteReader(); // execute command which returns a reader
  68. }
  69. function InsertIntoSpecific(tableName : String, col : Array, values : Array){ // Specific insert with col and values
  70. var query : String;
  71. query = "INSERT INTO " + tableName + "(" + col[0];
  72. for(var i=1; i
  73. query += ", " + col;
  74. }
  75. query += ") VALUES (" + values[0];
  76. for(i=1; i
  77. query += ", " + values;
  78. }
  79. query += ")";
  80. dbcmd = dbcon.CreateCommand();
  81. dbcmd.CommandText = query;
  82. reader = dbcmd.ExecuteReader();
  83. }

  84. function InsertInto(tableName : String, values : Array){ // basic Insert with just values
  85. var query : String;
  86. query = "INSERT INTO " + tableName + " VALUES (" + values[0];
  87. for(var i=1; i
  88. query += ", " + values;
  89. }
  90. query += ")";
  91. dbcmd = dbcon.CreateCommand();
  92. dbcmd.CommandText = query;
  93. reader = dbcmd.ExecuteReader();
  94. }

  95. // This function reads a single column
  96. // wCol is the WHERE column, wPar is the operator you want to use to compare with,
  97. // and wValue is the value you want to compare against.
  98. // Ex. - SingleSelectWhere("puppies", "breed", "earType", "=", "floppy")
  99. // returns an array of matches from the command: SELECT breed FROM puppies WHERE earType = floppy;
  100. function SingleSelectWhere(tableName : String, itemToSelect : String, wCol : String, wPar : String, wValue : String){ // Selects a single Item
  101. var query : String;
  102. query = "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue;
  103. dbcmd = dbcon.CreateCommand();
  104. dbcmd.CommandText = query;
  105. reader = dbcmd.ExecuteReader();
  106. var readArray = new Array();
  107. while(reader.Read()){
  108. readArray.Push(reader.GetString(0)); // Fill array with all matches
  109. }
  110. return readArray; // return matches
  111. }


  112. function CloseDB(){
  113. reader.Close(); // clean everything up
  114. reader = null;
  115. dbcmd.Dispose();
  116. dbcmd = null;
  117. dbcon.Close();
  118. dbcon = null;
  119. }

  120. }


上述代碼的基本的增刪改查什么的都具有了,仔細(xì)看看然后加上我的注釋,這段代碼也不難,就是可能初學(xué)的話有點(diǎn)難以接受,說(shuō)實(shí)話我也是初學(xué),如果沒(méi)有原文,我是絕對(duì)寫不出來(lái)這個(gè)的。。

7、好了,下面我們?cè)賮?lái)看看如何在Unity3D中使用這個(gè)數(shù)據(jù)庫(kù)的代碼吧:

  1. //#pragma strict
  2. /* Script for testing out SQLite in Javascript
  3. 2011 - Alan Chatham
  4. Released into the public domain

  5. This script is a GUI script - attach it to your main camera.
  6. It creates/opens a SQLite database, and with the GUI you can read and write to it.
  7. */

  8. // This is the file path of the database file we want to use
  9. // Right now, it'll load TestDB.sqdb in the project's root folder.
  10. // If one doesn't exist, it will be automatically created.
  11. public var DatabaseName : String = "TestDB.sqdb";

  12. // This is the name of the table we want to use
  13. public var TableName : String = "TestTable";
  14. var db : dbAccess;

  15. function Start(){
  16. // Give ourselves a dbAccess object to work with, and open it



  17. db = new dbAccess();
  18. db.OpenDB(DatabaseName);
  19. // Let's make sure we've got a table to work with as well!
  20. var tableName = TableName;
  21. var columnNames = new Array("firstName","lastName");
  22. var columnValues = new Array("text","text");
  23. try {db.CreateTable(tableName,columnNames,columnValues);
  24. }
  25. catch(e){// Do nothing - our table was already created判斷表是否被創(chuàng)建了
  26. //- we don't care about the error, we just don't want to see it
  27. }
  28. }

  29. // These variables just hold info to display in our GUI
  30. var firstName : String = "First Name";
  31. var lastName : String = "Last Name";
  32. var DatabaseEntryStringWidth = 100;
  33. var scrollPosition : Vector2;
  34. var databaseData : ArrayList = new ArrayList();

  35. // This GUI provides us with a way to enter data into our database
  36. // as well as a way to view it
  37. function OnGUI(){
  38. GUI.Box(Rect (25,25,Screen.width - 50, Screen.height - 50),"Data");
  39. GUILayout.BeginArea(Rect(50, 50, Screen.width - 100, Screen.height - 100));
  40. // This first block allows us to enter new entries into our table
  41. GUILayout.BeginHorizontal();
  42. firstName = GUILayout.TextField(firstName, GUILayout.Width (DatabaseEntryStringWidth));
  43. lastName = GUILayout.TextField(lastName, GUILayout.Width (DatabaseEntryStringWidth));

  44. //lastName = GUILayout.TextField();
  45. GUILayout.EndHorizontal();

  46. if (GUILayout.Button("Add to database")){
  47. // Insert the data
  48. InsertRow(firstName,lastName);
  49. // And update the readout of the database
  50. databaseData = ReadFullTable();
  51. }
  52. // This second block gives us a button that will display/refresh the contents of our database
  53. GUILayout.BeginHorizontal();
  54. if (GUILayout.Button ("Read Database"))
  55. databaseData = ReadFullTable();
  56. if (GUILayout.Button("Clear"))
  57. databaseData.Clear();
  58. GUILayout.EndHorizontal();

  59. GUILayout.Label("Database Contents");
  60. scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(100));
  61. for (var line : ArrayList in databaseData){
  62. GUILayout.BeginHorizontal();
  63. for (var s in line){
  64. GUILayout.Label(s.ToString(), GUILayout.Width(DatabaseEntryStringWidth));
  65. }
  66. GUILayout.EndHorizontal();
  67. }

  68. GUILayout.EndScrollView();
  69. if (GUILayout.Button("Delete All Data")){ Unity3D與Sqlite數(shù)據(jù)庫(kù)直連.rar (168.27 KB, 下載次數(shù): 239)


  70. Unity3D與Sqlite數(shù)據(jù)庫(kù)直連.rar (446.06 KB, 下載次數(shù): 584)





  71. DeleteTableContents();
  72. databaseData = ReadFullTable();
  73. }
  74. GUILayout.EndArea();
  75. }

  76. // Wrapper function for inserting our specific entries into our specific database and table for this file
  77. function InsertRow(firstName, lastName){
  78. var values = new Array(("'"+firstName+"'"),("'"+lastName+"'"));
  79. db.InsertInto(TableName, values);
  80. }

  81. // Wrapper function, so we only mess with our table.
  82. function ReadFullTable(){
  83. return db.ReadFullTable(TableName);
  84. }

  85. // Another wrapper function...
  86. function DeleteTableContents(){
  87. db.DeleteTableContents(TableName);
  88. }


這一段代碼是要你掛在你的主攝像機(jī)上面的,其實(shí)我對(duì)數(shù)據(jù)庫(kù)方面的知識(shí)很淺陋,很多地方還不是很理解,希望有高人能夠?qū)⑦@段代碼進(jìn)行詳細(xì)的注釋,以便能讓我們這種菜鳥級(jí)別的能夠充分吸收啊。。

9、下面咱們?cè)賮?lái)看看我們的運(yùn)行結(jié)果吧:

這是在Unity3D中運(yùn)行的結(jié)果,我們?cè)囋噷?duì)數(shù)據(jù)的操作會(huì)怎么樣

我們看見(jiàn)了我們對(duì)數(shù)據(jù)的操作能夠成功,經(jīng)過(guò)測(cè)試,其他的Button也都能出現(xiàn)相對(duì)應(yīng)的效果,那我們?cè)倏纯催@個(gè)到底有沒(méi)有生成我們想要的數(shù)據(jù)庫(kù)文件:

看看截圖大家就知道了:

看見(jiàn)沒(méi),生成了我們想要的數(shù)據(jù)庫(kù)文件了。那我們?cè)賮?lái)看看這個(gè)文件當(dāng)中有木有數(shù)據(jù):

  1. 看見(jiàn)了沒(méi),我們成功了。經(jīng)過(guò)測(cè)試,我們?cè)趯?duì)數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行操作的時(shí)候,我們的Unity3D中的數(shù)據(jù)也會(huì)發(fā)生相應(yīng)的改變了,所以,這次我們成功了。在這里我們要感謝原文的支持哈。希望路過(guò)的高手勿噴哈。。

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Unity3D與Sqlite數(shù)據(jù)庫(kù)直連

Unity3D與Sqlite數(shù)據(jù)庫(kù)直連:網(wǎng)址:http://bbs.9ria.com/forum.phpmod=viewthreadtid=168629fromuid=308561 這幾天也順便研究了下Unity3D 如何與 Sqlite 數(shù)據(jù)庫(kù)進(jìn)行直連的問(wèn)題。不廢話了,直接介紹吧。原文來(lái)自于: http://wiki.unity3d.com/index.ph
推薦度:
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top