Skip to content

Instantly share code, notes, and snippets.

@tsubaki tsubaki/TagProvider.cs
Last active Feb 28, 2019

Embed
What would you like to do?
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.QuickSearch;
using UnityEngine;
using System;
using UnityEditor;
using UnityEditorInternal;
public class TagProvider : MonoBehaviour
{
internal static string type = "tag";
internal static string displayName = "Tag";
[SearchItemProvider]
internal static SearchProvider CreateProvider()
{
return new SearchProvider(type, displayName)
{
filterId = "tag:",
fetchItems = (context, items, provider) =>
{
if(InternalEditorUtility.tags.Contains(context.searchQuery))
items.AddRange(
FindObjectsOfType(typeof(GameObject)).Cast<GameObject>()
.Where(go => go.CompareTag(context.searchQuery))
.Select(go =>
{
var id = go.GetInstanceID().ToString();
return provider.CreateItem(id, label:go.name, data:go);
})
);
},
fetchThumbnail = (item, context) =>
{
if (item.thumbnail)
return item.thumbnail;
var obj = ObjectFromItem(item);
if (obj != null)
{
item.thumbnail = PrefabUtility.GetIconForGameObject(obj);
if (item.thumbnail)
return item.thumbnail;
item.thumbnail = EditorGUIUtility.ObjectContent(obj, obj.GetType()).image as Texture2D;
}
return item.thumbnail;
},
startDrag = (item, context) =>
{
var obj = ObjectFromItem(item);
if (obj != null)
{
DragAndDrop.PrepareStartDrag();
DragAndDrop.objectReferences = new[] { obj };
DragAndDrop.StartDrag("Drag scene object");
}
},
isItemValid = item => ObjectFromItem(item) != null
};
}
[SearchActionsProvider]
internal static IEnumerable<SearchAction> ActionHandlers()
{
return new SearchAction[]
{
new SearchAction(type, "select", null, "Select object in scene...") {
handler = (item, context) =>
{
var obj = ObjectFromItem(item);
if (obj != null)
{
Selection.activeGameObject = obj;
EditorGUIUtility.PingObject(obj);
SceneView.lastActiveSceneView.FrameSelected();
}
}
}
};
}
private static GameObject ObjectFromItem(SearchItem item)
{
var instanceID = Convert.ToInt32(item.id);
var obj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.