如何统一安排父母下的实例化预制件的顺序?

尝试根据扩展名 *.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 时,文件多次显示相同的索引号。似乎它没有刷新不确定。另一个问题是最后保存的文件位于顶部。

http://img.mukewang.com/638c2c7e000129d705290594.jpg

http://img4.mukewang.com/638c2c860001b2ce06810595.jpg

aluckdog
浏览 72回答 1
1回答

胡子哥哥

我已经把它们放在一起了,现在这对我来说非常有用:注意:我注释掉了与示例无关的所有内容(或者您没有添加到问题代码中的内容)public class FilesExample : MonoBehaviour{&nbsp; &nbsp; // Start is called before the first frame update&nbsp; &nbsp; private void Start()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ListMap();&nbsp; &nbsp; }&nbsp; &nbsp; public static FileSystemInfo[] info;&nbsp; &nbsp; public void ListMap()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* If you already called this before you already have child objects&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* So I would destroy the old ones before instantiating new ones&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; //foreach(Transform child in Parentcontent)&nbsp; &nbsp; &nbsp; &nbsp; foreach(Transform child in transform)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Destroy(child.gameObject);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //panellist.SetActive(true);&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* preferred to use streamingAssetsPath for testing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; //var mainpath = Application.persistentDataPath;&nbsp; &nbsp; &nbsp; &nbsp; var mainpath = Application.streamingAssetsPath;&nbsp; &nbsp; &nbsp; &nbsp; var dir = new DirectoryInfo(mainpath);&nbsp; &nbsp; &nbsp; &nbsp; info = dir.GetFileSystemInfos("*.json").OrderBy(i => i.CreationTime).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 1; i <= info.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Instead of instantiating I simply created new empty objects&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* just as example&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var lisobj = Instantiate(prefabpanellist);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var lisobj = new GameObject(i + " " + info[i - 1].Name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //lisobj.transform.SetParent(Parentcontent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lisobj.transform.SetParent(transform);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Though I'm sure this should already have the correct order&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // you could still do SetLastSibling to move the&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // created/instantiated object to the bottom&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lisobj.transform.SetAsLastSibling();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //number.text = i.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //mapnamedb.text = info[i - 1].Name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* NOTE: I would simply do the buttons thing in the same&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; &nbsp;loop. That saves you a lot of storing and accessing lists&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var index = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var button = lisobj.GetComponentInChildren<Button>(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //button.onClick.AddListener(() => Deleteinformation(index));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void Deleteinformation(int index)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Index is " + index);&nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Path is " + info[index].FullName);&nbsp; &nbsp; &nbsp; &nbsp; File.Delete( info[index].FullName);&nbsp; &nbsp; &nbsp; &nbsp; // update the scene list&nbsp; &nbsp; &nbsp; &nbsp; ListMap();&nbsp; &nbsp; }}结果驱动器上的文件按名称排序驱动器上的文件按创建日期排序导致 Unity 按创建日期排序
打开App,查看更多内容
随时随地看视频慕课网APP