Skip to content

Instantly share code, notes, and snippets.

@ahokinson
Last active March 13, 2017 20:43
Show Gist options
  • Save ahokinson/77e2556fcd404d3698a3654fb6bc04fd to your computer and use it in GitHub Desktop.
Save ahokinson/77e2556fcd404d3698a3654fb6bc04fd to your computer and use it in GitHub Desktop.
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace Gubbins.Editor
{
public class Selections
{
[MenuItem("Tools/Gubbins/Selections/Objects with Missing Scripts")]
public static void SelectMissing()
{
Selection.objects = Object.FindObjectsOfType<Transform>()
.Where(t => t.GetComponents<Component>()
.Any(c => c == null))
.Select(t => t.gameObject as Object)
.ToArray();
}
public static void SelectObjectsWith<T>() where T : Component
{
Selection.objects = Object.FindObjectsOfType<Transform>()
.Where(t => t.GetComponent<T>() != null)
.Select(t => t.gameObject as Object)
.ToArray();
}
[MenuItem("Tools/Gubbins/Selections/Objects with Selected Component")]
private static void SelectWithComponent()
{
var ms = Selection.activeObject as MonoScript;
var type = ms.GetClass();
if (!type.IsSubclassOf(typeof(Component))) return;
var method = typeof(Selections).GetMethod("SelectObjectsWith");
var typeMethod = method.MakeGenericMethod(type);
typeMethod.Invoke(new Selections(), new object[] {});
}
[MenuItem("Tools/Gubbins/Selections/Objects with Selected Component", true)]
private static bool ValidateSelectWithComponent()
{
return Selection.objects.Length == 1 && Selection.activeObject is MonoScript;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment