Ejemplo n.º 1
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var settings = new SearchDatabase.Settings
            {
                guid      = null,
                root      = null,
                roots     = null,
                source    = null,
                name      = null,
                baseScore = 0,
                excludes  = null,
                includes  = null,

                type    = type,
                options = GetOptions(),
            };

            EditorApplication.LockReloadAssemblies();
            try
            {
                var indexer = SearchDatabase.CreateIndexer(settings);
                indexer.IndexDocument(ctx.assetPath, false);
                indexer.ApplyUnsorted();

                var indexArtifactPath = ctx.GetResultPath($"{type}.{(int)options:X}.index".ToLowerInvariant());
                using (var fileStream = new FileStream(indexArtifactPath, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
                    indexer.Write(fileStream);

                Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, $"\nGenerated {type} ({GetType().Name}) {indexArtifactPath} for {ctx.assetPath} with {options}");

                ctx.DependsOnSourceAsset(Path.GetDirectoryName(ctx.assetPath).Replace("\\", "/"));
                ctx.DependsOnCustomDependency(GetType().GUID.ToString("N"));

                #if UNITY_2020_1_OR_NEWER
                ctx.DependsOnCustomDependency(nameof(CustomObjectIndexerAttribute));
                #endif
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                ctx.LogImportError(ex.Message);
            }
            finally
            {
                EditorApplication.UnlockReloadAssemblies();
            }
        }
Ejemplo n.º 2
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var filePath = ctx.assetPath;
            var jsonText = System.IO.File.ReadAllText(filePath);
            var settings = JsonUtility.FromJson <SearchDatabase.Settings>(jsonText);
            var fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);

            hideFlags |= HideFlags.HideInInspector;

            #if DEBUG_INDEXING
            using (new DebugTimer($"Importing index {fileName}"))
            #endif
            {
                db               = ScriptableObject.CreateInstance <SearchDatabase>();
                db.name          = fileName;
                db.hideFlags     = HideFlags.NotEditable;
                db.settings      = settings;
                db.settings.root = Path.GetDirectoryName(filePath).Replace("\\", "/");
                if (String.IsNullOrEmpty(db.settings.name))
                {
                    db.settings.name = fileName;
                }
                db.index = db.CreateIndexer(settings);

                if (!Reimport(filePath))
                {
                    if (ShouldDelayImport())
                    {
                        db.Log("Delayed Import");
                        EditorApplication.delayCall += () => AssetDatabase.ImportAsset(filePath);
                    }
                    else
                    {
                        Build();
                    }
                }

                ctx.AddObjectToAsset(fileName, db);
                ctx.SetMainObject(db);
            }
        }