I’m trying to learn how to use the new Search feature. The least I could say is that is isn’t straightforward. I am trying to craft a query that can find all the prefab assets that have a component of a type that derives the BehaviouralActor class. This is one of my own class that derives MonoBehaviour.
I tried p: t:BehaviouralActor but it doesn’t find anything. If I do p: t:TopDownAvatar then it finds some prefab because TopDownAvatar is a final class that is used directly in prefabs. But this isn’t what I want.
I tried using the has: and has= filters as described on this page. But the search window says that the “has” filter is unknown. The documentation says that I have to check the Property checkbox in the Index Manager preferences and I did and I rebuilded the index. But still no luck.
I dug a little deeper and tried solving the issue by creating a custom object indexer following the example given on this page. Long story short, my indexer is called whenever a prefab is indexed, check if the preab contains my custom component and if it does it adds a property called “bev” into the indexer.
Here is the code.
[CustomObjectIndexer(typeof(GameObject))]
internal static void ActorIndexer(CustomObjectIndexerTarget context, ObjectIndexer indexer)
{
var go = context.target as GameObject;
if (go == null)
return;
var actor = go.GetComponent<BehaviouralActor>();
if (actor == null)
return;
var collection = actor.BehaviourCollection;
if (collection == null)
return;
foreach (var behaviourDescription in collection.BehaviourDescription)
{
var behaviourInfo = BehaviourInfo.GetBehaviourInfo(behaviourDescription.TypeId);
indexer.AddProperty("bev", behaviourInfo.TypeName, context.documentIndex, exact: true, saveKeyword: true);
}
}
I know the above code works because when I look into the indexer I can see my custom bev properties with the expected values.
Hello!
What version of Unity are you using? Can you try to enable “types” on the index? I think we only index ancestor types when this is enabled.
However, your custom indexer should have worked. We will investigate.
Hello sebastiengrenier. Thank you for taking the time to look at my problem.
I am using Unity 2021.3.26. I confirm that “types” is enabled in the indexes. But ancestor types doesn’t seem to work.
For my custom indexer, I managed to get it to work by making sure that the property value is made lowercase by calling ToLowerInvariant() on it. It appears the indexer can not find non lowercase properties, even if in the search query the are being typed with the exact same case as they were added. Maybe the doc should mention that, or maybe AddProperty() should give a warning if you try adding a non lowercase property value.
Another question I had : is there a way to force the indexer to reindex a particular asset? I have custom editor windows that I use to edit my assets. But when an asset is modified and saved it is not always reindexed right away.
Indexing all the chain of types is a bug we fixed in the last year. I am unsure though if it was backported to 21.3 (I know it is fixed in 22.3). I will do some testing and get back to you. If the fix is needed, we will check to backport the fix.
From 22.3 and onward, the UI of the Search window has been improved and we have a Visual Query builder that help you craft query without remembering all the keywords by heart. Hopefully you can upgrade to 22.3 in the near future.
Thanks for the report. I will get back to you when I get the chance to tets the workflow in 21.3
I tested your search testcase with a derived type in 21.3.33f1 and things were working fine (see attached screenshot). I suggest you upgrade to the newest LTS.
When we are running searches, we lower case the whole query to allow comparison IgnoringCasing. IndexProperty will also lower case all the values. AddProperty index without lower casing. This is a confusing behavior and I am sorry for that.
Also, if you chose to use a CustomObjectIndexer I suggest you set a version on the attribute. Updating the version will trigger a reindexation of assets using this CustomObjectIndexer.