Пример #1
0
        public static Texture2D GetSceneObjectPreview(GameObject obj, FetchPreviewOptions options, Texture2D defaultThumbnail)
        {
            var sr = obj.GetComponent <SpriteRenderer>();

            if (sr && sr.sprite && sr.sprite.texture)
            {
                return(sr.sprite.texture);
            }

            #if PACKAGE_UGUI
            var uii = obj.GetComponent <UnityEngine.UI.Image>();
            if (uii && uii.mainTexture is Texture2D uiit)
            {
                return(uiit);
            }
            #endif

            var preview = AssetPreview.GetAssetPreview(obj);
            if (preview)
            {
                return(preview);
            }

            var assetPath = SearchUtils.GetHierarchyAssetPath(obj, true);
            if (String.IsNullOrEmpty(assetPath))
            {
                return(defaultThumbnail);
            }
            return(Utils.GetAssetPreviewFromPath(assetPath, options));
        }
Пример #2
0
        private void IndexScene(string scenePath, bool checkIfDocumentExists)
        {
            bool sceneAdded = false;
            var  scene      = EditorSceneManager.GetSceneByPath(scenePath);

            try
            {
                if (scene == null || !scene.isLoaded)
                {
                    scene      = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
                    sceneAdded = scene != null && scene.isLoaded;
                }

                if (scene == null || !scene.isLoaded)
                {
                    return;
                }

                var objects = SearchUtils.FetchGameObjects(scene);
                IndexObjects(objects, "scene", scene.name, checkIfDocumentExists);
            }
            finally
            {
                if (sceneAdded)
                {
                    EditorSceneManager.CloseScene(scene, true);
                }
            }
        }
Пример #3
0
        internal IEnumerable <SearchItem> SelectPath(SearchItem item)
        {
            var obj = item.provider?.toObject?.Invoke(item, typeof(UnityEngine.Object));

            if (!obj)
            {
                return new [] { item }
            }
            ;

            var path = AssetDatabase.GetAssetPath(obj);

            if (!String.IsNullOrEmpty(path))
            {
                return new [] { new SearchItem(path) }
            }
            ;

            if (obj is GameObject go)
            {
                return new [] { new SearchItem(SearchUtils.GetHierarchyPath(go)) }
            }
            ;

            return(new [] { item });
        }
Пример #4
0
        public override Texture2D GetThumbnail(Object obj)
        {
            var assetPath = SearchUtils.GetHierarchyAssetPath(obj as GameObject, true);

            if (String.IsNullOrEmpty(assetPath))
            {
                return(Utils.GetThumbnailForGameObject(obj as GameObject));
            }
            return(AssetPreview.GetAssetPreview(obj) ?? Utils.GetAssetPreviewFromPath(assetPath, FetchPreviewOptions.Preview2D));
        }
Пример #5
0
        /// <summary>
        /// Index all the properties of an object.
        /// </summary>
        /// <param name="id">Object id.</param>
        /// <param name="obj">Object to index.</param>
        /// <param name="documentIndex">Document where the indexed object was found.</param>
        /// <param name="dependencies">Index dependencies.</param>
        protected void IndexObject(string id, Object obj, int documentIndex, bool dependencies = false)
        {
            using (var so = new SerializedObject(obj))
            {
                var p    = so.GetIterator();
                var next = p.Next(true);
                while (next)
                {
                    bool   saveKeyword = true;
                    var    fieldName   = p.displayName.Replace("m_", "").Replace(" ", "").ToLowerInvariant();
                    var    scc         = SearchUtils.SplitCamelCase(fieldName);
                    var    fcc         = scc.Length > 1 && fieldName.Length > 10 ? scc.Aggregate("", (current, s) => current + s[0]) : fieldName;
                    object fieldValue  = GetPropertyValue(p, ref saveKeyword);

                    // Some property names are not worth indexing and take to much spaces.
                    if (k_FieldNamesNoKeywords.Contains(fieldName))
                    {
                        saveKeyword = false;
                    }

                    if (fieldValue != null)
                    {
                        var sfv = fieldValue as string;
                        if (sfv != null)
                        {
                            if (sfv != "")
                            {
                                IndexProperty(id, fcc, sfv.Replace(" ", "").ToLowerInvariant(), documentIndex, saveKeyword);
                            }
                            else
                            {
                                IndexWord(id, $"@{fcc}", documentIndex);
                            }
                        }
                        else if (fieldValue is double)
                        {
                            var nfv = (double)fieldValue;
                            IndexNumber(id, fcc.ToLowerInvariant(), nfv, documentIndex);
                        }

                        IndexDebugMatch(id, fcc, fieldValue.ToString());
                    }

                    if (dependencies)
                    {
                        AddReference(id, p, documentIndex);
                    }

                    next = p.Next(p.hasVisibleChildren);
                }
            }
        }
