Find Scriptable Objects of a particular type in Editor

Hi !

I have a script defining a scriptable Object, and I created various assets from it. After this, I sorted them in various folders in my project. (note: they are not necessarily used on a given scene)

Is there a way to find them all using the search functionnalities of the editor ? (ex: “Find object of type” rather than “Find references in scene”)?

I would rather NOT use the label functionnalities, as I may easily forget to set it after creating such a scriptable object.

Thank you !

Bertrand.

AssetDatabase.FindAssets supports searching by type. It works exactly like in the search window in the project view, so if you do:

AssetDatabase.FindAssets("t:MyType");

you’ll get all assets of the type MyType.

The returned vales are Asset GUIDs, so you’ll need to use GUIDToAssetPath and LoadAssetAtPath to actually get the asset.

7 Likes

Be aware that this feature breaks apart if you rename your Type. While this issue has been reported over a year ago, no fix has landed yet.

A workaround, once AssetDatabase.FindAssets starts to return non-sense, is to re-import the project.

Link to bug-report in public Issue Tracker:
https://issuetracker.unity3d.com/issues/assetdatabase-is-not-updated-after-saving-renamed-script-name-which-is-referenced-in-asset

Thank you for your replies.

Sorry if I have been clear enough. I am NOT looking for a way to find assets using scripts. I am looking for a solution using Editor only.

Here is the “scenario” I am facing :

  • I create a script defining a ScriptableObject (ex: supercarDefinition.cs defines “SuperCar : ScriptableObject”
  • Using the [CreateAssetMenu] from the editor, I then create 12 scriptable objects assets (= files) of SuperCar which are properly renamed (“Ferrari Enzo”, “Porsche 911”, etc…)
  • In my project, my folder/file tree is very big, so let’s say I don’t really remember where all these 12 files are stored.
  • Now, let’s say I add a new variable to the script (ex; “maxSpeed”). Now my 12 existing superCars have an empty field which I need to fill.

… How can I find the 12 files ?

  • the filename does not include “SuperCar”, so I cannot use the search bar with the name
  • the “type” is a ScriptableObject, which does not exist in the default type.

thank you !

1 Like

Types that derive from ScriptableObject must use the file-name as class-name, otherwise serialization is going to behave in unexpected ways for assets of that type. Typical symptom would be that values are lost when restarting the editor.

You search the Project View using SuperCar as type argument. You’d enter the following search text in Project View search field:

Notice the “t:” prefix. This instructs Unity’s search engine to look for assets of the specific type, rather than names and it will display all assets in the project of type SuperCar. However, as I wrote in my previous comment, searching for types is sometimes not working properly. Unity’s search is affected by this bug as well.

2 Likes

Thanky ou, the research using “t:MyDefinitionName” works perfectly !

So this code doesn’t work anymore when renaming the class “MyType” ?

AssetDatabase.FindAssets("t:MyType");

Why not just using the keyword “nameof” to keep the link with the actual class name like so :

AssetDatabase.FindAssets($"t:{nameof(MyType)}");

Hope this helps.

4 Likes

Just ran into this. It literally stops working if the type is renamed, even when searching for the correct one. In my case, I changed the namespace of the class. Now it can’t find it anymore with the fully qualified class name.

t:ProjectName.SomeNamespace.TheType does no longer work, only t:TheType does. But that’s not an option for me, because I have types with the same names in different namespaces, so that’s a no-go. I have no idea how to resolve this now. I’m not using FindAssets(), though, but SearchService.Request(). Not sure if that makes any difference.

What version of Unity are you using? According to the issue tracker, it’s “Fixed in unity version 2020.2.0b3 with asset database V2” (link).

A workaround, once AssetDatabase.FindAssets starts to return non-sense, is to re-import the project.

I’m using 2021.3.31. Not keen on Re-importing the project - I wish it was enough to reimport the files that don’t work, but alas.

Okay, so I finally got around to test this, and re-importing the project didn’t fix the issue.

I wrote before that using just the type’s name would work. That’s broken now too. I only get some of the expected results.

If I open the Search window (Ctrl-K), and fill in p: t:smile:ataTile I get all 9 results as expected.

If I run

var searchText = $"p: t:smile:ataTile";
var results = SearchService.Request(searchText, SearchFlags.Synchronous);

then I only get 4 of them. It’s unclear why those four, but it’s always the same ones, even after reimporting all files.