10

The very useful feauture "Find usages" exists in Visual Studio and Resharper, but I can't find the same in Unity3D Editor. I see only "Select dependencies" in Unity3d, but I need the opposite one. Does it exist?

user2018103
  • 103
  • 1
  • 1
  • 5

5 Answers5

17

From the Editor, go to the Project Tab, select the given Asset, right-click on it and then click Find References In Scene. It will show you every GameObject that the given Asset is attached to in the Hierarchy View if the given Asset is a script. If it is an image, audio file, or a prefab, it will show you which GameObject is using that Asset in the Hierarchy View.

enter image description here

Alan Mattano
  • 860
  • 3
  • 14
  • 22
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Hello Programmer, thanks a lot for prompt and detailed reply even with graphics! It's pretty useful, but (from my point of view) for not so big projects, only with one or several scenes. Anyway, it's much better than nothing.)) – user2018103 Oct 24 '16 at 07:51
  • By bigger projects, I mean that selection I get when using Find Usages in Scene is not very useful when you have more that 3 usages. Also, it doesn't allow Project search like Select Dependencies does, so sorry, it doesn't fully solve my problem. – user2018103 Oct 24 '16 at 10:11
  • That's fine. Happy coding! – Programmer Oct 24 '16 at 10:23
  • When you right-click on an object option "Find Reference In Scene" does not exist. There only is the "Find References In Scene" – Alan Mattano Jul 24 '20 at 09:01
  • It's been broken for me for awhile now :/ https://issuetracker.unity3d.com/issues/find-references-in-scene-shows-objects-that-are-not-referencing-the-asset – DShook Dec 23 '20 at 03:30
13

The free and open-source tool Dependencies-Hunter does this job: finding all dependencies/usages of a given asset(s). It can also perform a full project analysis to find all unused assets there.

It consists of a single script so it's easy to just copy-paste it to your project.

Internally it uses AssetDatabase.GetDependencies to build a map of all assets to use for analysis.

So it has two options to use:

  • it adds an option "Find References In Project" in the asset context menu which shows what dependencies this specific asset has. The resulting window looks like this
  • there is also an option to find all unused assets in a project via a separate Editor window. You can filter assets for analysis by specifying RegExp patterns of what to ignore.
alvperov
  • 151
  • 1
  • 6
  • Just add `"unity-dependencies-hunter": "https://github.com/AlexeyPerov/Unity-Dependencies-Hunter.git#upm"` to your `manifest.json` file in `Packages/` and in my case Unity automatically loaded the package after saving. – ChrisoLosoph Aug 06 '22 at 15:28
5

Unfortunately, Unity Editor allows you to find usages of an asset in Scene only.

  • Moreover, what you get is selected list of assets using it, so after changing the mouse focus you'll lose your selection

There's a solution on Asset Store that does:

  • Find all usages of an asset, both in Project and in Scene view
  • Demonstrate results in a separate window, showing particular fields that are using target asset
  • Allows you to replace particular asset usages with drag&drop

GIF demonstrating features and interface:

Asset Store link:

gotlight
  • 78
  • 1
  • 3
3

There is this script from the Unity Answers that does a pretty good job at it. (Can also be easily slightly tweaked to improve its functionalities)

Source: 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
jeromej
  • 10,508
  • 2
  • 43
  • 62
1

The above solutions are actually incorrect. Unity does support finding references to an asset in project view, but there is no button for it. You have to type in the query manually into the project view search bar: "ref:Assets//" with file extension. This is the exact same query it puts into the scene view search by when you look for referenced in scene. It takes a bit for unity to "think" but it will eventually spit out the references to your asset/script.