Inspector中列表文件排序问题

将Project中的文件全选拖到Inspector列表中会出现排序紊乱的现象
解决方法是在列表中重新排序,将此方法添加到原有脚本中即可

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ListPX : MonoBehaviour
{
    public List<Texture> ObjList;               //目标存储数组
    private Dictionary<int, Texture> ObjDir;    //临时存储字典,里面键值对对应的是图片名字和图片资源
    public string gtName = "Four Areas_";       //统一源文件的共同名字

    //快捷菜单
    [ContextMenu("PaiXu")] 
                   
    public void PaiXu()
    {
        //先把图片名字剔除成123456数字,使用name.Substring(index,length);
        ObjDir = new Dictionary<int, Texture>();
        ObjDir.Clear();
        for (int i = 0; i < ObjList.Count; i++)
        {
            int nameLength = gtName.Length;
            Debug.LogError(nameLength);
           
            string _name = ObjList[i].name.Substring(nameLength, ObjList[i].name.Length - nameLength);
            ObjDir.Add(int.Parse(_name), ObjList[i]);
          
            Debug.LogError(int.Parse(_name));
        }

        //利用之前存储在字典内的键值对进行排序,重新排序
        for (int i = 0; i < ObjList.Count; i++)
        {
            int min = 9999999;
            Texture texture = null;
            foreach (var item in ObjDir)
            {
                if (item.Key < min)
                {
                    min = item.Key;
                    texture = item.Value;
                }
            }

            if (texture == null)
                Debug.LogError("error null!");
            else
            {
                ObjDir.Remove(min);
                ObjList[i] = texture;
            }

        }
    }  
}

posted @ 2017-02-04 12:18  diviner_V  阅读(338)  评论(0)    收藏  举报