Пример #6
0
        public override bool GetDescription(Object obj, StringBuilder sb)
        {
            AddSeparatorIfNeeded(sb);
            var go = obj as GameObject;

            if (go.scene.IsValid())
            {
                sb.AppendFormat("{0} ({1})", SearchUtils.GetHierarchyPath(go), go.tag);
            }
            else
            {
                sb.Append(go.tag);
            }
            return(true);
        }
Пример #7
0
        internal IEnumerable <SearchItem> SelectReferences(SearchItem item, string type, int depth)
        {
            var obj = item.provider?.toObject?.Invoke(item, typeof(UnityEngine.Object));

            if (!obj)
            {
                return(Enumerable.Empty <SearchItem>());
            }

            var assetProvider = SearchService.GetProvider("asset");

            return(SearchUtils.GetReferences(obj, depth)
                   .Where(path => string.IsNullOrEmpty(type) || AssetDatabase.GetMainAssetTypeAtPath(path)?.Name == type)
                   .Select(path => assetProvider.CreateItem(path)));
        }
Пример #8
0
        public static IEnumerable <string> SplitFileEntryComponents(string path, char[] entrySeparators, int minIndexCharVariation, int maxIndexCharVariation)
        {
            var name       = Path.GetFileNameWithoutExtension(path);
            var nameTokens = name.Split(entrySeparators).Distinct().ToArray();
            var scc        = nameTokens.SelectMany(SearchUtils.SplitCamelCase).Where(s => s.Length > 0).ToArray();
            var fcc        = scc.Aggregate("", (current, s) => current + s[0]);

            return(Enumerable.Empty <string>()
                   .Concat(scc)
                   .Concat(new[] { Path.GetExtension(path).Replace(".", "") })
                   .Concat(SearchUtils.FindShiftLeftVariations(fcc))
                   .Concat(nameTokens.Select(s => s.ToLowerInvariant()))
                   .Concat(path.Split(entrySeparators).Reverse())
                   .Where(s => s.Length >= minIndexCharVariation)
                   .Select(s => s.Substring(0, Math.Min(s.Length, maxIndexCharVariation)).ToLowerInvariant())
                   .Distinct());
        }
Пример #9
0
        private void IndexObjects(GameObject[] objects, string type, string containerName, bool checkIfDocumentExists)
        {
            var options   = settings.options;
            var globalIds = new GlobalObjectId[objects.Length];

            GlobalObjectId.GetGlobalObjectIdsSlow(objects, globalIds);

            for (int i = 0; i < objects.Length; ++i)
            {
                var obj = objects[i];
                if (!obj)
                {
                    continue;
                }

                if (PrefabUtility.IsPrefabAssetMissing(obj))
                {
                    continue;
                }

                if (obj.tag?.Equals("noindex~", StringComparison.Ordinal) ?? false)
                {
                    continue;
                }

                var gid           = globalIds[i];
                var id            = gid.ToString();
                var path          = SearchUtils.GetTransformPath(obj.transform);
                var documentIndex = AddDocument(id, path, checkIfDocumentExists);

                if (!String.IsNullOrEmpty(name))
                {
                    IndexProperty(documentIndex, "a", name, saveKeyword: true);
                }

                var depth = GetObjectDepth(obj);
                IndexNumber(documentIndex, "depth", depth);

                IndexWordComponents(documentIndex, path);
                IndexProperty(documentIndex, "from", type, saveKeyword: true, exact: true);
                IndexProperty(documentIndex, type, containerName, saveKeyword: true);
                IndexGameObject(documentIndex, obj, options);
                IndexCustomGameObjectProperties(id, documentIndex, obj);
            }
        }
Пример #10
0
 public IEnumerable <string> GetEntryComponents(string path, int index)
 {
     return(SearchUtils.SplitFileEntryComponents(path, entrySeparators));
 }
 /// <summary>
 /// Splits a string into multiple words that will be indexed.
 /// It works with paths and UpperCamelCase strings.
 /// </summary>
 /// <param name="entry">The string to be split.</param>
 /// <param name="documentIndex">The document index that will index that entry.</param>
 /// <returns>The entry components.</returns>
 protected virtual IEnumerable <string> GetEntryComponents(string entry, int documentIndex)
 {
     return(SearchUtils.SplitFileEntryComponents(entry, SearchUtils.entrySeparators));
 }
