gpt4 book ai didi

unity-game-engine - ApplicationData.persistentDataPath Unity 编辑器到 Android

转载 作者:行者123 更新时间:2023-12-02 12:08:22 27 4
gpt4 key购买 nike

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

提前致谢!

最佳答案

直接在 Assets 文件夹中,创建一个名为“StreamingAssets”的文件夹,并将您的 sqlite 数据库文件放入此文件夹中,然后使用以下代码提取 sqlite 数据库并将其复制到 permanentDataPath:

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

}

}
}

关于unity-game-engine - ApplicationData.persistentDataPath Unity 编辑器到 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822502/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com