Skip to content

Commit 05f3e40

Browse files
authored
Create SelectAssetsByType.cs
1 parent 3e2d55b commit 05f3e40

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// select multiple files or folders from Project window, then can filter by type and select all objects of that type
2+
3+
using UnityEditor;
4+
using UnityEngine;
5+
using System.Collections.Generic;
6+
7+
namespace UnityLibrary
8+
{
9+
public class SelectAssetsByType : EditorWindow
10+
{
11+
private Dictionary<System.Type, List<Object>> groupedObjects;
12+
private List<Object> allObjects;
13+
14+
[MenuItem("Tools/UnityLibrary/Select Assets By Type")]
15+
public static void ShowWindow()
16+
{
17+
GetWindow<SelectAssetsByType>("Select Assets By Type");
18+
}
19+
20+
private void OnEnable()
21+
{
22+
// Automatically refresh whenever selection changes
23+
Selection.selectionChanged += RefreshSelection;
24+
RefreshSelection();
25+
}
26+
27+
private void OnDisable()
28+
{
29+
// Unsubscribe from selection change event
30+
Selection.selectionChanged -= RefreshSelection;
31+
}
32+
33+
private void OnGUI()
34+
{
35+
if (groupedObjects != null)
36+
{
37+
foreach (var group in groupedObjects)
38+
{
39+
if (group.Value.Count > 0)
40+
{
41+
// Show type and number of objects
42+
if (GUILayout.Button($"{group.Value.Count} {group.Key.Name}(s)"))
43+
{
44+
// Select objects of this type in the editor
45+
SelectObjects(group.Value);
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
private void RefreshSelection()
53+
{
54+
groupedObjects = new Dictionary<System.Type, List<Object>>();
55+
allObjects = new List<Object>();
56+
57+
// Get selected objects and their children
58+
var selectedObjects = Selection.objects;
59+
foreach (var obj in selectedObjects)
60+
{
61+
allObjects.Add(obj);
62+
63+
// If the object is a folder, include assets inside the folder recursively
64+
if (AssetDatabase.IsValidFolder(AssetDatabase.GetAssetPath(obj)))
65+
{
66+
AddFolderContents(AssetDatabase.GetAssetPath(obj));
67+
}
68+
else
69+
{
70+
// Add children if it's a GameObject
71+
if (obj is GameObject gameObject)
72+
{
73+
AddChildren(gameObject);
74+
}
75+
}
76+
}
77+
78+
// Group by type
79+
foreach (var obj in allObjects)
80+
{
81+
var type = obj.GetType();
82+
if (!groupedObjects.ContainsKey(type))
83+
{
84+
groupedObjects[type] = new List<Object>();
85+
}
86+
groupedObjects[type].Add(obj);
87+
}
88+
89+
// Refresh the window UI
90+
Repaint();
91+
}
92+
93+
private void AddChildren(GameObject obj)
94+
{
95+
foreach (Transform child in obj.transform)
96+
{
97+
allObjects.Add(child.gameObject);
98+
AddChildren(child.gameObject);
99+
}
100+
}
101+
102+
private void AddFolderContents(string folderPath)
103+
{
104+
// Get all assets in the folder and subfolders
105+
string[] guids = AssetDatabase.FindAssets("", new[] { folderPath });
106+
foreach (string guid in guids)
107+
{
108+
string path = AssetDatabase.GUIDToAssetPath(guid);
109+
Object asset = AssetDatabase.LoadAssetAtPath<Object>(path);
110+
if (asset != null)
111+
{
112+
allObjects.Add(asset);
113+
}
114+
}
115+
}
116+
117+
private void SelectObjects(List<Object> objects)
118+
{
119+
Selection.objects = objects.ToArray();
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)