unity中检索当前文件夹下所有物体的某个组件工具

工具 在project视图中创建中点击选项  检索当前文件夹下所有物体的某个组件  代码用例是检索用到了UIDepth或UIEffectDepth

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class GetComponenTools : MonoBehaviour {
	// 在菜单来创建 选项 , 点击该选项执行搜索代码
    [MenuItem("Assets/GetComponentTools/读取预制体包括子孙物体的组件")]
    static void CheckSceneSetting()
    {
		string guid = Selection.assetGUIDs[0];
        var selectPath = AssetDatabase.GUIDToAssetPath(guid);
        List dirs = new List();
        GetDirs(selectPath, ref dirs);
    }
    //参数1 为要查找的总路径, 参数2 保存路径
    private static void GetDirs(string dirPath, ref List dirs)
    {
        foreach (string path in Directory.GetFiles(dirPath))
        {
            // Debug.Log(path);
            //获取所有文件夹中包含后缀为 .prefab 的路径
            if (System.IO.Path.GetExtension(path) == ".prefab" && path.IndexOf(@"UI\Slots") == -1)
            {
                dirs.Add(path.Substring(path.IndexOf("Asset")));
				string final_path = path.Substring(path.IndexOf("Asset")).Replace(@"\", "/");
				GameObject prefab = AssetDatabase.LoadAssetAtPath(final_path, typeof(System.Object)) as GameObject;
				// Debug.Log(final_path);
				if (prefab.transform.GetComponent() || prefab.transform.GetComponent())
				{
                    Debug.Log("" + prefab + ""+"用到了UIDepth或UIEffectDepth");
				}
                foreach(Transform child in prefab.GetComponentsInChildren(true))
                {
                    if (child.GetComponent() || child.GetComponent())
                    {
                        Debug.Log("父物体名字:" +"" + prefab + "" +"子物体名字为:" +"" + child + "");
                    }
                }
            }
        }         
        if (Directory.GetDirectories(dirPath).Length > 0)  //遍历所有文件夹
        {
            foreach (string path in Directory.GetDirectories(dirPath))
            {
                GetDirs(path, ref dirs);
            }
        }
    }

}

 

你可能感兴趣的:(C#,源码,unity)