Editor tool for selecting inverse dependencies, no-longer works in Unity 5.4?
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!
Answer by ancher · Oct 11, 2016 at 02:25 PM
Just got the same bug.
Seems that problem appears when invoking EditorUtility.CollectDependencies on *.unity or *.guiskin asssets
However, this bug seems to be auto-fixed after invoking AssetDatabase.Refresh() with any change in a database present.
Here is my workaround:
private static void CrappyUnityBugFix()
{
// Make some changes to asset database and trigger refresh which seems to fix the bug somehow
string dummyXml = Application.dataPath + "/Editor/Dummy.xml";
string dummyXmlMeta = dummyXml + ".meta";
if ( File.Exists( dummyXml ) )
{
File.Delete( dummyXml );
if ( File.Exists( dummyXmlMeta ) )
{
File.Delete( dummyXmlMeta );
}
}
else
{
using ( TextWriter writer = File.CreateText( dummyXml ) )
{
writer.WriteLine( "<?xml version=\"1.0\" encoding=\"utf-8\"?><dummy />" );
}
}
AssetDatabase.Refresh();
}
Your answer
Follow this Question
Related Questions
Editorscript: How to reference prefabs in editor not in awake or start 0 Answers
there is no build fail but editor script doesn't work in build 1 Answer
How to see variables of child class with custom editor 0 Answers
Unity Editor - Group Public GameObjects 0 Answers
Editor script: Variable created on gameObject in editor set to missing on runtime 0 Answers