尝试根据扩展名* .json列出数字文件。在父对象下实例化预制件,但不会按顺序进行。有没有一种方法可以刷新列表或按照升序排列它们。目的是加载和删除文件。如何排列文件1,2, 3,4,5 ...如果有10个文件,最后保存的文件应该放在父目录下的第10位吗?
Dictionary<int, Button> BtnList = new Dictionary<int, Button>();
public static FileInfo[] info;
GameObject lisobj;
public void ListMap()
{
panellist.SetActive(true);
string mainpath = Application.persistentDataPath;
DirectoryInfo dir = new DirectoryInfo(mainpath);
info = dir.GetFiles("*.json");
for(int i = 1;i<=info.Length;i++)
{
lisobj = Instantiate(prefabpanellist);
lisobj.transform.SetParent(Parentcontent);
number.text = i.ToString();
mapnamedb.text =info[i-1].Name;
var button = lisobj.GetComponentInChildren<Button>();
BtnList.Add(i,button);
}
lisobj.transform.SetParent(Parentcontent);
Dictionary<int, Button>.ValueCollection values = BtnList.Values;
foreach (Button btn in values)
{
btn.onClick.AddListener(() => Deleteinformation());
}
}
public void Deleteinformation()
{
var b= UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.GetComponent<Button>();
var mykey=BtnList.FirstOrDefault(x=>x.Value==b).Key;
Debug.Log("Items are" + mykey);
string mainpath = Application.persistentDataPath;
Debug.Log("Name is " + info[mykey - 1].Name);
//File.Delete(mainpath + info[mykey-1].);
}
最初,当我将文件保存到.json并单击Listmap的按钮(以显示文件列表-如屏幕快照中所示)。它两次显示索引号5。我保存的最后一个文件名为“ 00000”。 json”,但它成为第一个文件。也就是我保存它(文件列表)后没有更新,当我单击Listmap时,这些文件多次显示相同的索引号。似乎不确定是否刷新。另一个问题是最后保存的文件位于顶部。
答案 0 :(得分:1)
我将它们放在一起,现在对我来说非常合适:
注意:我注释掉了与示例无关的所有内容(或您未在问题中添加到代码中的内容)
public class FilesExample : MonoBehaviour
{
// Start is called before the first frame update
private void Start()
{
ListMap();
}
public static FileSystemInfo[] info;
public void ListMap()
{
/*
* If you already called this before you already have child objects
* So I would destroy the old ones before instantiating new ones
*/
//foreach(Transform child in Parentcontent)
foreach(Transform child in transform)
{
Destroy(child.gameObject);
}
//panellist.SetActive(true);
/*
* preferred to use streamingAssetsPath for testing
*/
//var mainpath = Application.persistentDataPath;
var mainpath = Application.streamingAssetsPath;
var dir = new DirectoryInfo(mainpath);
info = dir.GetFileSystemInfos("*.json").OrderBy(i => i.CreationTime).ToArray();
for (var i = 1; i <= info.Length; i++)
{
/*
* Instead of instantiating I simply created new empty objects
* just as example
*/
//var lisobj = Instantiate(prefabpanellist);
var lisobj = new GameObject(i + " " + info[i - 1].Name);
//lisobj.transform.SetParent(Parentcontent);
lisobj.transform.SetParent(transform);
// Though I'm sure this should already have the correct order
// you could still do SetLastSibling to move the
// created/instantiated object to the bottom
lisobj.transform.SetAsLastSibling();
//number.text = i.ToString();
//mapnamedb.text = info[i - 1].Name;
/*
* NOTE: I would simply do the buttons thing in the same
* loop. That saves you a lot of storing and accessing lists
*/
//var index = i;
//var button = lisobj.GetComponentInChildren<Button>(true);
//button.onClick.AddListener(() => Deleteinformation(index));
}
}
public void Deleteinformation(int index)
{
Debug.Log("Index is " + index);
Debug.Log("Path is " + info[index].FullName);
File.Delete( info[index].FullName);
// update the scene list
ListMap();
}
}
结果
按名称排序的驱动器上的文件
按创建日期排序的驱动器上的文件
Unity结果按创建日期排序