工具 在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<string> dirs = new List<string>();
GetDirs(selectPath, ref dirs);
}
//参数1 为要查找的总路径, 参数2 保存路径
private static void GetDirs(string dirPath, ref List<string> 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<UIDepth>() || prefab.transform.GetComponent<UIEffectDepth>())
{
Debug.Log("<color=#00EEEE>" + prefab + "</color>"+"用到了UIDepth或UIEffectDepth");
}
foreach(Transform child in prefab.GetComponentsInChildren<Transform>(true))
{
if (child.GetComponent<UIDepth>() || child.GetComponent<UIEffectDepth>())
{
Debug.Log("父物体名字:" +"<color=#00EEEE>" + prefab + "</color>" +"子物体名字为:" +"<color=#FF6A6A>" + child + "</color>");
}
}
}
}
if (Directory.GetDirectories(dirPath).Length > 0) //遍历所有文件夹
{
foreach (string path in Directory.GetDirectories(dirPath))
{
GetDirs(path, ref dirs);
}
}
}
}