Custom Asset Indexing for prefab

Hi,

I want to share a question we got from a client and how custom Indexing help solve a specific problem.

Initial situation

One of our client has built a validation system where they want to ensure that no prefabs (or children of a prefab) should contain AudioListener. This client asked us if it was possible to use the Search Window to find all prefabs failing this validation step.

How custom indexing help fixed the issue

I have written a custom indexer to flag prefab (and their children) with AudioListener.

using UnityEditor;
using UnityEditor.Search;
using UnityEngine;

public class PrefabValidator
{
    [CustomObjectIndexer(typeof(GameObject), version = 1)]
    internal static void PrefabWithAudioListener(CustomObjectIndexerTarget context, ObjectIndexer indexer)
    {
        /*
        This customindexer will tag all prefab who contains an AudioListener or a Children with an AudioListener.

        running the query: `p: validate:PrefabWithAudioListener` will yields all of these prefabs.

        */
        var go = context.target as GameObject;
        if (go == null)
            return;

        if (string.IsNullOrEmpty(AssetDatabase.GetAssetPath(go)))
            return;

        var hasAudioListener = go.GetComponentInChildren<AudioListener>();
        if (hasAudioListener == null)
            return;

        indexer.IndexProperty(context.documentIndex, "validate", "PrefabWithAudioListener", true);
    }

Thus writing the following query validate:PrefabWithAudioListener will yield all “faulty” prefabs.

You can find more information on CustomObjectndexer in the Unity Scripting API.

I have also written an article on our Search extensions wiki about this.

The code of this example can be found in our Search Extensions package.

5 Likes

This is a powerful API that I was unaware of. I can see lots of great uses for this kind of thing. Thanks for sharing.