本文转载自:https://blog.youkuaiyun.com/qq_26999509/article/details/80794973
通过Image Name快速反查Prefab
开发思路
1、获取到工程中所有Prefab
2、获取Prefab的所有Image和RawImage
3、检测Prefab下是否存在指定名称的Image
编辑器示意图
编辑器包括三个部分:
1、搜索框
2、预制体列表
3、预制体层级
Code
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using UnityEngine.UI;
public class FindPrefabByImage : EditorWindow {
private static string assetPath;
private static List<string> prefabPaths = new List<string> ();
private bool initialized = false;
private SearchField searchField;
private string searchStr;
Rect toolbarRect {
get { return new Rect (20f, 10f, position.width - 40f, 20f); }
}
Rect searchResultViewRect {
get { return new Rect (20f, 40f, (position.width - 40) / 2, position.height - 40f); }
}
Rect selectPrefabHierarchyViewRect {
get { return new Rect (position.width / 2, 40, (position.width - 40) / 2, position.height - 40f); }
}
Vector2 searchResultScrollViewPos = Vector2.zero;
[SerializeField]
TreeViewState treeViewState;
PrefabTreeView prefabTreeView;
private GameObject curSelectPrefab;
void OnEnable () {
if (null == treeViewState)
treeViewState = new TreeViewState ();
}
[MenuItem ("CommonTools/图片反查预制体引用工具")]
public static FindPrefabByImage GetWindow () {
prefabPaths.Clear ();
assetPath = Application.dataPath;
GetFiles (new DirectoryInfo (assetPath), "*.prefab", ref prefabPaths);
var window = GetWindow<FindPrefabByImage> ();
window.titleContent = new GUIContent ("FindPrefab");
window.Focus ();
window.Repaint ();
return window;
}
public static void GetFiles (DirectoryInfo directory, string pattern, ref List<string> fileList) {
if (directory != null && directory.Exists && !string.IsNullOrEmpty (pattern)) {
try {
foreach (FileInfo info in directory.GetFiles (pattern)) {
string path = info.FullName.ToString ();
fileList.Add (path.Substring (path.IndexOf ("Assets")));
}
} catch (System.Exception) {
throw;
}
foreach (DirectoryInfo info in directory.GetDirectories ()) {
GetFiles (info, pattern, ref fileList);
}
}
}
/// <summary>
/// 检测预制体是否包含Image组件
/// </summary>
/// <param name="prefab">检测的预制体</param>
/// <param name="imageName">Image名称,可为空</param>
/// <returns>如果Image名称为空且预制体包含Image组件,或者Image不为空预制体包含该指定Image,则返回true,否则返回false</returns>
public static bool CheckPrefabHasImage (GameObject prefab, string imageName = null) {
if (!string.IsNullOrEmpty (imageName))
imageName = imageName.ToLower ();
if (null == prefab) return false;
Image[] images = prefab.GetComponentsInChildren<Image> (true);
if (null != images && images.Length > 0) {
if (string.IsNullOrEmpty (imageName)) {
return true;
} else {
for (int i = 0; i < images.Length; i++) {
if (null != images[i] && images[i].sprite != null && images[i].sprite.name.ToLower() == imageName) {
return true;
}
}
}
}
RawImage[] rawImages = prefab.GetComponentsInChildren<RawImage> (true);
if (null != rawImages && rawImages.Length > 0) {
if (string.IsNullOrEmpty (imageName)) {
return true;
} else {
for (int i = 0; i < rawImages.Length; i++) {
if (null != rawImages[i] && null != rawImages[i].texture && rawImages[i].texture.name.ToLower() == imageName) {
return true;
}
}
}
}
return false;
}
private void InitIfNeeded () {
if (!initialized) {
if (null == searchField)
searchField = new SearchField ();
initialized = true;
}
}
void OnGUI () {
InitIfNeeded ();
DoSearchField ();
DoSearchResultView ();
DoSelectPrefabHierarchyVieww ();
}
private void DoSearchField () {
searchStr = searchField.OnGUI (toolbarRect, searchStr);
}
private void DoSearchResultView () {
GUILayout.BeginArea (searchResultViewRect);
searchResultScrollViewPos = GUILayout.BeginScrollView (searchResultScrollViewPos);
for (int i = 0; i < prefabPaths.Count; i++) {
GameObject gameObj = AssetDatabase.LoadAssetAtPath<GameObject> (@prefabPaths[i]);
if (gameObj != null)
{
bool result = CheckPrefabHasImage (gameObj,searchStr);
if (result)
{
if (GUILayout.Button (prefabPaths[i]))
{
curSelectPrefab = gameObj;
}
}
}
}
GUILayout.EndScrollView ();
GUILayout.EndArea ();
}
private void DoSelectPrefabHierarchyVieww () {
GUILayout.BeginArea (selectPrefabHierarchyViewRect);
if (null != curSelectPrefab) {
Rect rect = GUILayoutUtility.GetRect (0, 100000, 0, 100000);
prefabTreeView = new PrefabTreeView (treeViewState, curSelectPrefab);
prefabTreeView.OnGUI (rect);
prefabTreeView.ExpandAll ();
} else {
}
GUILayout.EndArea ();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
using System.Collections;
using System.Collections.Generic;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
public class GameObjectTreeViewItem {
public TreeViewItem viewItem;
public Transform treeViewTrans;
public Transform parent;
public List<Transform> childs = new List<Transform> ();
public bool IsChild (Transform tran) {
if (null == tran) return false;
if (tran.parent == treeViewTrans) return true;
return false;
}
public void AddChildTransform (Transform childTransform) {
if (null != childTransform)
childs.Add (childTransform);
}
public bool HasChildTransform (Transform tran) {
if (null == tran) return false;
return childs.Contains (tran);
}
}
public class PrefabTreeView : TreeView {
private Transform[] childs;
private GameObjectTreeViewItem[] items;
public PrefabTreeView (TreeViewState state, GameObject go) : base (state) {
treeGameObject = go;
childs = treeGameObject.GetComponentsInChildren<Transform> (true);
items = new GameObjectTreeViewItem[childs.Length];
#region Init Items Parent
for (int i = 0; i < childs.Length; i++) {
items[i] = new GameObjectTreeViewItem ();
items[i].treeViewTrans = childs[i];
if (childs[i].parent != null)
items[i].parent = childs[i].parent;
}
#endregion
#region Init Child Transform
for (int i = 0; i < childs.Length; i++) {
for (int j = 0; j < items.Length; j++) {
if (items[j].IsChild (childs[i])) {
items[j].AddChildTransform (childs[i]);
break;
}
}
}
#endregion
Reload ();
}
private GameObject treeGameObject;
private GameObjectTreeViewItem GetGameObjectTreeViewItem (GameObjectTreeViewItem[] items, Transform trans) {
if (null == items || items.Length == 0 || trans == null)
return null;
for (int i = 0; i < items.Length; i++) {
if (items[i] != null && items[i].treeViewTrans == trans)
return items[i];
}
return null;
}
private GameObjectTreeViewItem GetGameObjectTreeViewItem (GameObjectTreeViewItem[] items, TreeViewItem viewItem) {
if (null == items || items.Length == 0 || viewItem == null)
return null;
for (int i = 0; i < items.Length; i++) {
if (null != items && items[i].viewItem == viewItem)
return items[i];
}
return null;
}
protected override TreeViewItem BuildRoot () {
TreeViewItem root = new TreeViewItem { id = 0, depth = -1, displayName = "root" };
TreeViewItem[] childItems = new TreeViewItem[childs.Length];
for (int i = 0; i < childs.Length; i++) {
childItems[i] = new TreeViewItem { id = i + 1, displayName = childs[i].name };
GameObjectTreeViewItem item = GetGameObjectTreeViewItem (items, childs[i]);
if (null != item) {
item.viewItem = childItems[i];
}
}
root.AddChild (childItems[0]);
for (int i = 0; i < childItems.Length; i++) {
GameObjectTreeViewItem temp = GetGameObjectTreeViewItem (items, childItems[i]);
if (temp != null && temp.childs != null) {
for (int j = 0; j < temp.childs.Count; j++) {
GameObjectTreeViewItem childItem = GetGameObjectTreeViewItem (items, temp.childs[j]);
if (childItem != null && childItem.viewItem != null) {
childItems[i].AddChild (childItem.viewItem);
}
}
}
}
SetupDepthsFromParentsAndChildren (root);
return root;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
代码中用到了TreeView的知识,请自行补充,后续会更新关系TreeView的知识点
Code下载
https://github.com/JingFengJi/FindPrefabByImage
以上知识分享,如有错误,欢迎指出,共同学习,共同进步。
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
</div>