Пример #1
0
    private void RefreshList()
    {
        List <SearchResult> pagedList = new List <SearchResult>();

        for (int i = (page - 1) * pageEach; i < page * pageEach && i < results.Count; i++)
        {
            pagedList.Add(results[i]);
        }
        if (objectsList == null)
        {
            objectsList = new ReorderableList(pagedList, typeof(T), false, true, true, true);
            objectsList.drawElementCallback = (rect, index, isActive, isFocused) =>
            {
                if (waitingRepaint)
                {
                    return;
                }
                int lineCount = 0;
                EditorGUI.LabelField(new Rect(rect.x, rect.y + lineCount * lineHeightSpace, rect.width, lineHeight), GetResult(objectsList.list[index]).remark);
                DrawElementOperator(GetResult(objectsList.list[index]).find, new Rect(rect.x, rect.y, rect.width - 80, lineHeight));
                if (GUI.Button(new Rect(rect.x + rect.width - 80, rect.y + lineCount * lineHeightSpace, 40, lineHeight), "移动"))
                {
selection:
                    string folder = EditorUtility.OpenFolderPanel("选择移动文件夹", latestFolder, "");
                    if (!string.IsNullOrEmpty(folder))
                    {
                        if (ZetanUtility.Editor.IsValidFolder(folder))
                        {
                            try
                            {
                                AssetDatabase.MoveAsset(AssetDatabase.GetAssetPath(GetResult(objectsList.list[index]).find), $"{ZetanUtility.Editor.ConvertToAssetsPath(folder)}/{ZetanUtility.Editor.GetFileName(AssetDatabase.GetAssetPath(GetResult(objectsList.list[index]).find))}");
                                Refresh();
                                latestFolder = folder;
                            }
                            catch
                            {
                                if (EditorUtility.DisplayDialog("移动失败", "请选择Assets目录或以下的文件夹。", "确定"))
                                {
                                    goto selection;
                                }
                            }
                        }
                        else
                        {
                            if (EditorUtility.DisplayDialog("提示", "请选择Assets目录或以下的文件夹。", "确定"))
                            {
                                goto selection;
                            }
                        }
                    }
                }
                if (GUI.Button(new Rect(rect.x + rect.width - 40, rect.y + lineCount * lineHeightSpace, 40, lineHeight), "编辑"))
                {
                    EditorUtility.OpenPropertyEditor(GetResult(objectsList.list[index]).find);
                }
                lineCount++;
                EditorGUI.LabelField(new Rect(rect.x, rect.y + lineCount * lineHeightSpace, rect.width / 2, lineHeight), "配置文件:");
                GUI.enabled = false;
                EditorGUI.ObjectField(new Rect(rect.x + rect.width - 280, rect.y + lineCount * lineHeightSpace, 280, lineHeight), GetResult(objectsList.list[index]).find, typeof(T), false);
                GUI.enabled = true;
                lineCount++;
                EditorGUI.LabelField(new Rect(rect.x, rect.y + lineCount * lineHeightSpace, rect.width, lineHeight), $"路径:{AssetDatabase.GetAssetPath(GetResult(objectsList.list[index]).find)}");
                lineCount++;
            };
            objectsList.drawElementBackgroundCallback = (rect, index, isActive, isFocused) =>
            {
                if (waitingRepaint)
                {
                    return;
                }
                EditorGUI.DrawRect(new Rect(rect.x, rect.y, rect.width, lineHeightSpace * 3), isActive ? new Color(0.5f, 0.5f, 0.5f, 0.5f) : (index % 2 != 0 ? Color.clear : new Color(1, 1, 1, 0.25f)));
            };
            objectsList.elementHeightCallback = (index) =>
            {
                return(lineHeightSpace * 3);
            };
            objectsList.onAddDropdownCallback = (rect, list) =>
            {
                if (waitingRepaint)
                {
                    return;
                }
                GenericMenu menu = new GenericMenu();
                MakeDropDownMenu(menu);
                menu.DropDown(rect);
            };
            objectsList.onRemoveCallback = (list) =>
            {
                if (waitingRepaint)
                {
                    return;
                }
                if (list.selectedIndices.Count < 1)
                {
                    return;
                }
                T obj = GetResult(list.list[list.selectedIndices[0]]).find;
                if (EditorUtility.DisplayDialog("警告", $"确定将{GetConfigurationName()} [{GetElementName(obj)}] 放入回收站吗?", "确定", "取消"))
                {
                    AssetDatabase.MoveAssetToTrash(AssetDatabase.GetAssetPath(obj));
                    AssetDatabase.Refresh();
                    Refresh();
                }
            };
            objectsList.onCanRemoveCallback = (list) =>
            {
                return(list.IsSelected(list.index));
            };
            objectsList.drawHeaderCallback = (rect) =>
            {
                if (waitingRepaint)
                {
                    return;
                }
                EditorGUI.LabelField(new Rect(rect.x, rect.y, rect.width, lineHeight), "配置列表");
                if (GUI.Button(new Rect(rect.x + 55, rect.y, 40, lineHeight), "刷新"))
                {
                    GUI.FocusControl(null);
                    Refresh();
                }
                GUI.SetNextControlName("keyWords");
                keyWords = EditorGUI.TextField(new Rect(rect.x + rect.width - 300, rect.y, 100, lineHeight), keyWords);
                if (string.IsNullOrEmpty(keyWords))
                {
                    searching = false;
                }
                if (!searching)
                {
                    if (string.IsNullOrEmpty(keyWords))
                    {
                        GUI.enabled = false;
                    }
                    if (GUI.Button(new Rect(rect.x + rect.width - 200, rect.y, 40, lineHeight), "查找"))
                    {
                        GUI.FocusControl(null);
                        Search();
                    }
                    if (string.IsNullOrEmpty(keyWords))
                    {
                        GUI.enabled = true;
                    }
                }
                else if (GUI.Button(new Rect(rect.x + rect.width - 200, rect.y, 40, lineHeight), "返回"))
                {
                    GUI.FocusControl(null);
                    keyWords  = string.Empty;
                    searching = false;
                    Refresh();
                }
                GUIStyle style = new GUIStyle()
                {
                    alignment = TextAnchor.MiddleRight
                };
                style.normal.textColor = GUI.contentColor;
                EditorGUI.LabelField(new Rect(rect.x + rect.width - 150, rect.y, 30, lineHeight), $"{page}/{maxPage}", style);
                if (GUI.Button(new Rect(rect.x + rect.width - 120, rect.y, 60, lineHeight), "上一页"))
                {
                    if (page > 1)
                    {
                        GUI.FocusControl(null);
                        page--;
                        Refresh();
                    }
                }
                if (GUI.Button(new Rect(rect.x + rect.width - 60, rect.y, 60, lineHeight), "下一页"))
                {
                    if (page * pageEach <= results.Count)
                    {
                        GUI.FocusControl(null);
                        page++;
                        Refresh();
                    }
                }
            };
            objectsList.drawNoneElementCallback = (rect) =>
            {
                EditorGUI.LabelField(new Rect(rect.x, rect.y, rect.width, lineHeight), searching ? $"没有找到相关{GetConfigurationName()}配置" : $"暂无{GetConfigurationName()}配置,请点击 + 新建");
            };
            objectsList.multiSelect = false;
        }
        else
        {
            objectsList.list = pagedList;
        }
        objectsList.displayAdd = !searching;
        objectsList.ClearSelection();
        waitingRepaint = true;