【问题标题】:ApplicationData.persistentDataPath Unity Editor to AndroidApplicationData.persistentDataPath Unity Editor to Android
【发布时间】:2016-09-20 16:48:48
【问题描述】:

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

提前致谢!

【问题讨论】:

    标签: unity3d unityscript


    【解决方案1】:

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

    【讨论】:

    • 应该被标记为正确答案。我为此苦苦挣扎,但我猜 StreamingAssets 文件夹是关键。这么多的陷阱,这么少的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    相关资源
    最近更新 更多