Visual Studio 和 Resharper 中存在非常有用的“查找用法”功能,但我在 Unity3D 编辑器中找不到相同的功能。我在 Unity3d 中只看到“选择依赖项”,但我需要相反的。它存在吗?
问问题
20018 次
4 回答
4
不幸的是,Unity Editor 仅允许您在 Scene 中查找资产的使用情况。
- 此外,您得到的是使用它的选定资产列表,因此在更改鼠标焦点后,您将失去选择
Asset Store 上有一个解决方案:
- 在项目和场景视图中查找资产的所有用途
- 在单独的窗口中展示结果,显示正在使用目标资产的特定字段
- 允许您通过拖放替换特定的资产用途
GIF演示功能和界面:
资源商店链接:
于 2016-10-23T22:25:54.990 回答
4
免费和开源工具Dependencies-Hunter完成这项工作:查找给定资产的所有依赖项/使用情况。它还可以执行完整的项目分析以找到那里所有未使用的资产。
它由一个脚本组成,因此很容易将其复制粘贴到您的项目中。
在内部,它使用 AssetDatabase.GetDependencies 构建所有资产的地图以用于分析。
所以它有两个使用选项:
- 它在资产上下文菜单中添加了一个选项“在项目中查找引用”,它显示了该特定资产具有哪些依赖项。结果窗口如下所示
- 还有一个选项可以通过单独的编辑器窗口查找项目中所有未使用的资产。您可以通过指定要忽略的 RegExp 模式来过滤资产以进行分析。
于 2021-07-08T13:03:00.150 回答
2
Unity Answers 中的这个脚本在这方面做得很好。(也可以很容易地进行微调以改进其功能)
来源: http ://answers.unity.com/answers/1509032/view.html
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
public class BacktraceReference : EditorWindow
{
/// <summary> The result </summary>
public static List<Component> ReferencingSelection = new List<Component>();
/// <summary> allComponents in the scene that will be searched to see if they contain the reference </summary>
private static Component[] allComponents;
/// <summary> Selection of gameobjects the user made </summary>
private static GameObject[] selections;
/// <summary>
/// Adds context menu to hierarchy window https://answers.unity.com/questions/22947/adding-to-the-context-menu-of-the-hierarchy-tab.html
/// </summary>
[UnityEditor.MenuItem("GameObject/Find Objects Referencing This", false, 48)]
public static void InitHierarchy()
{
selections = UnityEditor.Selection.gameObjects;
BacktraceSelection(selections);
GetWindow(typeof(BacktraceReference));
}
/// <summary>
/// Display referenced by components in window
/// </summary>
public void OnGUI()
{
if (selections == null || selections.Length < 1)
{
GUILayout.Label("Select source object/s from scene Hierarchy panel.");
return;
}
// display reference that is being checked
GUILayout.Label(string.Join(", ", selections.Where(go => go != null).Select(go => go.name).ToArray()));
// handle no references
if (ReferencingSelection == null || ReferencingSelection.Count == 0)
{
GUILayout.Label("is not referenced by any gameobjects in the scene");
return;
}
// display list of references using their component name as the label
foreach (var item in ReferencingSelection)
{
EditorGUILayout.ObjectField(item.GetType().ToString(), item, typeof(GameObject), allowSceneObjects: true);
}
}
// This script finds all objects in scene
private static Component[] GetAllActiveInScene()
{
// Use new version of Resources.FindObjectsOfTypeAll(typeof(Component)) as per https://forum.unity.com/threads/editorscript-how-to-get-all-gameobjects-in-scene.224524/
var rootObjects = UnityEngine.SceneManagement.SceneManager
.GetActiveScene()
.GetRootGameObjects();
List<Component> result = new List<Component>();
foreach (var rootObject in rootObjects)
{
result.AddRange(rootObject.GetComponentsInChildren<Component>());
}
return result.ToArray();
}
private static void BacktraceSelection(GameObject[] selections)
{
if (selections == null || selections.Length < 1)
return;
allComponents = GetAllActiveInScene();
if (allComponents == null) return;
ReferencingSelection.Clear();
foreach (GameObject selection in selections)
{
foreach (Component cOfSelection in selection.GetComponents(typeof(Component)))
{
FindObjectsReferencing(cOfSelection);
}
}
}
private static void FindObjectsReferencing<T>(T cOfSelection) where T : Component
{
foreach (Component sceneComponent in allComponents)
{
componentReferences(sceneComponent, cOfSelection);
}
}
/// <summary>
/// Determines if the component makes any references to the second "references" component in any of its inspector fields
/// </summary>
private static void componentReferences(Component component, Component references)
{
// find all fields exposed in the editor as per https://answers.unity.com/questions/1333022/how-to-get-every-public-variables-from-a-script-in.html
SerializedObject serObj = new SerializedObject(component);
SerializedProperty prop = serObj.GetIterator();
while (prop.NextVisible(true))
{
bool isObjectField = prop.propertyType == SerializedPropertyType.ObjectReference && prop.objectReferenceValue != null;
if (isObjectField && prop.objectReferenceValue == references)
{
ReferencingSelection.Add(component);
}
}
}
}
#endif
于 2019-03-27T09:02:12.777 回答