Version: 2022.3
言語: 日本語
public Func<string,string> resolveDocumentHandler ;

説明

Handler used to resolve a document ID to some other data string.

using System.Linq;
using UnityEditor;
using UnityEditor.Search;
using UnityEngine;

/// <summary>
/// Since the search indexes only contain string document IDs that must be unique,
/// you can use `resolveDocumentHandler` to transform these document IDs into something
/// that can be searched while running queries that contain simple words.
/// </summary>
static class Example_SearchIndexer_resolveDocumentHandler
{
    struct Weapon
    {
        public string id { get; private set; }
        public int power { get; set; }
        public string name { get; set; }

        public Weapon(string name, int power)
        {
            id = System.Guid.NewGuid().ToString("N");
            this.name = name;
            this.power = power;
        }
    }

    [MenuItem("Examples/SearchIndexer/resolveDocumentHandler")]
    public static void Run()
    {
        const int MagicPower = 1;
        var weapons = new[]
        {
            new Weapon("Long Bow", 2),
            new Weapon("Short Sword", 3),
            new Weapon("Short Sword", 3 + MagicPower), // We have two weapons that will have different ids, but the same name.
            new Weapon("Long Sword", 4)
        };
        var si = new SearchIndexer("Weapons")
        {
            // Define a handler that returns a searchable string that can search for each document.
            // These words are not indexed, therefore the string is linear and might not scale as expected.
            // IMPORTANT: Unless you want to have case-sensitive search, you should convert the resolved string to lowercase.
            resolveDocumentHandler = (id) => weapons.First(w => w.id == id).name.ToLowerInvariant()
        };

        si.Start();
        foreach (var w in weapons)
        {
            var di = si.AddDocument(w.id);
            si.AddWord("weapon", 0, di);
        }
        si.Finish(() => OnIndexReady(si, weapons));
    }

    private static void OnIndexReady(SearchIndexer si, Weapon[] weapons)
    {
        var results = si.Search("weapon sword").ToArray();
        Debug.Assert(results.Length == 3, "No weapon were found");
        foreach (var result in results)
        {
            var w = weapons.First(w => w.id == result.id);
            Debug.Log($"Found [{result.index}] {result.id}/{w.name} ({w.power})");
        }
    }
}
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961