Background information
我的游戏在iOS上运行,现在正试图将其更新到android上运行。我有一个数据库,Artifact.db,在StreamingAssets文件夹中,它是基于SQLite建立的,它有只读的信息,我用它来获取游戏的数据。
Issue
核心问题是SQlite无法读取数据,很可能是因为从我所读到的文档中无法访问。我正在寻找任何解决方案,但最好是使用
UnityWebRequest
功能,而不是被废弃的
WWW
Working iOS Code
// Platform dependent code
string filepath = Application.streamingAssetsPath + "/Artifact.db";
string conn = "URI=file:" + filepath;
// SQlite
IDbConnection dbconn;
IDbCommand dbcmd;
IDataReader reader;
dbconn = (IDbConnection)new SqliteConnection(conn);
dbconn.Open();
dbcmd = dbconn.CreateCommand();
string sqlQuery = $"SELECT * FROM Artifact WHERE Rarity='{type}'";
dbcmd.CommandText = sqlQuery;
reader = dbcmd.ExecuteReader();
在android上使用这段代码,在做reader = dbcmd.ExecuteReader();时出现错误,因为它无法找到文件。
Attempts at Android code
(由于SQLite代码本身没有问题,我将只包括与平台有关的部分,该部分涉及寻找文件。)
string DatabaseName = "Artifact.db";
#if UNITY_ANDROID
string filepath = Application.persistentDataPath + "/Artifact.db";
if (!File.Exists(filepath))
Debug.Log("Database not in Persistent path");
// if it doesn't ->
// open StreamingAssets directory and load the db ->
var loadDb = new WWW("jar:file://" + Application.dataPath + "!/assets/" + DatabaseName); // this is the path to your StreamingAssets in android
while (!loadDb.isDone) { } // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDb.bytes);
string conn = "URI=file:" + filepath;
#endif
我还试了一下这个的代码unity forum post.(还修改了一些东西以适应我的项目)
var loadingRequest = UnityWebRequest.Get(Path.Combine(Application.streamingAssetsPath, "Artifact.db"));
// Used "Artifact.db" instead of "your.bytes"
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone) {
if (loadingRequest.isNetworkError || loadingRequest.isHttpError) {
break;
if (loadingRequest.isNetworkError || loadingRequest.isHttpError) {
} else {
File.WriteAllBytes(Path.Combine(Application.persistentDataPath , "Artifact.db"), loadingRequest.downloadHandler.data);
// Used "Artifact.db" instead of "your.bytes"