private void DrawData(LayerNode data)
 {
     if (data.content != null)
     {
         EditorGUI.indentLevel = data.indent;
         DrawGUIData(data);
     }
     if (data.isExpanded)
     {
         for (int i = 0; i < data.childs.Count; i++)
         {
             LayerNode child = data.childs[i];
             if (child.content != null)
             {
                 EditorGUI.indentLevel = child.indent;
                 if (child.childs.Count > 0)
                 {
                     DrawData(child);
                 }
                 else
                 {
                     DrawGUIData(child);
                 }
             }
         }
     }
 }
        private static LayerNode LoadNodesFromDic(Dictionary <string, LayerNode> nodeDic)
        {
            if (nodeDic == null)
            {
                return(null);
            }
            LayerNode root = nodeDic[rootFolder];

            foreach (var item in nodeDic)
            {
                item.Value.parent = null;
                item.Value.childs.Clear();
            }
            foreach (var item in nodeDic)
            {
                foreach (var child in nodeDic)
                {
                    if (child.Key.Contains(item.Key + "/") && !child.Key.Replace(item.Key + "/", "").Contains("/"))
                    {
                        item.Value.childs.Add(child.Value);
                        child.Value.parent = item.Value;
                        if (root == null || AssetDatabase.GetAssetPath(root.layer).Contains(item.Key))
                        {
                            root = item.Value;
                        }
                    }
                }
            }
            return(root);
        }
 private static Dictionary <string, LayerNode> LoadDicFromObj(ConfigBuildObj buildObj)
 {
     if (buildObj.needBuilds == null)
     {
         return(null);
     }
     else
     {
         Dictionary <string, LayerNode> nodeDic = new Dictionary <string, LayerNode>();
         LayerNode node;
         if (LayerNode.TryCreateNode(rootFolder, out node))
         {
             nodeDic.Add(rootFolder, node);
         }
         foreach (var item in buildObj.needBuilds)
         {
             RetriveAddFolder(item.assetPath, nodeDic);
             if (!nodeDic.ContainsKey(item.assetPath))
             {
                 var       path = AssetDatabase.GetAssetPath(item.obj);
                 LayerNode layernode;
                 if (LayerNode.TryCreateNode(path, out layernode))
                 {
                     nodeDic.Add(item.assetPath, layernode);
                 }
             }
         }
         return(nodeDic);
     }
 }
 private void OnEnable()
 {
     _groupff        = new GUIContent(EditorGUIUtility.IconContent("IN foldout focus").image);
     _groupOn        = new GUIContent(EditorGUIUtility.IconContent("IN foldout focus on").image);
     script          = serializedObject.FindProperty("m_Script");
     localPath_prop  = serializedObject.FindProperty("localPath");
     menuName_prop   = serializedObject.FindProperty("menuName");
     targetPath_prop = serializedObject.FindProperty("targetPath");
     ReFelsh((target as ConfigBuildObj).needBuilds);
     nodeDic  = LoadDicFromObj((ConfigBuildObj)target);
     rootNode = LoadNodesFromDic(nodeDic);
 }
        private static void RetriveAddFolder(string assetPath, Dictionary <string, LayerNode> nodeDic)
        {
            var folder = assetPath.Remove(assetPath.LastIndexOf("/"));

            if (folder != assetPath && !nodeDic.ContainsKey(folder))
            {
                LayerNode layernode;
                if (LayerNode.TryCreateNode(folder, out layernode))
                {
                    nodeDic.Add(folder, layernode);
                }
            }
            if (folder.Contains("/"))
            {
                RetriveAddFolder(folder, nodeDic);
            }
        }
        private void AddGroupOfObject(params string[] paths)
        {
            foreach (var item in paths)
            {
                var assetPath = item;
                RetriveObject(assetPath, (x) =>
                {
                    RetriveAddFolder(assetPath, nodeDic);

                    assetPath = AssetDatabase.GetAssetPath(x);

                    if (!nodeDic.ContainsKey(assetPath))
                    {
                        nodeDic.Add(assetPath, new LayerNode(assetPath));
                    }
                });
            }

            rootNode = LoadNodesFromDic(nodeDic);
        }
        private void DrawGUIData(LayerNode data)
        {
            GUIStyle style      = "Label";
            var      pointWidth = 10;
            Rect     rt         = GUILayoutUtility.GetRect(300, EditorGUIUtility.singleLineHeight);

            rt = new Rect(rt.x, rt.y, rt.width, EditorGUIUtility.singleLineHeight);
            var offset = (16 * EditorGUI.indentLevel);
            var srect  = new Rect(rt.x + offset, rt.y, rt.width - offset - pointWidth, EditorGUIUtility.singleLineHeight);

            if (Event.current != null && srect.Contains(Event.current.mousePosition) &&
                Event.current.button == 0 && Event.current.type <= EventType.mouseUp)
            {
                EditorGUIUtility.PingObject(data.layer.GetInstanceID());
            }

            var selected = EditorGUI.Toggle(srect, data.selected, style);

            if (selected != data.selected)
            {
                data.Select(selected);
            }


            if (data.selected)
            {
                EditorGUI.DrawRect(srect, new Color(1, 1, 1, 0.1f));
            }
            if (data.isFolder)
            {
                var btnRect = new Rect(rt.x + offset - pointWidth * 2, rt.y, pointWidth * 2, rt.height);
                if (GUI.Button(btnRect, data.isExpanded ? _groupOn : _groupff, style))
                {
                    data.Expland(!data.isExpanded);
                }
            }

            EditorGUI.LabelField(rt, data.content);
        }
        public static bool TryCreateNode(string path, out LayerNode node)
        {
            node = null;
            var obj = AssetDatabase.LoadAssetAtPath(path, typeof(Object));

            if (obj == null)
            {
                return(false);
            }

            var           isFolder = ProjectWindowUtil.IsFolder(obj.GetInstanceID());
            AssetImporter asset    = AssetImporter.GetAtPath(path);

            if (!isFolder && string.IsNullOrEmpty(asset.assetBundleName))
            {
                return(false);
            }
            else
            {
                node = new LayerNode(path);
                return(true);
            }
        }
        //绘制添加,删除,重置等功能
        private void DrawHeadTools()
        {
            using (var hor = new EditorGUILayout.HorizontalScope())
            {
                if (GUILayout.Button(new GUIContent("+", "从Project添加"), GUILayout.Width(20)))
                {
                    if (Selection.activeObject == null)
                    {
                        return;
                    }

                    var assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);

                    AddGroupOfObject(assetPath);
                }
                if (GUILayout.Button(new GUIContent("-", "移除选中"), GUILayout.Width(20)))
                {
                    var selectedNode = new List <string>();
                    foreach (var item in nodeDic)
                    {
                        if (item.Value.selected && item.Key != rootFolder)
                        {
                            selectedNode.Add(item.Key);
                        }
                    }
                    foreach (var item in selectedNode)
                    {
                        nodeDic.Remove(item);
                    }
                    rootNode = LoadNodesFromDic(nodeDic);
                }
                //导入相关
                if (GUILayout.Button(new GUIContent("~", "导入关联资源"), GUILayout.Width(20)))
                {
                    var needAdd = new List <string>();
                    foreach (var item in nodeDic)
                    {
                        if (item.Value.selected)
                        {
                            var childs = AssetDatabase.GetDependencies(item.Value.assetPath, true);
                            foreach (var child in childs)
                            {
                                if (!needAdd.Contains(child))
                                {
                                    needAdd.Add(child);
                                }
                            }
                        }
                    }
                    foreach (var item in needAdd)
                    {
                        RetriveAddFolder(item, nodeDic);
                        if (!nodeDic.ContainsKey(item))
                        {
                            nodeDic.Add(item, new LayerNode(item));
                        }
                    }
                    rootNode = LoadNodesFromDic(nodeDic);
                }
                //选中所有引用
                if (GUILayout.Button(new GUIContent("&", "关联资源"), GUILayout.Width(20)))
                {
                    var needSelect = new List <string>();
                    foreach (var item in nodeDic)
                    {
                        if (item.Value.selected)
                        {
                            var childs = AssetDatabase.GetDependencies(item.Value.assetPath, true);
                            foreach (var child in childs)
                            {
                                if (!needSelect.Contains(child))
                                {
                                    needSelect.Add(child);
                                }
                            }
                        }
                    }
                    foreach (var item in nodeDic)
                    {
                        item.Value.Select((needSelect.Contains(item.Key)));
                    }
                }
                //刷新
                if (GUILayout.Button(new GUIContent("*", "刷新重置"), GUILayout.Width(20)))
                {
                    var buildObj = target as ConfigBuildObj;
                    nodeDic  = LoadDicFromObj(buildObj);
                    rootNode = LoadNodesFromDic(nodeDic);
                }

                if (GUILayout.Button(new GUIContent("s", "保存配制"), GUILayout.Width(20)))
                {
                    var buildObj = target as ConfigBuildObj;
                    buildObj.needBuilds.Clear();
                    StoreLayerNodeToAsset(nodeDic, buildObj);
                    EditorUtility.SetDirty(buildObj);
                }
            }
        }