Skip to content

Instantly share code, notes, and snippets.

@WikkidEdd
Last active November 19, 2023 11:53
Show Gist options
  • Save WikkidEdd/935b5f017b79acf4d24de76757330060 to your computer and use it in GitHub Desktop.
Save WikkidEdd/935b5f017b79acf4d24de76757330060 to your computer and use it in GitHub Desktop.
Find references within a scene on a right click context menu for components. As per this tweet https://twitter.com/KarlJamesJones/status/1026756113218371586 Note: Only tested with 2017.4.6
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
public class FindSceneRefs {
[MenuItem("CONTEXT/Component/Find References In Scene")]
private static void OnSearchForReferencesToComponent(MenuCommand command)
{
Type searchableEditorWindowType = typeof(SearchableEditorWindow);
FieldInfo hierarhcyType = searchableEditorWindowType.GetField("m_HierarchyType", BindingFlags.Instance | BindingFlags.NonPublic);
FieldInfo hasSearchFilterFocus = searchableEditorWindowType.GetField("m_HasSearchFilterFocus", BindingFlags.Instance | BindingFlags.NonPublic);
FieldInfo searchFilterField = searchableEditorWindowType.GetField("m_SearchFilter", BindingFlags.Instance | BindingFlags.NonPublic);
Type editorApplicationType = typeof(EditorApplication);
MethodInfo CallSearchHasChanged = editorApplicationType.GetMethod("Internal_CallSearchHasChanged", BindingFlags.NonPublic | BindingFlags.Static);
var component = command.context as Component;
SearchableEditorWindow[] searchableWindows = Resources.FindObjectsOfTypeAll<SearchableEditorWindow>();
if (component)
{
var searchFilter = "ref:" + component.GetInstanceID() + ":";
foreach (SearchableEditorWindow sw in searchableWindows)
{
if ((HierarchyType)hierarhcyType.GetValue(sw) == HierarchyType.GameObjects)
{
searchFilterField.SetValue(sw, searchFilter);
CallSearchHasChanged.Invoke(null, new object[0]);
hasSearchFilterFocus.SetValue(sw,true);
sw.Repaint();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment