自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Unity3D教程:與Sqlite數(shù)據(jù)庫直連

開發(fā) 游戲開發(fā)
本文將為您介紹Unity 3D如何與Sqlite數(shù)據(jù)進(jìn)行連接的過程,操作環(huán)境為Windows 7下。

環(huán)境介紹:

Windows7,Unity3D,SQLite Expert Personal 3

開發(fā)語言:

JavaScript

需要的dll文件:

Mono.Data.Sqlite.dll和sqlite3.dll,dll文件位置,截圖:

Unity3D教程:Unity3D與Sqlite數(shù)據(jù)庫直連

Unity3D教程:Unity3D與Sqlite數(shù)據(jù)庫直連

一定要在這個目錄下,請保持一致。

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

Unity3D教程:Unity3D與Sqlite數(shù)據(jù)庫直連

要改動的地方已用紅色標(biāo)記,注意這個要改成.NET2.0,這樣才能夠發(fā)布的。系統(tǒng)默認(rèn)的不是.NET2.0,這一點(diǎn)要注意!?。?/p>

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

  1. /*  Javascript class for accessing SQLite objects. 
  2.      To use it, you need to make sure you COPY Mono.Data.SQLiteClient.dll from wherever it lives in your Unity directory 
  3.      to your project's Assets folder 
  4.      Originally created by dklompmaker in 2009 
  5.      http://forum.unity3d.com/threads ... sier-Database-Stuff 
  6.      Modified 2011 by Alan Chatham           */ 
  7. //#pragma strict 
  8. /* 

代碼描述

*本代碼是為了在Windows環(huán)境下運(yùn)行unity3d和Sqlite數(shù)據(jù)庫而寫的;實現(xiàn)的基本功能是unity3d能夠與數(shù)據(jù)庫之間進(jìn)行基本的通信,比如說:在數(shù)據(jù)庫中的數(shù)據(jù)被改變了以后,unity3d中得到的數(shù)據(jù)也會在刷新了之后跟著改變;這只是一個基本的核心的技術(shù),為的是能夠應(yīng)用在大型的unity3d項目中,能夠存儲場景中的項目的屬性,在需要改變對象的屬性或增加、減少等對象時能夠很方便的用得上。要實現(xiàn)本代碼。首先需要一些dll文件,一個是Mono.Data.SQLiteClient.dll,另外一個是sqlite3.dll,這些文件都能夠在unity3d的安裝目錄中找得到。除此之外,還需要把這兩個文件放在你的項目的這個路徑下面:\Assets\Plugins\,沒有Plugins文件夾就必須創(chuàng)建這個文件夾,然后將這兩個dll文件放在該文件夾寫。當(dāng)然,如果你想能夠在PC上面發(fā)布成可執(zhí)行文件,還需要改動一些地方。在unity3d中的Play Setting ->Other Setting 中將Api Compatibility的等級改為.NET 2.0;那么這些操作做完了以后,如果你的代碼寫得沒有問題,那么你就可以成功了。

細(xì)解釋代碼: 

  1. */ 
  2. import          System.Data;  // we import our  data class 我們先導(dǎo)入我們的數(shù)據(jù)集 
  3. import          Mono.Data.Sqlite; // we import sqlite        我們導(dǎo)入sqlite數(shù)據(jù)集,也就是Plugins文件夾下的那個dll文件 
  4. class dbAccess { 
  5.     // variables for basic query access 
  6.     private var connection : String;        //數(shù)據(jù)庫的連接字符串,用于建立與特定數(shù)據(jù)源的連接 
  7.     private var dbcon : IDbConnection;        //IDbConnection的連接對象,其實就是一個類對象 
  8.     private var dbcmd : IDbCommand;                //IDbCommand類對象,用來實現(xiàn)操作數(shù)據(jù)庫的命令:注解:我在網(wǎng)上資料看到的如何實現(xiàn)對數(shù)據(jù)庫執(zhí)行命令:                                                  
  9.                     //首先創(chuàng)建一個IDbConnection連接對象,然后將一條數(shù)據(jù)庫命令賦值給一個字符串,利用這個字符串和連接對象                                                                    
  10.       //就可以創(chuàng)建(new)一個IDbCommand對象了,然后使用提供的方法就可以執(zhí)行這個命令了。 
  11.     private var reader : IDataReader;        //reader的作用就是讀取結(jié)果集的一個或多個只進(jìn)結(jié)果流 
  12.     function OpenDB(p : String){ 
  13.     connection = "URI=file:" + p; // we set the connection to our database 
  14.     dbcon = new SqliteConnection(connection); 
  15.     dbcon.Open();                                                //打開數(shù)據(jù)庫連接操作 
  16.     } 
  17.     function BasicQuery(q : String, r : boolean){ // run a baic Sqlite query 
  18.         dbcmd = dbcon.CreateCommand(); // create empty command 
  19.         dbcmd.CommandText = q; // fill the command 
  20.         reader = dbcmd.ExecuteReader(); // execute command which returns a reader  返回IDataReader的對象,創(chuàng)建IDataReader的對象 
  21.         if(r){ // if we want to return the reader 
  22.         return reader; // return the reader        返回讀取的對象,就是讀到了什么東西 
  23.         } 
  24.     } 
  25.     // This returns a 2 dimensional ArrayList with all the 
  26.     //  data from the table requested 
  27.     function ReadFullTable(tableName : String){ 
  28.         var query : String; 
  29.         query = "SELECT * FROM " + tableName; 
  30.         dbcmd = dbcon.CreateCommand(); 
  31.         dbcmd.CommandText = query; 
  32.         reader = dbcmd.ExecuteReader(); 
  33.         var readArray = new ArrayList(); 
  34.         while(reader.Read()){ 
  35.             var lineArray = new ArrayList(); 
  36.             for (var i = 0; i < reader.FieldCount; i++) 
  37.                 lineArray.Add(reader.GetValue(i)); // This reads the entries in a row 
  38.             readArray.Add(lineArray); // This makes an array of all the rows 
  39.         } 
  40.         return readArray; // return matches 
  41.     } 
  42.     // This function deletes all the data in the given table.  Forever.  WATCH OUT! Use sparingly, if at all 
  43.     function DeleteTableContents(tableName : String){ 
  44.     var query : String; 
  45.     query = "DELETE FROM " + tableName; 
  46.     dbcmd = dbcon.CreateCommand(); 
  47.     dbcmd.CommandText = query; 
  48.     reader = dbcmd.ExecuteReader(); 
  49.     } 
  50.     function CreateTable(name : String, col : Array, colType : Array){ // Create a table, name, column array, column type array 
  51.         var query : String; 
  52.         query  = "CREATE TABLE " + name + "(" + col[0] + " " + colType[0]; 
  53.         for(var i=1; i<col.length; i++){ 
  54.             query += ", " + col + " " + colType; 
  55.         } 
  56.         query += ")"
  57.         dbcmd = dbcon.CreateCommand(); // create empty command 
  58.         dbcmd.CommandText = query; // fill the command 
  59.         reader = dbcmd.ExecuteReader(); // execute command which returns a reader 
  60.     } 
  61.     function InsertIntoSingle(tableName : String, colName : String, value : String){ // single insert 
  62.         var query : String; 
  63.         query = "INSERT INTO " + tableName + "(" + colName + ") " + "VALUES (" + value + ")"
  64.         dbcmd = dbcon.CreateCommand(); // create empty command 
  65.         dbcmd.CommandText = query; // fill the command 
  66.         reader = dbcmd.ExecuteReader(); // execute command which returns a reader 
  67.     } 
  68.     function InsertIntoSpecific(tableName : String, col : Array, values : Array){ // Specific insert with col and values 
  69.         var query : String; 
  70.         query = "INSERT INTO " + tableName + "(" + col[0]; 
  71.         for(var i=1; i<col.length; i++){ 
  72.             query += ", " + col; 
  73.         } 
  74.         query += ") VALUES (" + values[0]; 
  75.         for(i=1; i<values.length; i++){ 
  76.             query += ", " + values; 
  77.         } 
  78.         query += ")"
  79.         dbcmd = dbcon.CreateCommand(); 
  80.         dbcmd.CommandText = query; 
  81.         reader = dbcmd.ExecuteReader(); 
  82.    } 
  83.     function InsertInto(tableName : String, values : Array){ // basic Insert with just values 
  84.         var query : String; 
  85.         query = "INSERT INTO " + tableName + " VALUES (" + values[0]; 
  86.         for(var i=1; i<values.length; i++){ 
  87.             query += ", " + values; 
  88.         } 
  89.         query += ")"
  90.         dbcmd = dbcon.CreateCommand(); 
  91.         dbcmd.CommandText = query; 
  92.         reader = dbcmd.ExecuteReader(); 
  93.     } 
  94.     // This function reads a single column 
  95.     //  wCol is the WHERE column, wPar is the operator you want to use to compare with, 
  96.     //  and wValue is the value you want to compare against. 
  97.     //  Ex. - SingleSelectWhere("puppies", "breed", "earType", "=", "floppy") 
  98.     //  returns an array of matches from the command: SELECT breed FROM puppies WHERE earType = floppy; 
  99.     function SingleSelectWhere(tableName : String, itemToSelect : String, wCol : String, wPar : String, wValue : String){ // Selects a single Item 
  100.         var query : String; 
  101.         query = "SELECT " + itemToSelect + " FROM " + tableName + " WHERE " + wCol + wPar + wValue;         
  102.         dbcmd = dbcon.CreateCommand(); 
  103.         dbcmd.CommandText = query; 
  104.         reader = dbcmd.ExecuteReader(); 
  105.         var readArray = new Array(); 
  106.         while(reader.Read()){ 
  107.             readArray.Push(reader.GetString(0)); // Fill array with all matches 
  108.         } 
  109.         return readArray; // return matches 
  110.     } 
  111.     function CloseDB(){ 
  112.         reader.Close(); // clean everything up 
  113.         reader = null
  114.         dbcmd.Dispose(); 
  115.         dbcmd = null
  116.         dbcon.Close(); 
  117.         dbcon = null
  118.     } 

7、如何在Unity3D中使用這個數(shù)據(jù)庫的代碼:

  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.  
  7.         It creates/opens a SQLite database, and with the GUI you can read and write to it. 
  8.  
  9.                                         */ 
  10.  
  11. // This is the file path of the database file we want to use 
  12.  
  13. // Right now, it'll load TestDB.sqdb in the project's root folder. 
  14.  
  15. // If one doesn't exist, it will be automatically created. 
  16.  
  17. public var DatabaseName : String = "TestDB.sqdb"
  18.  
  19. // This is the name of the table we want to use 
  20.  
  21. public var TableName : String = "TestTable"
  22.  
  23. var db : dbAccess; 
  24.  
  25. function Start(){ 
  26.  
  27.     // Give ourselves a dbAccess object to work with, and open it 
  28.  
  29.     db = new dbAccess(); 
  30.  
  31.     db.OpenDB(DatabaseName); 
  32.  
  33.     // Let's make sure we've got a table to work with as well! 
  34.  
  35.     var tableName = TableName; 
  36.  
  37.     var columnNames = new Array("firstName","lastName"); 
  38.  
  39.     var columnValues = new Array("text","text"); 
  40.  
  41.     try {db.CreateTable(tableName,columnNames,columnValues); 
  42.  
  43.     } 
  44.  
  45.     catch(e){// Do nothing - our table was already created判斷表是否被創(chuàng)建了 
  46.  
  47.         //- we don't care about the error, we just don't want to see it 
  48.  
  49.     } 
  50.  
  51.  
  52. // These variables just hold info to display in our GUI 
  53.  
  54. var firstName : String = "First Name"
  55.  
  56. var lastName : String = "Last Name"
  57.  
  58. var DatabaseEntryStringWidth = 100; 
  59.  
  60. var scrollPosition : Vector2; 
  61.  
  62. var databaseData : ArrayList = new ArrayList(); 
  63.  
  64. // This GUI provides us with a way to enter data into our database 
  65.  
  66. //  as well as a way to view it 
  67.  
  68. function OnGUI(){ 
  69.  
  70.     GUI.Box(Rect (25,25,Screen.width - 50, Screen.height - 50),"Data"); 
  71.  
  72.     GUILayout.BeginArea(Rect(50, 50, Screen.width - 100, Screen.height - 100)); 
  73.  
  74.     // This first block allows us to enter new entries into our table 
  75.  
  76.         GUILayout.BeginHorizontal(); 
  77.  
  78.             firstName = GUILayout.TextField(firstName, GUILayout.Width (DatabaseEntryStringWidth)); 
  79.  
  80.             lastName = GUILayout.TextField(lastName, GUILayout.Width (DatabaseEntryStringWidth)); 
  81.  
  82.             //lastName = GUILayout.TextField(); 
  83.  
  84.         GUILayout.EndHorizontal(); 
  85.  
  86.         if (GUILayout.Button("Add to database")){ 
  87.  
  88.             // Insert the data 
  89.  
  90.             InsertRow(firstName,lastName); 
  91.  
  92.             // And update the readout of the database 
  93.  
  94.             databaseData = ReadFullTable(); 
  95.  
  96.         } 
  97.  
  98.         // This second block gives us a button that will display/refresh the contents of our database 
  99.  
  100.         GUILayout.BeginHorizontal(); 
  101.  
  102.             if (GUILayout.Button ("Read Database"))         
  103.  
  104.                 databaseData = ReadFullTable(); 
  105.  
  106.             if (GUILayout.Button("Clear")) 
  107.  
  108.                 databaseData.Clear(); 
  109.  
  110.         GUILayout.EndHorizontal(); 
  111.  
  112.         GUILayout.Label("Database Contents"); 
  113.  
  114.         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(100)); 
  115.  
  116.             for (var line : ArrayList in databaseData){ 
  117.  
  118.                 GUILayout.BeginHorizontal(); 
  119.  
  120.                 for (var s in line){ 
  121.  
  122.                     GUILayout.Label(s.ToString(), GUILayout.Width(DatabaseEntryStringWidth)); 
  123.  
  124.                 } 
  125.  
  126.                 GUILayout.EndHorizontal(); 
  127.  
  128.             } 
  129.  
  130.         GUILayout.EndScrollView(); 
  131.  
  132.         if (GUILayout.Button("Delete All Data")){ 
  133.  
  134.  DeleteTableContents(); 
  135.  
  136.             databaseData = ReadFullTable(); 
  137.  
  138.         } 
  139.  
  140.     GUILayout.EndArea(); 
  141.  
  142.  
  143. // Wrapper function for inserting our specific entries into our specific database and table for this file 
  144.  
  145. function InsertRow(firstName, lastName){ 
  146.  
  147.     var values = new Array(("'"+firstName+"'"),("'"+lastName+"'")); 
  148.  
  149.     db.InsertInto(TableName, values); 
  150.  
  151.  
  152. // Wrapper function, so we only mess with our table. 
  153.  
  154. function ReadFullTable(){ 
  155.  
  156.     return db.ReadFullTable(TableName); 
  157.  
  158.  
  159. // Another wrapper function... 
  160.  
  161. function DeleteTableContents(){ 
  162.  
  163.     db.DeleteTableContents(TableName); 
  164.  

 

  運(yùn)行結(jié)果:

Unity3D教程:Unity3D與Sqlite數(shù)據(jù)庫直連

這是在Unity3D中運(yùn)行的結(jié)果,數(shù)據(jù)的操作結(jié)果如下:

Unity3D教程:Unity3D與Sqlite數(shù)據(jù)庫直連

我們看見了數(shù)據(jù)的操作能夠成功,經(jīng)過測試,其他的Button也都能出現(xiàn)相對應(yīng)的效果,那我們再看看這個到底有沒有生成我們想要的數(shù)據(jù)庫文件:

Unity3D教程:Unity3D與Sqlite數(shù)據(jù)庫直連

 

Unity3D教程:Unity3D與Sqlite數(shù)據(jù)庫直連

文件當(dāng)中數(shù)據(jù):經(jīng)測試,我們在對數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行操作的時候,我們的Unity3D中的數(shù)據(jù)也會發(fā)生相應(yīng)的改變了!

原文鏈接:http://bbs.9ria.com/forum.php?mod=viewthread&tid=168629&fromuid=308561

 

 

責(zé)任編輯:彭凡 來源: 9RIA天地會
相關(guān)推薦

2013-04-25 09:56:24

unity3D手機(jī)游戲引擎

2013-04-25 10:03:07

unity3D手機(jī)游戲引擎

2013-06-19 08:52:48

Unity3D

2013-06-17 09:12:31

Unity3D

2013-06-18 08:49:15

2013-04-25 13:27:11

unity3D手機(jī)游戲引擎

2012-12-24 09:14:31

ios

2013-04-25 09:08:39

unity3D手機(jī)游戲引擎

2013-04-09 13:42:23

Unity3D基礎(chǔ)知識梳理

2012-12-24 09:20:48

AndoidUnity3D

2012-12-24 09:09:27

AndoidUnity3D

2012-03-06 09:50:24

Android SQLAndroidSQLite3

2012-12-24 09:15:57

iOSUnity3D

2012-12-24 08:52:44

iOSUnity3D

2013-04-10 14:21:35

2018-07-13 09:20:30

SQLite數(shù)據(jù)庫存儲

2012-12-24 09:19:31

iOSUnity3D

2012-12-24 09:08:14

iOSUnity3D

2012-12-24 08:40:12

2012-12-24 08:51:23

iOSUnity3D
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號