Including files in building apk application in Unity

a 夏天 提交于 2019-12-11 00:20:43

问题


How to add files and folders to the apk file when building it in unity.

What I need is to have some files and a folder to be present in the parent directory of the application ( android/data/com.company.product/files) after installing it on Android.


回答1:


This is my code for copying files from streaming assets into android persist path:

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;
    }
}

I hope it helps.



来源:https://stackoverflow.com/questions/41848427/including-files-in-building-apk-application-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!