【问题标题】:Including files in building apk application in Unity在 Unity 中构建 apk 应用程序时包含文件
【发布时间】:2017-01-25 09:56:10
【问题描述】:

unity构建时如何在apk文件中添加文件和文件夹。

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

【问题讨论】:

  • afaik 你只能从流媒体资产文件夹复制到 android 持久路径。
  • 我就是这样做的:我将文件复制到 StreamingAssets 文件夹并附加一个脚本,正如他们在此链接中所说的那样:“docs.unity3d.com/ScriptReference/…”但“persistentDataPath”仍然为空。
  • 这可能是访问冲突问题。确保您在清单中提供了 WRITE_EXTERNAL_STORAGE 系统权限
  • 我将这一行添加到 Androidmainfest 文件中:" " 但我得到了相同的结果

标签: android unity3d apk


【解决方案1】:

这是我将文件从流媒体资源复制到 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;
    }
}

希望对你有帮助。

【讨论】:

  • 它可以在电脑上运行,它会复制所有文件,但在三星 S4 上尝试时却不行。
  • 尝试 debug.log 源和目标路径并在 logcat 中读取它们。设备上可能发生异常。我已经使用这段代码快一年了,在任何设备上我都没有遇到任何问题
  • 并确保您包含了扩展程序
  • DirectoryNotFoundException:找不到目录'/jar:file:/data/app/com.ScryptechTest.AirFlow-2.apk!/assets'。这是错误
  • 确保你把这行写对了:"jar:file://" + Application.dataPath + "!/assets/" + fileName 其中filename 是流媒体资产中的完全相同的文件名及其文件扩展名(我不知道区分大小写,但在案例)
猜你喜欢
  • 1970-01-01
  • 2012-05-10
  • 2011-05-17
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
  • 2022-01-01
  • 2021-10-09
相关资源
最近更新 更多