ฉันเขียนโค้ดเพื่อรับโฟลเดอร์ทั้งหมดภายในโฟลเดอร์ภายใน 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));
});
}
ฉันต้องเพิ่ม #if UNITY_STANDALONE_WIN สำหรับ windows เนื่องจาก '\'
จากนั้นสำหรับไฟล์ในโฟลเดอร์นั้นฉันเขียนสิ่งนี้:
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) และวนซ้ำจนกว่าจะไม่มีไฟล์อยู่
แต่ฉันจะต้องตรวจสอบด้วยว่ามีโฟลเดอร์สำหรับส่วนที่ 1 ของสคริปต์หรือไม่ และฉันไม่สามารถค้นหาวิธีตรวจสอบว่ามีโฟลเดอร์อยู่ใน 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();
คุณช่วยฉันหาวิธีจัดเก็บสิ่งนี้ลงในไฟล์ได้ไหม
Application.persistentDataPath
ในทุกกรณี - ทุกแพลตฟอร์ม - ทุกที่ - ไม่ว่าจะอย่างไรก็ตาม - person Fattie   schedule 23.02.2016