做網(wǎng)站賣廣告掙幾百萬建站模板平臺
目錄
1.功能概述
2.完整代碼
3. 實現(xiàn)原理
4. 使用預(yù)覽
5.新增優(yōu)化版本
在Unity項目開發(fā)過程中,管理和維護資源之間的引用關(guān)系是至關(guān)重要的。當(dāng)然我們項目也是需要這個功能 畢竟項目大了之后查找資源引用還是交給?資源引用查找器 比較好。
1.功能概述
資源引用查找器允許開發(fā)者選擇一個目標(biāo)資源,并在整個項目中查找引用了該資源的其他資源。其主要功能包括:
- 在Unity編輯器菜單欄下添加一個名為"Resource Reference Finder"的菜單項。
- 提供一個可調(diào)整大小的窗口,用于選擇目標(biāo)資源和顯示引用結(jié)果。
- 通過點擊"Search"按鈕來觸發(fā)搜索過程,查找所有引用了目標(biāo)資源的Prefab文件,并將結(jié)果展示在窗口中。
2.完整代碼
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.IO;public class ResourceReferenceFinder : EditorWindow
{[MenuItem("Assets/Resource Reference Finder")]static void SearchReference(){GetWindow<ResourceReferenceFinder>("Resource Reference Finder");}private static Object targetResource;private List<Object> referencingAssets = new List<Object>();private Vector2 scrollPosition;private void OnGUI(){// 顯示資源選擇字段和搜索按鈕EditorGUILayout.BeginHorizontal();GUILayout.Label("Select Target Resource:", GUILayout.Width(150));targetResource = EditorGUILayout.ObjectField(targetResource, typeof(Object), true, GUILayout.Width(200));if (GUILayout.Button("Search", GUILayout.Width(100))){ReferenceFinder();}EditorGUILayout.EndHorizontal();// 滾動視圖開始scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);EditorGUILayout.BeginVertical();// 顯示搜索結(jié)果for (int i = 0; i < referencingAssets.Count; i++){EditorGUILayout.ObjectField(referencingAssets[i], typeof(Object), true, GUILayout.Width(300));}EditorGUILayout.EndVertical();EditorGUILayout.EndScrollView();// 滾動視圖結(jié)束}// 查找引用private void ReferenceFinder(){referencingAssets.Clear();// 檢查是否選擇了資源if (targetResource == null){Debug.LogWarning("Please select a resource to search for references.");return;}// 獲取選擇資源的 GUIDstring assetPath = AssetDatabase.GetAssetPath(targetResource);string assetGuid = AssetDatabase.AssetPathToGUID(assetPath);// 遍歷項目中所有 Prefab 文件string[] guids = AssetDatabase.FindAssets("t:Prefab", new[] { "Assets" });int length = guids.Length;for (int i = 0; i < length; i++){string filePath = AssetDatabase.GUIDToAssetPath(guids[i]);EditorUtility.DisplayCancelableProgressBar("Checking", filePath, (float)i / length);// 讀取 Prefab 文件內(nèi)容,檢查是否包含選擇資源的 GUIDstring content = File.ReadAllText(filePath);if (content.Contains(assetGuid)){// 如果包含,將該 Prefab 添加到結(jié)果列表中Object referencingAsset = AssetDatabase.LoadAssetAtPath(filePath, typeof(Object));referencingAssets.Add(referencingAsset);}}// 清除進度條EditorUtility.ClearProgressBar();}
}
3. 實現(xiàn)原理
- 使用Unity編輯器提供的
MenuItem
特性,在菜單欄下添加了一個自定義的菜單項,用于觸發(fā)資源引用查找器窗口的顯示。 - 創(chuàng)建了一個繼承自
EditorWindow
的窗口類,實現(xiàn)了GUI繪制和資源引用搜索的邏輯。 - 在GUI中,通過
ObjectField
和Button
控件實現(xiàn)了目標(biāo)資源的選擇和搜索按鈕的功能。 - 使用
AssetDatabase
類來訪問項目中的資源,并通過FindAssets
方法查找所有Prefab文件。 - 讀取Prefab文件的內(nèi)容,檢查是否包含目標(biāo)資源的GUID,如果是,則將該Prefab添加到引用結(jié)果列表中。
4. 使用預(yù)覽
查找Prefab引用
查找圖片引用
5.新增優(yōu)化版本
新增優(yōu)化版本右鍵直接選中需要查找的資源? 直接省略繁瑣步驟 完整代碼如下
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.IO;public class ResourceReferenceFinder : EditorWindow
{private List<Object> referencingAssets = new List<Object>();private Vector2 scrollPosition;[MenuItem("Assets/ZYT ASSETS/Find References", true)]private static bool ValidateSearchReference(){// 只在選中了對象且不是文件夾時才顯示菜單項return Selection.activeObject != null && !AssetDatabase.IsValidFolder(AssetDatabase.GetAssetPath(Selection.activeObject));}[MenuItem("Assets/ZYT ASSETS/Find References")]private static void SearchReference(){// 創(chuàng)建并打開資源引用查找窗口if (Selection.activeObject != null && !AssetDatabase.IsValidFolder(AssetDatabase.GetAssetPath(Selection.activeObject))){GetWindow<ResourceReferenceFinder>("Resource Reference Finder").ReferenceFinder(Selection.activeObject);}}private void OnGUI(){// 顯示搜索結(jié)果EditorGUILayout.LabelField("Search Results:");// 滾動視圖開始scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);EditorGUILayout.BeginVertical();// 顯示搜索結(jié)果for (int i = 0; i < referencingAssets.Count; i++){EditorGUILayout.ObjectField(referencingAssets[i], typeof(Object), true, GUILayout.Width(300));}EditorGUILayout.EndVertical();EditorGUILayout.EndScrollView();// 滾動視圖結(jié)束}// 查找引用private void ReferenceFinder(Object targetResource){referencingAssets.Clear();// 獲取選擇資源的 GUIDstring assetPath = AssetDatabase.GetAssetPath(targetResource);string assetGuid = AssetDatabase.AssetPathToGUID(assetPath);// 遍歷項目中所有 Prefab 文件string[] guids = AssetDatabase.FindAssets("t:Prefab", new[] { "Assets" });int length = guids.Length;for (int i = 0; i < length; i++){string filePath = AssetDatabase.GUIDToAssetPath(guids[i]);EditorUtility.DisplayCancelableProgressBar("Checking", filePath, (float)i / length);// 讀取 Prefab 文件內(nèi)容,檢查是否包含選擇資源的 GUIDstring content = File.ReadAllText(filePath);if (content.Contains(assetGuid)){// 如果包含,將該 Prefab 添加到結(jié)果列表中Object referencingAsset = AssetDatabase.LoadAssetAtPath(filePath, typeof(Object));referencingAssets.Add(referencingAsset);}}// 清除進度條EditorUtility.ClearProgressBar();}
}
?使用方法??
如果未選中資源則是如下狀況 工具是沒法使用的
下圖是現(xiàn)在修改后的界面