Спасибо, посмотрел этот скрипт.
Мне необходима прямо противоложная задача. Не по заданным объектам в Hierarchy получить список ассетов, а по ассету в Project View получить список объектов в Hierarchy, которые используют этот ассет. Но скрипт не оказался совсем бесполезным, он натолкнул на мысль как реализовать нужный мне функционал. Привожу его здесь.
Скрипт работает для 3х видов ассетов: текстур, материалов, скриптов, но легко расширяется на все другие виды ассетов
Помещаем скрипт ComponentShower.cs в Assets/Editor
Выделяем любой материал\текстуру\скрипт в ProjectView и открываем окно Componet->Component Shower
В окне отобразится список объектов сцены, которые используют выделенный ассет. Щелкаем кнопку Select Objects in Scene и все эти объекты выделяются в сцене.
Используется csharp
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
//Author: Oleg Gelezcov
public class ComponentShower : EditorWindow {
private string selection;
private Vector2 scrollPosition;
[MenuItem("Component/Component Shower")]
public static void Launch() {
EditorWindow window = GetWindow<ComponentShower>();
window.Show();
}
public void OnGUI() {
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
GUILayout.BeginHorizontal(GUI.skin.GetStyle("Box"));
UpdateString();
foreach (KeyValuePair<string, List<GameObject>> pair in nameAssetUseItObjectsPair) {
string text = "Asset: " + pair.Key + System.Environment.NewLine;
text += "Objects: " + System.Environment.NewLine;
foreach (GameObject obj in pair.Value) {
text += " " + obj.name + System.Environment.NewLine;
}
GUILayout.Label(text);
if (GUILayout.Button("Select objects in scene")) {
Selection.objects = pair.Value.ToArray();
}
}
GUILayout.EndHorizontal();
GUILayout.EndScrollView();
}
Dictionary<string, List<GameObject>> nameAssetUseItObjectsPair = new Dictionary<string, List<GameObject>>();
private void UpdateString() {
//get id of selected asset
int instanceId = Selection.activeInstanceID;
//get pass of asset by id
string assetPath = AssetDatabase.GetAssetPath(instanceId);
//load all assets in selected path
Object[] assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
//output string
selection = string.Empty;
nameAssetUseItObjectsPair.Clear();
foreach (Object obj in assets) {
//If asset is material, then get all GameObjects, which used this material in renderer
Material m = obj as Material;
if (m != null)
{
List<GameObject> materialObjects = GetObjectsWhichUsedMaterialWithName(m.name).Distinct().ToList();
selection += "Material: " + m.name + " used by objects in scene" + System.Environment.NewLine;
foreach (GameObject o in materialObjects) {
selection += " " + o.name + System.Environment.NewLine;
}
nameAssetUseItObjectsPair.Add(m.name, materialObjects);
}
//If asset is Texture find all objects in scene, which used this texture
Texture t = obj as Texture;
if (t != null) {
List<GameObject> texturesObjects = GetObjectsWhichUsedTexturesWithThisName(t.name).Distinct().ToList();
selection += "Texture: " + t.name + " used by objects in scene" + System.Environment.NewLine;
foreach (GameObject o in texturesObjects) {
selection += " " + o.name + System.Environment.NewLine;
}
nameAssetUseItObjectsPair.Add(t.name, texturesObjects);
}
//If asset is script, find all objects in scene which used this script
MonoScript ms = obj as MonoScript;
if (ms != null) {
List<GameObject> scriptObjects = GetObjectsWhichUsedScriptsWithThisType(ms.GetClass()).Distinct().ToList();
selection += "Script: " + ms.name + System.Environment.NewLine;
foreach (GameObject o in scriptObjects) {
selection += " " + o.name + System.Environment.NewLine;
}
nameAssetUseItObjectsPair.Add(ms.name, scriptObjects);
}
}
//selection = assetPath;
}
//get list of all object, which contain component typeof t
private List<GameObject> GetObjectsWhichUsedScriptsWithThisType(System.Type t)
{
Component[] comps = (Component[])FindObjectsOfType(typeof(Component));
List<GameObject> objs = new List<GameObject>();
foreach (Component c in comps) {
if (c.GetType() == t) {
objs.Add(c.gameObject);
}
}
return objs;
}
//get list of all objects in scene which contain texture with name p(Name of texture asset)
private List<GameObject> GetObjectsWhichUsedTexturesWithThisName(string p)
{
Component[] comps = (Component[])FindObjectsOfType(typeof(Component));
List<GameObject> objs = new List<GameObject>();
foreach (Component c in comps) {
if (c.gameObject.renderer != null) {
if (c.gameObject.renderer.sharedMaterials != null) {
foreach (Material m in c.gameObject.renderer.sharedMaterials) {
if (m.mainTexture != null) {
if (m.mainTexture.name == p) {
objs.Add(c.gameObject);
}
}
}
}
}
}
return objs;
}
//Get list of all objects which contains material with name materialName(Name of material asset)
private List<GameObject> GetObjectsWhichUsedMaterialWithName(string materialName) {
Component[] comps = (Component[])FindObjectsOfType(typeof(Component));
List<GameObject> objs = new List<GameObject>();
foreach (Component c in comps) {
if (c.gameObject.renderer != null)
{
if (c.gameObject.renderer.sharedMaterial != null)
{
if (c.gameObject.renderer.sharedMaterial.name == materialName)
{
objs.Add(c.gameObject);
}
}
}
}
return objs;
}
}