Hey guys
So for cleaning up in our project I wrote an editor tool that can select “inverse dependencies” (call them dependants or dependees) of an object…
It works by first collecting all assets of the project, then going over them and checking their dependencies against the selected object.
This gives you a collection of objects that depend on the selected asset, and if that collection is empty, the assumption is that the selected object can be safely removed from the project, as it is no longer referenced/used.
My problem now is that after upgrading to Unity 5.4, the tool no-longer works… It will result in a weird editor error, where all text disappears and the editor has to be restarted… like so:
Here is the code for the tool:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.Linq;
public class SelectDependants : Editor
{
private static bool _checking;
private static float _progress;
private static string _status;
private static Object selectedObject;
private static Object[] selectedObjects = new Object[]{};
private static string[] allAssetPaths = new string[]{};
private static List<Object> allObjects = new List<Object>();
[MenuItem("Assets/Select Dependants")]
private static void FindDependants()
{
// Get selected object
//selectedObject = AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(Selection.activeInstanceID), typeof(Object));
selectedObjects = Selection.objects;
// Gather all objects
allAssetPaths = AssetDatabase.GetAllAssetPaths();
//Progress bar info
_checking = true;
_progress = 0f;
_status = "Collecting all assets...";
int iterator = 0;
int listSize = allAssetPaths.Length;
foreach (var item in allAssetPaths)
{
if(_checking)
{
iterator++;
if (iterator % 100 == 0)
{
_progress = (float)iterator / listSize;
if (EditorUtility.DisplayCancelableProgressBar("Checking assets", _status, _progress))
{
_checking = false;
EditorUtility.ClearProgressBar();
}
}
Object obj = AssetDatabase.LoadAssetAtPath(item, typeof(Object)) as Object;
if (obj)
{
allObjects.Add(obj);
}
}
else
{
return;
}
}
//Reset progress bar info
_progress = 0f;
_status = "Comparing dependencies...";
listSize = allObjects.Count;
iterator = 0;
// Find matches and select them
List<Object> matches = new List<Object>();
foreach (var assetObject in allObjects)
{
if (_checking)
{
iterator++;
if (iterator % 100 == 0)
{
_progress = (float)iterator / listSize;
if (EditorUtility.DisplayCancelableProgressBar("Checking assets", _status, _progress))
{
_checking = false;
EditorUtility.ClearProgressBar();
}
}
Object[] dependencies = EditorUtility.CollectDependencies(new Object[] { assetObject });
foreach (var dependency in dependencies)
{
if (selectedObjects.Contains(dependency) && !selectedObjects.Contains(assetObject))
{
matches.Add(assetObject);
}
}
}
else
{
return;
}
}
if (matches.Count == 0)
{
EditorUtility.DisplayDialog("No dependants", "No assets depend on the selection", "Ok");
}
//Clear pogress bar
_checking = false;
EditorUtility.ClearProgressBar();
Selection.objects = matches.ToArray();
}
[MenuItem("Assets/Select Dependants", true)]
static bool ValidateFindDependants()
{
return Selection.objects.Length > 0;
}
}
Any help in what might be going wrong here is greatly appreciated!