Trying to List a number files according to extension *.json. Instantiating the prefab under a parent but it wont come in an order.Is there a way to refresh the list or arrange them according in an ascending order way.Aim is to Load and delete the files.How to arrange the files 1,2,3,4,5...and if there are 10 files the last saved file should come down there in the 10 th place under a parent?
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].);
}
Initially when I save the file to .json and click the button for Listmap(To show the filelist - shown in Screenshot).It is showing the index number 5 two times.Also the last file I saved was named "00000.json",but it became the first file.That is after I save it(the file list) is not getting updated,When I click Listmap the files are showing same index number mulitiple times.Seems like it is not refreshing not sure.Another problem is the last saved file is coming on the top.