ApplicationData.persistentDataPath Unity编辑器到Android [英] ApplicationData.persistentDataPath Unity Editor to Android
本文介绍了ApplicationData.persistentDataPath Unity编辑器到Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人知道,为了在apk编译和安装后将文件(sqlite文件)放置在unity项目目录结构中的哪个位置,该文件仍存储在Android端的persistentDataPath中?
Does anyone know, where do I have to place a file (sqlite file) in the unity project directory structure, in order to, after apk compilation and installation the file remains stored in the persistentDataPath at Android side?
提前谢谢!
推荐答案
直接在Assets文件夹中,创建一个名称为"StreamingAssets"的文件夹,并将您的sqlite数据库文件放入此文件夹中,然后使用以下代码提取并复制sqlite数据库到persistentDataPath:
Directly inside Assets folder, create a folder with name "StreamingAssets" and put your sqlite database file in this folder then use the following code to extract and copy the sqlite database to 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.");
}
}
}
}
这篇关于ApplicationData.persistentDataPath Unity编辑器到Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文