Android 在Unity中构建apk应用程序时包含文件

Android 在Unity中构建apk应用程序时包含文件,android,unity3d,apk,Android,Unity3d,Apk,在unity中构建apk文件时,如何将文件和文件夹添加到apk文件 在android上安装应用程序后,我需要在父目录(android/data/com.company.product/files)中显示一些文件和文件夹。这是我将文件从流媒体资产复制到android持久路径的代码: using System.IO; using UnityEngine; public static class FileManager { public static string RereadFile(str

在unity中构建apk文件时,如何将文件和文件夹添加到apk文件


在android上安装应用程序后,我需要在父目录(android/data/com.company.product/files)中显示一些文件和文件夹。

这是我将文件从流媒体资产复制到android持久路径的代码:

using System.IO;
using UnityEngine;
public static class FileManager
{
    public static string RereadFile(string fileName)
    {  //copies and unpacks file from apk to persistentDataPath where it can be accessed
        string destinationPath = Path.Combine(Application.persistentDataPath, fileName);
#if UNITY_EDITOR
        string sourcePath = Path.Combine(Application.streamingAssetsPath, fileName);
#else
        string sourcePath = "jar:file://" + Application.dataPath + "!/assets/" + fileName;
#endif

        //UnityEngine.Debug.Log(string.Format("{0}-{1}-{2}-{3}", sourcePath,  File.GetLastWriteTimeUtc(sourcePath), File.GetLastWriteTimeUtc(destinationPath)));

        //copy whatsoever

        //if DB does not exist in persistent data folder (folder "Documents" on iOS) or source DB is newer then copy it
        //if (!File.Exists(destinationPath) || (File.GetLastWriteTimeUtc(sourcePath) > File.GetLastWriteTimeUtc(destinationPath)))
        {
            if (sourcePath.Contains("://"))
            {
                // Android  
                WWW www = new WWW(sourcePath);
                while (!www.isDone) {; }                // Wait for download to complete - not pretty at all but easy hack for now 
                if (string.IsNullOrEmpty(www.error))
                {
                    File.WriteAllBytes(destinationPath, www.bytes);
                }
                else
                {
                    Debug.Log("ERROR: the file DB named " + fileName + " doesn't exist in the StreamingAssets Folder, please copy it there.");
                }
            }
            else
            {
                // Mac, Windows, Iphone                
                //validate the existens of the DB in the original folder (folder "streamingAssets")
                if (File.Exists(sourcePath))
                {
                    //copy file - alle systems except Android
                    File.Copy(sourcePath, destinationPath, true);
                }
                else
                {
                    Debug.Log("ERROR: the file DB named " + fileName + " doesn't exist in the StreamingAssets Folder, please copy it there.");
                }
            }
        }

        StreamReader reader = new StreamReader(destinationPath);
        var jsonString = reader.ReadToEnd();
        reader.Close();


        return jsonString;
    }
}

我希望这会有所帮助。

afaik您只能从streaming assets文件夹复制到android persist path。我只是这样做了:我将文件复制到StreamingAssets文件夹,并附加一个脚本,就像他们在这个链接中所说的那样:“,但“persistentDataPath”仍然为空。这可能是访问冲突问题。确保您在manifestI中授予了写入外部存储系统权限,并将这一行添加到Androidmainfest文件:“但我得到了相同的结果,它在pc上工作,并复制所有文件,但在Samsung S4上尝试时没有。请尝试debug.log源路径和目标路径,然后在logcat中读取它们。可能设备上发生异常。我已经使用这段代码将近一年了,我在任何设备上都没有遇到任何问题,请确保您包括extensionDirectoryNotFoundException:Directory'/jar:file:/data/app/com.ScryptechTest.afflow-2.apk/找不到资产。这是一个错误,请确保这一行正确:
“jar:file://“+Application.dataPath+”!/assets/“+fileName
,其中
fileName
在流媒体资产中与文件扩展名完全相同(我不知道区分大小写,但使用相同的以防万一)