当前位置: 首页 > news >正文

Unity 使用Editor工具查找 Prefab 中的指定脚本

在 Unity 项目中,随着项目规模的扩大和 Prefab 数量的增加,管理和定位 Prefab 中的脚本变得更加复杂。为了提高开发效率,所以需要编写一个自定义的 Unity Editor 工具,帮助查找某个 Prefab 中是否使用了指定的脚本。本文将介绍如何通过 Unity Editor API 来实现一个 "Prefab Script Finder" 工具,该工具可以在 Prefab 模式下帮助查找附带特定脚本的对象,并直接在编辑器中进行选择和高亮。

一、工具实现目标

在本次实现中,我们的目标是构建一个简单且实用的 Unity Editor 工具,通过以下几个步骤实现:

  • 允许用户从编辑器中选择某个 MonoScript,即一个脚本文件。
  • 查找当前正在编辑的 Prefab 中是否有 GameObject 附带这个脚本。
  • 将找到的对象列出来,并允许开发者在结果列表中点击对象以高亮和定位到该对象。

这个工具非常适合于 Prefab 数量庞大、对象和脚本复杂度较高的项目,有助于快速定位特定组件,提高开发效率。

二、效果

三、代码实现

首先,可以使用 Unity 的 EditorWindow 创建一个自定义的编辑器窗口。在该窗口中,用户可以选择一个脚本文件并点击按钮来查找 Prefab 中附带该脚本的对象。接下来,我们通过递归的方式遍历 Prefab 的所有子对象,检查是否附带指定的脚本。如果找到了,便将这些对象显示在结果列表中。以下是完整的代码实现:

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using UnityEditor.SceneManagement;public class PrefabScriptFinder : EditorWindow
{private MonoScript selectedScript;private string scriptTypeName = "";private List<(GameObject obj, string prefabPath)> foundObjects = new List<(GameObject, string)>();private Vector2 scrollPos;[MenuItem("GameObject/ZYT ASSETS/Prefab Script Finder", false, 100)]public static void ShowWindow(){GetWindow<PrefabScriptFinder>("Prefab Script Finder");}private void OnGUI(){selectedScript = (MonoScript)EditorGUILayout.ObjectField("Script Type", selectedScript, typeof(MonoScript), false);if (selectedScript != null){if (selectedScript.GetClass() != null){scriptTypeName = selectedScript.GetClass().FullName;}else{scriptTypeName = "";}}else{scriptTypeName = "";}if (GUILayout.Button("Find in Prefab")){if (string.IsNullOrEmpty(scriptTypeName)){EditorUtility.DisplayDialog("No Script Selected", "Please select a valid script type.", "OK");return;}FindInPrefab();}if (foundObjects.Count > 0){GUILayout.Space(10);GUILayout.Label("Found Objects:", EditorStyles.boldLabel);scrollPos = GUILayout.BeginScrollView(scrollPos);EditorGUILayout.BeginVertical();// 显示找到的对象列表foreach (var (obj, prefabPath) in foundObjects){// 对象点击可以选中EditorGUI.BeginChangeCheck();GameObject selectedObj = (GameObject)EditorGUILayout.ObjectField(obj, typeof(GameObject), true);if (EditorGUI.EndChangeCheck()){SelectObjectInOpenedPrefab(obj, prefabPath);}}EditorGUILayout.EndVertical();GUILayout.EndScrollView();}}// 在Prefab中查找private void FindInPrefab(){foundObjects.Clear();// 获取当前打开的Prefab场景PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();if (prefabStage == null){EditorUtility.DisplayDialog("No Opened Prefab", "Please open a prefab in Prefab Mode.", "OK");return;}GameObject prefabInstance = prefabStage.prefabContentsRoot;if (prefabInstance == null){EditorUtility.DisplayDialog("Error", "Failed to get the prefab contents.", "OK");return;}// 遍历Prefab中的对象FindObjectsWithScript(prefabInstance.transform, prefabStage.assetPath);if (foundObjects.Count > 0){Repaint();}else{EditorUtility.DisplayDialog("No Results", $"No objects with script '{scriptTypeName}' found in the opened prefab.", "OK");}}// 递归查找带有指定脚本的对象private void FindObjectsWithScript(Transform parent, string prefabPath){foreach (Transform child in parent){Component[] components = child.GetComponents<Component>();foreach (Component comp in components){if (comp != null && comp.GetType().FullName == scriptTypeName){// 将找到的对象和Prefab路径保存foundObjects.Add((child.gameObject, prefabPath));break;}}// 递归检查子对象FindObjectsWithScript(child, prefabPath);}}// 选中Prefab中的对象private void SelectObjectInOpenedPrefab(GameObject obj, string prefabPath){PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();if (prefabStage == null){prefabStage = PrefabStageUtility.OpenPrefab(prefabPath);}if (prefabStage != null){// 选中并高亮对象Selection.activeGameObject = obj;EditorGUIUtility.PingObject(obj);}else{Debug.LogError("Failed to open Prefab in Prefab Mode.");}}
}

四、主要功能解读

  1. 脚本选择: 用户通过 EditorGUILayout.ObjectField 来选择一个脚本MonoScript,当脚本被选择后,我们使用 GetClass() 方法来获取该脚本所对应的 C# 类类型,并获取其全名。

  2. Prefab 搜索: 我们使用 PrefabStageUtility.GetCurrentPrefabStage() 获取当前正在编辑的 Prefab 场景。如果 Prefab 场景未打开,我们提示用户打开 Prefab 模式。然后我们通过递归遍历 Prefab 中的每一个子对象,检查对象上是否附带了指定的脚本。

  3. 结果显示: 找到的对象会被列出,并且用户可以点击每一个对象,工具将自动高亮并选中该对象,方便用户进行进一步操作。

  4. 递归查找: 通过递归遍历 Prefab 的子节点,确保我们不会遗漏任何对象。每一个带有指定脚本的对象都会被保存到结果列表中。

五、工具的优势

  • 快速查找: 在复杂的项目中,Prefab 中的对象可能非常多,手动查找是低效的。这个工具能快速帮助开发者找到指定脚本。
  • 结果交互: 找到的对象直接在编辑器中展示,点击即可选中,并进行定位,非常方便。

六、总结

本文介绍的 "Prefab Script Finder" 是一个实用的 Unity Editor 工具,能够帮助开发者快速在 Prefab 中查找附带特定脚本的对象。通过这种方式,我们可以减少开发过程中的重复劳动,快速定位问题所在,并提高整体开发效率。

http://www.lryc.cn/news/444704.html

相关文章:

  • Frida-JSAPI:Interceptor使用
  • 【深度学习】(3)--损失函数
  • git学习报告
  • Spring MVC的应用
  • JavaEE: 深入探索TCP网络编程的奇妙世界(六)
  • 探秘 Web Bluetooth API:连接蓝牙设备的新利器
  • Kubernetes Pod调度基础(kubernetes)
  • Angular由一个bug说起之十:npm Unsupported engine
  • Android 开发高频面试题之——Flutter
  • 视频单目标跟踪研究
  • 若依vue3.0表格的增删改查文件封装
  • 【已解决】如何使用JAVA 语言实现二分查找-二分搜索折半查找【算法】手把手学会二分查找【数据结构与算法】
  • ERROR 1524 (HY000): Plugin ‘mysql_native_password‘ is not loaded
  • .NET 6.0 WebAPI 使用JWT生成Token的验证授权
  • M9410A VXT PXI 矢量收发信机,300/600/1200MHz带宽
  • 用工厂模式演示springboot三种注入方式 | @Autowired
  • es查询语法
  • LabVIEW提高开发效率技巧----合理使用数据流与内存管理
  • 如何在 ECharts 中设置轴标签
  • 怎么用gitee做一个图片仓库,在md文档中用这个图片网络地址,然后显示图片
  • Thinkphp(TP)
  • 【艾思科蓝】前端框架巅峰对决:React、Vue与Angular的全面解析与实战指南
  • IT行业的未来:技术变革与创新的持续推动
  • Python PDF转图片自定义输出
  • Git 常用操作命令说明
  • 自学前端的正确姿势是...
  • C/C++语言基础--C++构造函数、析构函数、深拷贝与浅拷贝等等相关知识讲解
  • json格式互相转换
  • Linux下共享内存详解
  • MySQL篇(管理工具)