ApplicationData.persistentDataPath Unity Editor到Android

时间:2014-11-08 21:54:57

标签: unity3d unityscript

有谁知道,我必须在Unity项目目录结构中放置文件(sqlite文件),以便在apk编译和安装后文件仍存储在Android端的persistentDataPath中?

提前致谢!

1 个答案:

答案 0 :(得分: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.");
            }   

        }           

    }
}