Reputation: 1389
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.
Upvotes: 1
Views: 1496
Reputation: 90872
I've set it all together and this works perfectly for me now:
Note: I commented out everything not relevant for the example (or what you didn't add to your code in the question)
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();
}
}
Result
Files on drive ordered by name
Files on drive ordered by creation date
result in Unity ordered by creation date
Upvotes: 2