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

提前致谢!

最佳答案

直接在 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.");
            }

        }

    }
}

关于unity3d - ApplicationData.persistentDataPath Unity Editor to Android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26822502/

10-12 12:37