android - 在 Android 上访问 Unity StreamingAssets

标签 android directory assets unity3d

我写了一些代码来获取 StreamingAssets 中某个文件夹中的所有文件夹,然后获取该文件夹中的所有文件。 它在 Windows 上运行良好,但我无法在 Android 上运行。

代码如下:

foreach (string s in Directory.GetDirectories(model.path + "/Levels")) {
        GameObject el = (GameObject)Instantiate(listButton, Vector3.zero, Quaternion.identity);
        el.transform.SetParent(grid1.transform);
        string[] words = s.Split('/');
        #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
                            words = s.Split('\\');
        #endif
        el.GetComponentInChildren<Text>().text = words[words.Length - 1];
        el.GetComponent<Button>().onClick.AddListener(() => {
            MapSizeButtonClick(Int32.Parse(el.GetComponentInChildren<Text>().text));
        });
    }

由于 '\',我必须为 Windows 添加 #if UNITY_STANDALONE_WIN。

然后对于该文件夹中的文件,我这样写:

foreach (string s in Directory.GetFiles(model.path + "/Levels/" + model.mapSize + "/" + model.colorNumber)) {
        string[] words = s.Split('/');
        #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
                    words = s.Split('\\');
        #endif
        words = words[words.Length - 1].Split('.');
        if (words[1] == "json") {
            GameObject el = (GameObject)Instantiate(listButton, Vector3.zero, Quaternion.identity);
            el.transform.SetParent(grid3.transform);
            el.GetComponentInChildren<Text>().text = words[0];
            el.GetComponent<Button>().onClick.AddListener(() => {
                levelButtonClick(Int32.Parse(el.GetComponentInChildren<Text>().text));
            });
        }
    }

我正在使用 UnityEngine.UI.Directory,但我了解到它不能在 Android 上使用,我必须改用 WWW。像这样:

string xmlSetUpPath = "jar:file://" + Application.dataPath + "!/assets/xml/xmlSetUp.xml";

        WWW book1WWW = new WWW(book1Path);
        yield return book1WWW;
        if(!File.Exists(Application.persistentDataPath + "/xml/book1.xml"))
        {
            File.WriteAllBytes(Application.persistentDataPath + "/xml/book1.xml", book1WWW.bytes); 
        }

但是我找不到任何类似 Directory.getFiles() 或 Directory.getFolder() 的东西。所以我想我可以使用 WWW 检查文件是否存在(因为文件名是 1.json、2.json、3.json)并迭代直到文件不存在。

但我还需要检查脚本第一部分的文件夹是否存在,但我找不到如何检查 WWW 的文件夹是否存在。 (我的文件夹名称也是1,2,3,4...)

那么如何检查一个文件夹是否存在 WWW 呢?

或者我还能做什么?

更新:

我更新了代码,现在它看起来像这样:

for (int i = 0; i < 100; i++) {
        if (Directory.Exists(Path.Combine(model.path, "Levels/" + i.ToString()))){
            GameObject el = (GameObject)Instantiate(listButton, Vector3.zero, Quaternion.identity);
            el.transform.SetParent(grid1.transform);
            el.GetComponentInChildren<Text>().text = i.ToString();
            el.GetComponent<Button>().onClick.AddListener(() => {
                MapSizeButtonClick(Int32.Parse(el.GetComponentInChildren<Text>().text));
            });
        }
    }

我使用 path.combine() 并检查从 0 到 100 的每个文件夹。它适用于 Windows,但它仍然不适用于 Android。

路径设置如下:

#if UNITY_IPHONE
        path = Application.dataPath + "/Raw";
    #endif

    #if UNITY_ANDROID
        path = "jar:file://" + Application.dataPath + "!/assets";
    #endif

    #if UNITY_STANDALONE || UNITY_EDITOR
        path = Application.dataPath + "/StreamingAssets";
    #endif

问题更新

我制作了一个生成文件路径的脚本。路径始终相同:“FolderNumerotedFromYToX/FolderNumerotedFromYToX/FileNumerotedFrom1ToX.json”。生成有效,但写入 JSON 无效。它输出“{}”。

        // Generate level list
    List<KeyValuePair<int, List<KeyValuePair<int, List<int>>>>> l;
    l = new List<KeyValuePair<int, List<KeyValuePair<int, List<int>>>>>();
    for (int i = 1; i < 100; i++) {
        if (Directory.Exists(Path.Combine(Path.Combine(Application.dataPath, "StreamingAssets"), "Levels/" + i.ToString()))) {
            l.Add(new KeyValuePair<int, List<KeyValuePair<int, List<int>>>>(i, new List<KeyValuePair<int, List<int>>>()));
            for (int j = 1; j < 100; j++) {
                if (Directory.Exists(Path.Combine(Path.Combine(Application.dataPath, "StreamingAssets"), "Levels/" + i + "/" + j))) {
                    l[l.Count - 1].Value.Add(new KeyValuePair<int, List<int>>(j, new List<int>()));
                    int k = 1;
                    while (true) {
                        if (File.Exists(Path.Combine(Path.Combine(Application.dataPath, "StreamingAssets"), "Levels/" + i + "/" + j + "/" + k + ".json"))) {
                            l[l.Count - 1].Value[l[l.Count - 1].Value.Count - 1].Value.Add(k);
                        }
                        else
                            break;
                        k++;
                    }
                }
            }
        }
    }
    string ljson = JsonUtility.ToJson(l);
    var lsr = File.CreateText("Assets/StreamingAssets/levels.json");
    lsr.WriteLine(ljson);
    lsr.Close();

你能帮我找到一种方法将其存储到文件中吗?

最佳答案

这听起来可能很难,但您确实必须使用 WWW 类才能从 Android 上的 StreamingAssets 访问您的文件。

而且你必须保留一个文件名列表,以便能够访问它们,因为似乎没有办法使用 File.ExistsDirectory.GetFiles

例子:

WWW www = new WWW(source);
yield return www;

if (string.IsNullOrEmpty(www.error))
{
    // make sure the destination directory exists
    File.WriteAllBytes(destination, www.bytes); 
}
else
{
    // error handling
}

您的源文件(基本)路径应如下所示: jar:file://"+ Application.dataPath + "!/assets/


来自文档:

Note that on Android, the files are contained within a compressed .jar file (which is essentially the same format as standard zip-compressed files). This means that if you do not use Unity’s WWW class to retrieve the file then you will need to use additional software to see inside the .jar archive and obtain the file.

来源:http://docs.unity3d.com/Manual/StreamingAssets.html

关于android - 在 Android 上访问 Unity StreamingAssets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35582074/

相关文章:

Android AudioRecord 和 MediaRecorder

java - 从 mainactivity 访问 searchview 查询文本

c++ - 如何使用 Windows 从 C++ 中的指定路径获取每个子目录和每个文件的大小?

java - 无法删除 NetBeans 中 servlet 中目录中的所有文件

android - Activity 内容转换不起作用

android - 如何将 FragmentFactory 与 FragmentStatePagerAdapter 一起使用

shell - 如何从查找 "type d"中排除此/当前/点文件夹

ios - Unity3d 使用内存 : load and unload prefabs

node.js - Nodejs + 连接 Assets 管理器 + uglifyJs

ruby-on-rails - 在 Assets 管道中需要树