2014-11-08 2 views
3

Кто-нибудь знает, где я должен поместить файл (файл sqlite) в структуру каталога проекта единства, чтобы после компиляции и установки apk файл оставался сохраненным в persistentDataPath при Android-сторона?ApplicationData.persistentDataPath Unity Editor для Android

Заранее благодарен!

ответ

4

непосредственно в папке Assets, создайте папку с именем «StreamingAssets» и поместить файл базы данных SQLite в этой папке, используйте следующий код для извлечения и копирование SQLite базы данных в persistentDataPath:

void InitSqliteFile (string dbName) 
{ 
    // dbName = "example.sqlite"; 
    pathDB = System.IO.Path.Combine (Application.persistentDataPath, dbName); 
    //original path 
    string sourcePath = System.IO.Path.Combine (Application.streamingAssetsPath, dbName); 

    //if DB does not exist in persistent data folder (folder "Documents" on iOS) or source DB is newer then copy it 
    if (!System.IO.File.Exists (pathDB) || (System.IO.File.GetLastWriteTimeUtc(sourcePath) > System.IO.File.GetLastWriteTimeUtc(pathDB))) { 

     if (sourcePath.Contains ("://")) { 
      // Android 
      WWW www = new WWW (sourcePath); 
      // Wait for download to complete - not pretty at all but easy hack for now 
      // and it would not take long since the data is on the local device. 
      while (!www.isDone) {;} 

      if (String.IsNullOrEmpty(www.error)) {     
       System.IO.File.WriteAllBytes(pathDB, www.bytes); 
      } else { 
       CanExQuery = false;          
      } 

     } else { 
      // Mac, Windows, Iphone 

      //validate the existens of the DB in the original folder (folder "streamingAssets") 
      if (System.IO.File.Exists (sourcePath)) { 

       //copy file - alle systems except Android 
       System.IO.File.Copy (sourcePath, pathDB, true); 

      } else { 
       CanExQuery = false; 
       Debug.Log ("ERROR: the file DB named " + dbName + " doesn't exist in the StreamingAssets Folder, please copy it there."); 
      } 

     }   

    } 
} 
+0

должен быть отмечен как правильный ответ. Я боролся с этим, но я думаю, что папка StreamingAssets была ключом. Так много ошибок, так мало времени. – gdbj

Смежные вопросы