Unity - 在 StreamingAssets 之外的其他文件夹中使用 SQLite 数据库

Unity - Use SQLite database in other folder than StreamingAssets

我为 Android 设备制作了一个交叉匹配单词的 Unity 游戏。目前,我在 StreamingAssets 文件夹中使用 SQLite 数据库来获取游戏中的文字,一切都很好。

问题是任何人都可以轻松访问我在 StreamingAssets 文件夹中的数据库。

有什么方法可以从 StreamingAssets 以外的其他文件夹移动和访问我的数据库吗?

我正在使用下面的代码访问我在 Android 上的数据库:

string databaseName = "/MyDatabase.db";
StartCoroutine(SetDatabase(databaseName));
return Path.Combine("URI=file:" + Application.persistentDataPath + databaseName);
 
private IEnumerator SetDatabase(string databaseName)
{
   string path = Path.Combine("jar:file://" + Application.dataPath + "!/assets" + databaseName);
   UnityWebRequest unityWebRequest = UnityWebRequest.Get(path);
   yield return unityWebRequest.SendWebRequest();            
 
   if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
         AppHelper.Dbg("SetDatabase()", this + ": " +unityWebRequest.error);
 
   /// Retrieve results as binary data.
   byte[] data = unityWebRequest.downloadHandler.data;
 
   /// Writes the DB in the persistent memory.
   File.WriteAllBytes(Path.Combine(Application.persistentDataPath + databaseName), data);
}

附上我的 .apk 文件的屏幕截图,显示了我在 StreamingAssets 文件夹中的数据库。

所以我找到了解决我遇到的问题的方法。我将我的数据库添加到 Resources 文件夹中,然后使用下面的代码将其复制到持久内存中,因此该数据库在 .apk 文件中不可见。

private void CopyDefinitionsDatabaseFromResources()
{
    string RESOURCES_DATABASE_NAME = "words.bytes";
    string DATABASE_NAME = "words.db";
    
    TextAsset db = Resources.Load<TextAsset>(Path.GetFileNameWithoutExtension(RESOURCES_DATABASE_NAME));
    byte[] data = db.bytes;
            
    // copy the database file from resources to persistent app path
    string destination = Path.Combine(Application.persistentDataPath, DATABASE_NAME);
    // writing file
    File.WriteAllBytes(destination, data); 
}