I have an editor script that finds objects in the scene which reference the currently selected object, adds them to a List and then sets Selection.objects to those objects:
foreach (UnityObject sceneObject in sceneObjects)
{
// Iterate over the fields in the object
FieldInfo[] fInfos = sceneObject.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
bool addedObjectToReferences = false;
foreach (FieldInfo fInfo in fInfos)
{
bool fieldReferencesValue = false;
SysObject fieldValue = fInfo.GetValue(sceneObject);
if (fieldValue == selection)
{
fieldReferencesValue = true;
}
else if (fInfo.FieldType.IsArray && fInfo.FieldType.GetElementType().IsAssignableFrom(selectionType))
{
// This is an array field, check each of it's elements for a reference
foreach (System.Object element in (System.Object[]) fieldValue)
{
if (element == selection)
{
fieldReferencesValue = true;
break;
}
}
}
if (fieldReferencesValue)
{
if (!addedObjectToReferences)
{
EditorGUIUtility.PingObject(sceneObject);
references.Add(sceneObject);
addedObjectToReferences = true;
}
// Log with context for easy use
Log.Log(string.Format("{0} referenced by {1} ({2}.{3})", selection, sceneObject.name, sceneObject.GetType().Name, fInfo.Name), sceneObject);
}
}
}
if (references.Count == 0)
{
EditorUtility.DisplayDialog("No references", "No references found to selection", "Ok");
}
else
{
Log.Log(string.Format("Found {0} references to {1}", references.Count, selection.name));
Selection.objects = references.ToArray();
}
For the most part this works beautifully, the references are found and selected (I can tell they are selected by the content of the inspector), but they are not hilighted in the Hierarchy. Am I missing something here? Is there some way to force the editor to highlight the current selection(s)?