2015-11-14 24 views
0

您好我正在unity3d 5(語言C#)需要保存一些數據到SQLite數據庫的Android遊戲項目。 遊戲沒有太多的數據來存儲它只有1個表。有一些連接到sqlite的統一插件,但我不想使用它們。 那麼如何將我的數據存儲在統一的sqlite文件?如何連接一個sqlite數據庫在統一3D 5?(無插件)

+0

我覺得這個鏈接可以幫助你:http://answers.unity3d.com/questions/743400/database-sqlite-setup-for-unity.html –

回答

4

你可以找到答案here

  1. 下assets文件夾中創建新的文件夾重命名爲插件
  2. 複製sqlite3.defsqlite3.dll資產/插件在統一的項目。你可以在這裏下載這些文件 http://www.sqlite.org/download.html的Windows(預編譯的二進制 適用於Windows)
  3. 下載SQLite的瀏覽器http://sourceforge.net/projects/sqlitebrowser/http://sqliteadmin.orbmu2k.de/下載SQLite管理員工具
  4. 創建數據庫使用SQLite瀏覽器在您的統一項目中的資產文件夾
  5. 複製System.Data.dll中Mono.Data.Sqlite.dll從** C:\ Program Files文件(x86)的\團結\編輯\ DATA \單聲道\ LIB \單聲道\ 2.0 *並粘貼在Unity項目的Assets/Plugins *文件夾中。
  6. 使用Mono.Data.Sqlite添加這些命名空間;使用System.Data;使用系統;
  7. string conn =「URI = file:」+ Application.dataPath +「/PickAndPlaceDatabase.s3db」;

與你的數據庫名稱替換PickAndPlaceDatabase.s3db

void Start() { 
    string conn = "URI=file:" + Application.dataPath + "/PickAndPlaceDatabase.s3db"; //Path to database. 
    IDbConnection dbconn; 
    dbconn = (IDbConnection) new SqliteConnection(conn); 
    dbconn.Open(); //Open connection to the database. 
    IDbCommand dbcmd = dbconn.CreateCommand(); 
    string sqlQuery = "SELECT value,name, randomSequence " + "FROM PlaceSequence"; 
    dbcmd.CommandText = sqlQuery; 
    IDataReader reader = dbcmd.ExecuteReader(); 
    while (reader.Read()) 
    { 
     int value = reader.GetInt32(0); 
     string name = reader.GetString(1); 
     int rand = reader.GetInt32(2); 

     Debug.Log("value= "+value+" name ="+name+" random ="+ rand); 
    } 
    reader.Close(); 
    reader = null; 
    dbcmd.Dispose(); 
    dbcmd = null; 
    dbconn.Close(); 
    dbconn = null; 
} 

enter image description here enter image description here

而且SQLite的幫助,請訪問:http://www.tutorialspoint.com/sqlite/