自定義窗口顯示文件夾結(jié)構(gòu)

假如我想在自定義窗口中顯示文件夾的結(jié)構(gòu),并且可以用鼠標(biāo)選擇對(duì)應(yīng)的文件。如下圖所示,文件夾的結(jié)構(gòu)我顯示在了Inspector里,鼠標(biāo)可以進(jìn)行選擇操作。


現(xiàn)在越來(lái)越懶,直接上代碼吧。。

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
 
[CustomEditor (typeof(UnityEditor.DefaultAsset))]
public class FolderInspector : Editor
{
 
    Data data;
    Data selectData;
    void OnEnable()
    {
        if (Directory.Exists (AssetDatabase.GetAssetPath (target))) {
            data = new Data ();
            LoadFiles (data,AssetDatabase.GetAssetPath (Selection.activeObject));
        }
 
    }
    public override void OnInspectorGUI ()
    {
 
        if (Directory.Exists (AssetDatabase.GetAssetPath (target))) 
        {
            GUI.enabled = true;
            EditorGUIUtility.SetIconSize (Vector2.one * 16);
            DrawData (data);
        }
    
    }
 
    void LoadFiles (Data data,string currentPath, int indent=0)
    {
        GUIContent content = GetGUIContent (currentPath);
 
        if (content != null) {
            data.indent = indent;
            data.content = content;
            data.assetPath = currentPath;
 
        }
 
        foreach (var path in Directory.GetFiles(currentPath)) {
            content = GetGUIContent (path);
            if (content != null) {
                Data child = new Data ();
                child.indent = indent + 1;
                child.content = content;
                child.assetPath = path;
                data.childs.Add (child);
            }
        }
 
 
        foreach (var path in Directory.GetDirectories(currentPath)) {
            Data childDir = new Data ();
            data.childs.Add (childDir);
            LoadFiles (childDir,path, indent + 1);
        }
    }
 
 
 
    void DrawData(Data data)
    {
        if (data.content != null) {
            EditorGUI.indentLevel = data.indent;
            DrawGUIData (data);
 
        }
        for (int i = 0; i < data.childs.Count; i++) {
            Data child = data.childs [i];
            if (child.content != null) {
                EditorGUI.indentLevel = child.indent;
                if(child.childs.Count>0)
                    DrawData (child);
                else
                    DrawGUIData (child);
            }
        }
    }
 
 
 
 
     void DrawGUIData (Data data)
    {
        GUIStyle style = "Label";
        Rect rt = GUILayoutUtility.GetRect (data.content, style);
        if (data.isSelected) {
            EditorGUI.DrawRect (rt, Color.gray);
        }
 
        rt.x += (16 * EditorGUI.indentLevel);
        if (GUI.Button (rt, data.content, style)) {
            if (selectData != null) {
                selectData.isSelected = false;
            }
            data.isSelected = true;
            selectData = data;
            Debug.Log (data.assetPath);
        }
    }
 
    GUIContent GetGUIContent (string path)
    {
        Object asset = AssetDatabase.LoadAssetAtPath (path, typeof(Object));  
        if (asset) {
            return new GUIContent (asset.name,  AssetDatabase.GetCachedIcon (path));
        }
        return null;
    }
 
    class Data
    {
        public bool isSelected = false;
        public int indent =0;
        public GUIContent content;
        public string assetPath;
        public List<Data> childs = new List<Data> ();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容