Ser*_*sky 6 directory android assets unity-game-engine unity5
我编写了一些代码来获取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));
});
}
Run Code Online (Sandbox Code Playgroud)
由于'\',我不得不为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));
});
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)
但是我找不到像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));
});
}
}
Run Code Online (Sandbox Code Playgroud)
我使用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
Run Code Online (Sandbox Code Playgroud)
问题更新
我制作了一个生成文件路径的脚本.路径始终相同:"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();
Run Code Online (Sandbox Code Playgroud)
你能帮我找到一种方法将它存储到文件中吗?
这可能听起来很难,但您必须使用WWW
该类从StreamingAssets
Android 访问您的文件.
你必须保持你的文件名的列表,以便能够访问它们,因为似乎没有办法使用类似File.Exists
或Directory.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
}
Run Code Online (Sandbox Code Playgroud)
您的源文件(基本)路径应如下所示:
jar:file://" + Application.dataPath + "!/assets/
请注意,在Android上,文件包含在压缩的.jar文件中(其格式与标准的zip压缩文件基本相同).这意味着如果您不使用Unity的WWW类来检索文件,那么您将需要使用其他软件来查看.jar存档内部并获取该文件.
资料来源:http://docs.unity3d.com/Manual/StreamingAssets.html