Пример #12
0
        private void DrawInspector(SearchSelection selection, float width)
        {
            if (m_Editors == null)
            {
                return;
            }

            for (int i = 0; i < m_Editors.Length; ++i)
            {
                var e = m_Editors[i];
                if (!e)
                {
                    continue;
                }

                EditorGUIUtility.labelWidth = 0.4f * width;
                bool foldout = false;
                if (!m_EditorTypeFoldout.TryGetValue(e.GetType().Name, out foldout))
                {
                    foldout = true;
                }
                using (new EditorGUIUtility.IconSizeScope(new Vector2(16, 16)))
                {
                    var sectionContent = selection.Count == 1 ? EditorGUIUtility.ObjectContent(e.target, e.GetType()) : e.GetPreviewTitle();
                    if (selection.Count == 1)
                    {
                        sectionContent.tooltip = sectionContent.text;
                    }
                    else
                    {
                        sectionContent.tooltip = String.Join("\r\n", e.targets.Select(t => $"{SearchUtils.GetObjectPath(t)} ({t.GetInstanceID()})"));
                    }
                    foldout = EditorGUILayout.BeginToggleGroup(sectionContent, foldout);
                    if (foldout)
                    {
                        try
                        {
                            if (e.target is Transform)
                            {
                                e.DrawDefaultInspector();
                            }
                            else
                            {
                                e.OnInspectorGUI();
                            }
                            m_EditorTypeFoldout[e.GetType().Name] = foldout;
                        }
                        catch
                        {
                            // Ignore
                        }
                    }
                    EditorGUILayout.EndToggleGroup();
                }
            }
        }
Пример #13
0
        /// <summary>
        /// Index all the properties of an object.
        /// </summary>
        /// <param name="obj">Object to index.</param>
        /// <param name="documentIndex">Document where the indexed object was found.</param>
        /// <param name="dependencies">Index dependencies.</param>
        protected void IndexObject(int documentIndex, Object obj, bool dependencies = false)
        {
            using (var so = new SerializedObject(obj))
            {
                var p    = so.GetIterator();
                var next = p.Next(true);
                while (next)
                {
                    var fieldName = p.displayName.Replace("m_", "").Replace(" ", "").ToLowerInvariant();
                    var scc       = SearchUtils.SplitCamelCase(fieldName);
                    var fcc       = scc.Length > 1 && fieldName.Length > 10 ? scc.Aggregate("", (current, s) => current + s[0]) : fieldName;

                    switch (p.propertyType)
                    {
                    case SerializedPropertyType.Integer:
                        IndexNumber(documentIndex, fcc, (double)p.intValue);
                        break;

                    case SerializedPropertyType.Boolean:
                        IndexProperty(documentIndex, fcc, p.boolValue.ToString().ToLowerInvariant(), saveKeyword: false, exact: true);
                        break;

                    case SerializedPropertyType.Float:
                        IndexNumber(documentIndex, fcc, (double)p.floatValue);
                        break;

                    case SerializedPropertyType.String:
                        if (!string.IsNullOrEmpty(p.stringValue))
                        {
                            IndexProperty(documentIndex, fcc, p.stringValue.ToLowerInvariant(), saveKeyword: false, exact: p.stringValue.Length >= 16);
                        }
                        break;

                    case SerializedPropertyType.Enum:
                        if (p.enumValueIndex >= 0 && p.type == "Enum")
                        {
                            IndexProperty(documentIndex, fcc, p.enumNames[p.enumValueIndex].Replace(" ", "").ToLowerInvariant(), saveKeyword: true, exact: false);
                        }
                        break;

                    case SerializedPropertyType.Color:
                        IndexProperty(documentIndex, fcc, ColorUtility.ToHtmlStringRGB(p.colorValue).ToLowerInvariant(), saveKeyword: false, exact: true);
                        break;

                    case SerializedPropertyType.Vector2:
                        IndexProperty(documentIndex, fcc, V2S(p.vector2Value), saveKeyword: false, exact: true);
                        break;

                    case SerializedPropertyType.Vector3:
                        IndexProperty(documentIndex, fcc, V2S(p.vector3Value), saveKeyword: false, exact: true);
                        break;

                    case SerializedPropertyType.Vector4:
                        IndexProperty(documentIndex, fcc, V2S(p.vector4Value), saveKeyword: false, exact: true);
                        break;

                    case SerializedPropertyType.ObjectReference:
                        if (p.objectReferenceValue && !string.IsNullOrEmpty(p.objectReferenceValue.name))
                        {
                            IndexProperty(documentIndex, fcc, p.objectReferenceValue.name.ToLowerInvariant(), saveKeyword: false, exact: true);
                        }
                        break;
                    }

                    if (dependencies)
                    {
                        AddReference(documentIndex, p);
                    }

                    next = p.Next(p.hasVisibleChildren && !p.isArray);
                }
            }
        }