Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created January 3, 2019 12:47
Show Gist options
  • Select an option

  • Save jeffvella/e7275f15fcba26498b66c836cbb383ba to your computer and use it in GitHub Desktop.

Select an option

Save jeffvella/e7275f15fcba26498b66c836cbb383ba to your computer and use it in GitHub Desktop.
Invert the current Unity project / assets selection
using System;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Object = UnityEngine.Object;
public class InvertSelection : ScriptableWizard
{
[MenuItem("Assets/Selection/Invert", false, 50)]
static void InvertAssetSelection()
{
EditorUtility.DisplayProgressBar("Inverting Selection", "Working...", 0);
var oldSelection = Selection.GetFiltered(typeof(Object), SelectionMode.Assets).ToList();
var newSelection = new List<Object>();
var allAssets = AssetDatabase.FindAssets(string.Empty);
for (var i = 0; i < allAssets.Length; i++)
{
var guid = allAssets[i];
var path = AssetDatabase.GUIDToAssetPath(guid);
var asset = AssetDatabase.LoadAssetAtPath<Object>(path);
if (!oldSelection.Contains(asset))
{
newSelection.Add(asset);
}
var progress = (i / (float) allAssets.Length);
EditorUtility.DisplayProgressBar("Inverting Selection", $"{progress*100f:N0}% Complete", progress);
}
Selection.objects = newSelection.ToArray();
EditorUtility.ClearProgressBar();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment