Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to use the search feature to search for prefab in a certain directory?

Discussion in 'Editor & General Support' started by ChiwTheNeko, Dec 12, 2023.

  1. ChiwTheNeko

    ChiwTheNeko

    Joined:
    Mar 24, 2022
    Posts:
    150
    Hello everyone

    I need to use the search function to search for prefabs that are in a certain directory and its sub directories.

    I managed to make it work before by doing queries like this:
    Code (CSharp):
    1. t=prefab ^Packages/com.my-company.my-package/Runtime/Prefabs
    I swear that used to work before. But now it doesn't. This query used to list me all the prefabs in the given directory but now it no longer find anything.

    It's not just for assets in packages. Before I could do this and it would list me all the prefabs in the given directory. But now it no longer find anything.
    Code (CSharp):
    1. t=prefab ^Assets/MyPrefabDirecory
    I tried queries without the regex stuff like this but they also can't find anything.
    Code (CSharp):
    1. t=prefab Assets/MyPrefabDirecory
    Screenshot_20231212_165123.png
    I checked the indexer, I even rebuild it, but no luck. My queries still can't find anything. It's like I can only search assets by their name but not by their path.

    So, can anyone please explain to me what is the proper way to use the search function to search for prefabs that are in a given directory? Am I not using the search query properly?

    I swear to God that used to work before and I didn't change anything. But now all my queries return no result and I just can't figure out why and I am loosing my mind.

    THIS IS NOT OPTIONAL!!! THIS IS NOT NICE TO HAVE FEATURE!!! I NEED THIS TO WORK!!! I ABSOLUTELY NEED THIS TO WORK LIKE IT WORKED BEFORE!!!

    Please anyone tell me what it is that I missed.
     
    Last edited: Dec 12, 2023
  2. ChiwTheNeko

    ChiwTheNeko

    Joined:
    Mar 24, 2022
    Posts:
    150
    I also tried it in code. Because in the end I need to use it in code.

    The following code used to work. I would list me all the prefabs in the Assets directory and its sub directories:
    Code (CSharp):
    1. [MenuItem("Merde/Merde")]
    2. private static void Merde()
    3. {
    4.   var query          = "t:prefab ^Assets/*";
    5.   var searchProvider = SearchService.GetProvider("asset"); // This is p: in search query, see AssetProvider static class
    6.   var searchContext  = new SearchContext(new[] {searchProvider}, query);
    7.   SearchService.Request(searchContext, OnSearchCompleted);
    8. }
    9.  
    10. private static void OnSearchCompleted(SearchContext context, IList<SearchItem> items)
    11. {
    12.   Debug.Log("-> completed");
    13.   foreach (var item in items)
    14.     Debug.Log("-> " + item.GetLabel(context));
    15. }
    Now it doesn't find anything. What the hell is going on?
     
  3. ChiwTheNeko

    ChiwTheNeko

    Joined:
    Mar 24, 2022
    Posts:
    150
    Still trying to figure it out.

    For example here I do a search by type. I search all the assets of type prefab. You can see that the search find both the prefabs that are in the Assets directory and the prefabs that are in the packages. This is what I expect. So far so good.
    type.png
    Now I do a search by glob. I try to find all the files that have a name ending by .prefab. But this time only the prefabs in the Assets directory are listed. The packages are completely ignored. Why? Does glob/regex not work on packaged assets? Is this by design? I'm pretty sure I got it to work before. There is no reason it shouldn't work now.
    glob.png
    Finally I try to mix the two. Searching files that are of type prefab and have a name that ends with .prefab (kinda redundant but this is for testing purpose). Now I get nothing. Why? The doc says you can use the 'and' operator to set several search criteria. Is this not the way to do it? How am I supposed to search for assets of a certain type with a path that match a glob/regex expression?
    Screenshot_20231213_194211.png
    I really wish the doc had some examples.
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,206
  5. ChiwTheNeko

    ChiwTheNeko

    Joined:
    Mar 24, 2022
    Posts:
    150
    Hello bugfinders and thank you for your reply.

    Yes, dir= is the closest thing from what I want but it's not quiet it.

    If I do p: dir="Platforming" t=Prefab then I will get a list of all prefabs that are into ANY directory named Platforming any where in both the project and the packages.

    I wish it was possible to do something like dir="Packages/com.my-company.my-package/Runtime/Prefabs" but that doesn't work. The dir= option can only take one exact directory name.
     
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,206
    yeah. i can understand that, it doesnt seem to like the / part.

    but then if my brain says you know exactly which folder, so example /assets/synty/elves/prefabs and you're looking for say archer.. well its in alphabetical order.. it will be there.
     
  7. ChiwTheNeko

    ChiwTheNeko

    Joined:
    Mar 24, 2022
    Posts:
    150
    Code (CSharp):
    1. where{t=prefab, @path:Packages/com.my-company.my-package/Runtime/Prefabs}
    I finally got it.

    I don't know why I thought my previous attempts with glob/regex worked before. I guess they never did and I was mistaken.
     
    bugfinders likes this.
  8. sebastienp_unity

    sebastienp_unity

    Unity Technologies

    Joined:
    Feb 16, 2018
    Posts:
    201
    Hi @ChiwTheNeko ,

    Sorry for the delayed answer.

    The dir filter in the AssetProvider has been updated in the latest 22.3 (and 23.X) to be clearer and more precise:

    ex: for an asset located in: Assets/Levels/Prototype/Materials/brick.mat the following query would yield all assets in the folder:

    dir:Assets/Levels/Prototype/Materials
    dir:Levels/Prototype/Materials
    dir:prototype/Materials
    dir:Materials
    dir:Assets/Levels/Prototype
    dir:Assets/Levels
    dir:Assets

    As for your first question:
    t=prefab Assets/MyPrefabDirecory

    When using only keywords search (Assets/MyPrefabFactory) we chose NOT to match directy name anymore (you have to use the dir filter for this). We had a lot of feedback saying matching dir name introduce a lot of noise in search results. This is similar to the Project Browser who matches keywords search only against NAME and not directory:
    upload_2023-12-18_8-56-44.png
     
  9. ChiwTheNeko

    ChiwTheNeko

    Joined:
    Mar 24, 2022
    Posts:
    150
    Hello @sebastienp_unity. Thank you for coming to my rescue.

    I upgraded to Unity 2022.3.16 and I rebuilt the indexer just to make sure. But I didn't managed to get all the behaviours you are describing.

    When I search for all prefabs in the Project directory I get the expected result. All the prefabs in Project and its subdirectories are shown. I don't think this work before upgrading Unity, but now it does so that's cool.
    Work.png
    However, if I search all prefabs in the Assets directory then I get no result.
    Assets.png
    If I specify the full directory path I get no result as well. This is the behaviour I was looking for from the beginning, being able to specify the full directory path instead of just its name. As far as I can tell this is still not possible. Or did I miss something?

    DontWork.png
    Thank you very much.
     
  10. sebastiengrenier

    sebastiengrenier

    Unity Technologies

    Joined:
    Jun 11, 2019
    Posts:
    96
    Hi! Can you try 2022.3.17 or later? Our fix for the "dir" filter landed in 2022.3.17. Thank you!
     
    ChiwTheNeko likes this.
  11. ChiwTheNeko

    ChiwTheNeko

    Joined:
    Mar 24, 2022
    Posts:
    150
    YES!
    I just tried it and your fix completely solve my problem. Thank you very much.