How can i find all scripts in folders in most optimized way?

For an editor script I want to find all objects containing a certain interface in my project. Including prefabs and scriptable objects.

I can find it with AssetDatabase.FindAssets, but it will take time to search for all assets each time and I will have to do this very often.

How can I do this without searching all assets?

A different architecture could lead to better results. :wink:

Specifically this:

Do you? Why? This seems like a brute-force solution.

If a user created, renamed, moved or deleted one or more assets that you are interested in, you write an AssetPostprocessor and implement OnPostprocessAllAssets and for each check if this is an asset you are interested in. The most lightweight check comes first of course, such as if it’s not a .cs or .asset extension you skip the rest.

You would then register and unregister the assets you are interested in by using another (ScriptableObject) asset. Only on first use or when running manually (ie “force refresh”) would you need to actually go looking through the entire assets tree.

Alternatively, require them to be in a specific folder.

Lastly … provide us with the use case for this script and we may be able to tell you that perhaps Unity already has this or that for exactly that purpose. That’s all in the “why are you doing this” part that you did not mention. :wink:

1 Like

I am writing a dependency injection system that will only run on the editor.

Instead of manually drag-dropping, I will add [Auto] attributes to my scripts and it will try to get data from classes with IService interface.

My goal is to first find all fields with the [Auto] attribute, both in the scene and in the project.

Then find all asset/prefab/scene game objects that have the IService interface. If the IService data and type match, assign this data to the auto field. This is my purpose

How are you going to make that assignment on a SO, on a component in a scene, and a component in a prefab? That seems like the bigger challenge in case you haven‘t done that part yet.

Also be sure to use TypeCache, don‘t enumerate AppDomain Assemblies.

I do wonder, say I have this:

[Auto, SerializeReference] private field MySOBase so1;
[Auto, SerializeReference] private field MySOBase so2;

Both are base class references where base class implements IService (bad name btw because it‘s extremely generic, same goes for „Auto“) but I have several subclass implementations and corresponding assets in the project. Which of my SO implementations is this going to find and inject?

Have you researched whether there‘s already a tool like yours?
If so, you could just use that.
If not, perhaps there may be a good reason why there isn‘t one, because it may just stop short of being useful due to technical limitations. Scan for those „edge cases“, try to find workable solutions first. :wink:

1 Like

I already have working version with filtred paths.

   public interface IProvideService
    {
        object Service { get; }
        string ID { get; }
        IEnumerable<Type> ServiceTypes { get; }
       
    }

9870642--1422735--upload_2024-6-3_18-49-20.png

For same type im using string id, I also have tool which turns scriptable objects into enums automatcly with asset name so im referencing like this.