Last active
February 28, 2019 15:36
-
-
Save tsubaki/c1fe3456fc5d33b51c1bbcbe7ecc029a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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