- Home /
How can I find all instances of a Scriptable Object in the Project (Editor)
I have the .cs script of a ScriptableObject in the project. Somewhere in the project, there are instances of this ScriptableObject. I don't know their names, because someone else created them. Is there an easy way to find them? "Find References" works only in the Scene and not in the Project.
Thanks.
Answer by PizzaPie · Oct 25, 2017 at 06:38 PM
public static T[] GetAllInstances<T>() where T : ScriptableObject
{
string[] guids = AssetDatabase.FindAssets("t:"+ typeof(T).Name); //FindAssets uses tags check documentation for more info
T[] a = new T[guids.Length];
for(int i =0;i<guids.Length;i++) //probably could get optimized
{
string path = AssetDatabase.GUIDToAssetPath(guids[i]);
a[i] = AssetDatabase.LoadAssetAtPath<T>(path);
}
return a;
}
This will return all objects of Type T, you may make it more general if you change the where T : ScriptableObject to something more basic, but should suffice fo your occasion. Run it inside a Custom Editor, or create a ScriptableObject(Create an instance in the editor) with an Array of wanted type of scriptables and inside OnEnable() call that function. (if it is one time thing second solution probably is the fastest). Cheers.
Edit: make sure to add using UnityEditor; , this will not work on runtime.
Thanks, was hoping there was some out-of-the-box feature for this!
Answer by JosehCastro · Mar 02, 2018 at 07:36 PM
There is already an accepted answer, but it seems there is a more ergonomic way.
In the Project tab, right beside the search box, find the Search By Type button, with the circle, triangle and square icon. Select entries in that dropdown and see how their search strings are prepended by a t:
.
So, If you want to find all the ScriptableObject assets in your Project folders, you can search for:
t:ScriptableObject
To filter it even further you can search for specific subclasses of ScriptableObject by searching for their type:
t:MyScriptableObjectSubclass
It will look like this:
Edit: add more info and example code.
You can convert any search into a favorite by clicking on the star button.
I'm also using the script below to add a menu entry to make searching easier. Do notice that it uses reflection to access private methods inside the project window class. That is not safe and may break without notice whenever Unity changes its implementation.
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace Editors {
class EditorProjectWindowExtensions {
[MenuItem("Assets/Find ScriptableObject Instances",false, 30)]
private static void DoSomethingWithVariable() {
SetSearchString("t:" + Selection.activeObject.name);
}
// Note that we pass the same path, and also pass "true" to the second argument.
[MenuItem("Assets/Find ScriptableObject Instances", true)]
private static bool NewMenuOptionValidation()
{
if (!(Selection.activeObject is MonoScript)) {
return false;
}
MonoScript o = (MonoScript) Selection.activeObject;
Type ot = o.GetClass();
return ot.IsSubclassOf(typeof(ScriptableObject));
}
private static void SetSearchString(string searchString)
{
Type projectBrowserType = Type.GetType("UnityEditor.ProjectBrowser,UnityEditor");
if (projectBrowserType == null) {
return;
}
MethodInfo setSearchMethodInfo = projectBrowserType.GetMethod("SetSearch", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string) }, null);
if (setSearchMethodInfo == null) {
return;
}
EditorWindow window = EditorWindow.GetWindow(projectBrowserType);
setSearchMethodInfo.Invoke(window, new object[] {searchString});
}
}
}
You may also add search query to favorites ins$$anonymous$$d of $$anonymous$$enuItem. That tiny star button at right to the search field. Unfairly forgotten feature)
Answer by ogulcan_topsakal · Dec 27, 2021 at 08:40 AM
/// <summary>
/// Get all instances of scriptable objects with given type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<T> GetAllInstances<T>() where T : ScriptableObject
{
return AssetDatabase.FindAssets($"t: {typeof(T).Name}").ToList()
.Select(AssetDatabase.GUIDToAssetPath)
.Select(AssetDatabase.LoadAssetAtPath<T>)
.ToList();
}
It was a problem I encountered while doing research. If anyone still interested there is an edited version of @PizzaPie 's answer with using LINQ.