I can search for asset references in the Hierarchy window using the following search term: ref:Art/Effects/Explosion.png
Alternatively I can use “Find references in Scene” from the context menu when I right-click an asset. This will highlight all GameObject’s in the scene that use that asset.
Is there an editor API equivalent for this functionality? I would like to get all GameObject’s (or the number of GOs) that use a particular asset through code.
AssetDatabase.GetDependencies is operating in the project, but I have to restrict the search to the open scene only. It also doesn’t return the actual number how often an asset is used as far as I know, it returns more of a compacted dependency array.
I need to get all GameObjects in the opened scene, that are using a particular asset. I want to use this functionality to count how many GameObject’s a certain asset use in the scene.
GameObject.GetComponentsInChildren will only return the components under a particular game object, you would still have to get all the current game objects in the scene.
You can construct a simple solution using Resources.FindObjectsOfTypeAll.
It will return all loaded objects, you would have to filter these to contain only scene objects.
Once you have a list of scene objects you can iterate them using the SerializedObject and SerializedProperty APIs to extract any references to a given asset, and keep a counter for each asset.
Should be pretty easy using editor scripting APIs. Let me know if you need further help with this solution.
Sorry, misread the last line, thought you were looking for all under an asset.
Yea, FindObjectsOfTypeAll will work best in editor. If you need runtime for some reason, you can use GetComponentsInChildren + Scene.GetRootGameObjects to get all gameobjects (including inactive).