Skip to content

Instantly share code, notes, and snippets.

@TrinketBen
Created January 10, 2024 15:44
Show Gist options
  • Save TrinketBen/27ce3ab448a9a7604ad252f1530fde3c to your computer and use it in GitHub Desktop.
Save TrinketBen/27ce3ab448a9a7604ad252f1530fde3c to your computer and use it in GitHub Desktop.
Adds a right-click context menu item to all components that sets the Hierarchy search filter to find all components of the selected type in the scene.
using System.Reflection;
using UnityEditor;
using UnityEngine;
#if UNITY_EDITOR
namespace Trinket.StaticUtilities {
public static class ComponentContextMenu {
public enum FilterMode : int {
All,
Name,
Type
}
[MenuItem("CONTEXT/Component/Find Components in Scene")]
static void Context_LayoutChildrenHorizontally(MenuCommand command) {
var component = (Component)command.context;
if(component == null)
return;
SetSearchFilter($"t:{component.GetType()}", FilterMode.Type);
}
public static void SetSearchFilter(string inFilter, FilterMode inFilterMode) {
SearchableEditorWindow hierarchyWindow = null;
var windows = (SearchableEditorWindow[])Resources.FindObjectsOfTypeAll(typeof(SearchableEditorWindow));
foreach(var window in windows) {
if(window.GetType().ToString() != "UnityEditor.SceneHierarchyWindow")
continue;
hierarchyWindow = window;
break;
}
if(hierarchyWindow == null)
return;
var setSearchType = typeof(SearchableEditorWindow).GetMethod("SetSearchFilter", BindingFlags.NonPublic | BindingFlags.Instance);
if(setSearchType == null)
return;
var parameters = new object[] { inFilter, (int)inFilterMode, false, false };
setSearchType.Invoke(hierarchyWindow, parameters);
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment