日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Unity3D教程:與Sqlite數(shù)據(jù)庫(kù)直連

環(huán)境介紹:

目前創(chuàng)新互聯(lián)已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、梧州網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

Windows7,Unity3D,SQLite Expert Personal 3

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

JavaScript

需要的dll文件:

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

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

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

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

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

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

  
  
  
  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ù)庫(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)題,那么你就可以成功了。

細(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文件夾下的那個(gè)dll文件 
  4. class dbAccess { 
  5.     // variables for basic query access 
  6.     private var connection : String;        //數(shù)據(jù)庫(kù)的連接字符串,用于建立與特定數(shù)據(jù)源的連接 
  7.     private var dbcon : IDbConnection;        //IDbConnection的連接對(duì)象,其實(shí)就是一個(gè)類對(duì)象 
  8.     private var dbcmd : IDbCommand;                //IDbCommand類對(duì)象,用來(lái)實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的命令:注解:我在網(wǎng)上資料看到的如何實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)執(zhí)行命令:                                                  
  9.                     //首先創(chuàng)建一個(gè)IDbConnection連接對(duì)象,然后將一條數(shù)據(jù)庫(kù)命令賦值給一個(gè)字符串,利用這個(gè)字符串和連接對(duì)象                                                                    
  10.       //就可以創(chuàng)建(new)一個(gè)IDbCommand對(duì)象了,然后使用提供的方法就可以執(zhí)行這個(gè)命令了。 
  11.     private var reader : IDataReader;        //reader的作用就是讀取結(jié)果集的一個(gè)或多個(gè)只進(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ù)庫(kù)連接操作 
  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的對(duì)象,創(chuàng)建IDataReader的對(duì)象 
  21.         if(r){ // if we want to return the reader 
  22.         return reader; // return the reader        返回讀取的對(duì)象,就是讀到了什么東西 
  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
  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
  72.             query += ", " + col; 
  73.         } 
  74.         query += ") VALUES (" + values[0]; 
  75.         for(i=1; 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
  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中使用這個(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.  
  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中運(yùn)行的結(jié)果,數(shù)據(jù)的操作結(jié)果如下:

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

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


新聞名稱:Unity3D教程:與Sqlite數(shù)據(jù)庫(kù)直連
本文地址:http://m.5511xx.com/article/dhdhphd.html