在移動端StreamingAssets下的文件是只讀的,但大家可能跟我一樣遇到了發布安卓以后放在StreamingAssets下的數據庫文件一樣讀取不了,
但其實這個文件夾其實是可以讀取到的,所以我們需要使用XML來讀取文件,然后轉存到persistentDataPath目錄下,下面是代碼:
1 public static class Config 2 { 3 public static string filepath = Application.persistentDataPath + "/TowerDefenseDB.sqlite3"; 4 public static string DbConnectionString = "URI=file:" + filepath; 5 }
1 if (!File.Exists(Config.filepath)) 2 { 3 //如果數據庫文件沒有被創建,則創建數據庫文件. 4 WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "TowerDefenseDB.sqlite3"); 5 while (!loadDB.isDone) 6 { 7 } 8 File.WriteAllBytes(Config.filepath, loadDB.bytes); 9 } 10 conn = new SqliteConnection(Config.DbConnectionString);
下面是數據庫的正常操作代碼,我這里就不多寫了