效果:
先在菜单栏里面找到Tools/CheckPrefabLayers打开窗口
代码:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;public class CheckPrefabLayers : EditorWindow
{public int selectedLayerIndex = 0;void OnGUI(){string[] layerNames = UnityEditorInternal.InternalEditorUtility.layers;selectedLayerIndex = EditorGUILayout.Popup("检查层级:", selectedLayerIndex, layerNames);if (GUILayout.Button("开始查找")){CheckLayers();}}private void CheckLayers(){// 指定资源文件夹路径string folderPath = "Assets/Prefab";// 获取指定文件夹下所有预制体的路径string[] prefabPaths = Directory.GetFiles(folderPath, "*.prefab", SearchOption.AllDirectories);foreach (string prefabPath in prefabPaths){// 加载预制体GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);// 检查预制体的 Layer 层级if (prefab != null && prefab.layer ==selectedLayerIndex){Debug.Log($"Prefab {prefab.name} 在 {prefabPath} 有指定的层级");}}Debug.LogWarning("检测完毕");}[MenuItem("Tools/Check Prefab Layers")]static void Init(){// 创建窗口实例并显示CheckPrefabLayers window = (CheckPrefabLayers)EditorWindow.GetWindow(typeof(CheckPrefabLayers));window.Show();}
}