Skip to content

Instantly share code, notes, and snippets.

@winkels
Created November 17, 2014 05:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save winkels/0b3f556e395c1c428d70 to your computer and use it in GitHub Desktop.
Save winkels/0b3f556e395c1c428d70 to your computer and use it in GitHub Desktop.
Unity editor script to find all assets that depend on the currently selected asset (i.e. assets that have serialized references to the selected asset). NOTE: Currently only works on OS X.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class FindDependentAssets
{
[MenuItem("Assets/Find Dependent Assets")]
public static void FindAssets()
{
UnityEngine.Object selectedObject = Selection.activeObject;
string[] paths = FindFilesReferringToGUID(AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(selectedObject)));
for(int i = 0; i < paths.Length; i++)
{
paths[i] = paths[i].Replace(Application.dataPath, "Assets");
}
List<string> processedPaths = paths.Where(path => !path.Contains(".meta")).ToList();
List<UnityEngine.Object> dependentAssets = new List<UnityEngine.Object>(processedPaths.Count);
foreach(string path in processedPaths)
{
dependentAssets.Add(AssetDatabase.LoadMainAssetAtPath(path));
}
FindDependentAssetsWindow window = EditorWindow.GetWindowWithRect<FindDependentAssetsWindow>(new Rect(0, 0, 640, 480), true, "Dependent Assets");
window.Setup(selectedObject, dependentAssets);
}
[MenuItem("Assets/Find Dependent Assets", true)]
public static bool FindAssetsValidation()
{
return Application.platform == RuntimePlatform.OSXEditor && Selection.activeObject != null;
}
public static string[] FindFilesReferringToGUID(string assetGUID)
{
System.Diagnostics.ProcessStartInfo mdfindInfo = new System.Diagnostics.ProcessStartInfo();
mdfindInfo.CreateNoWindow = true;
mdfindInfo.RedirectStandardError = true;
mdfindInfo.RedirectStandardOutput = true;
mdfindInfo.UseShellExecute = false;
mdfindInfo.FileName = "mdfind";
System.Diagnostics.Process mdfindProcess = new System.Diagnostics.Process();
mdfindInfo.Arguments = assetGUID + " -onlyin .";
mdfindInfo.WorkingDirectory = Application.dataPath;
mdfindProcess.StartInfo = mdfindInfo;
mdfindProcess.Start();
string stderr_str = mdfindProcess.StandardError.ReadToEnd();
string stdout_str = mdfindProcess.StandardOutput.ReadToEnd();
mdfindProcess.WaitForExit();
mdfindProcess.Close();
if(stderr_str.Trim().Length > 0)
{
throw new System.Exception("Error getting git hash: " + stderr_str);
}
stdout_str = stdout_str.Trim();
string[] paths = stdout_str.Split(System.Environment.NewLine.ToCharArray());
return paths;
}
}
@nvg58
Copy link

nvg58 commented Feb 25, 2015

Dear winkels,

I wonder where can i find this class FindDependentAssetsWindow.

Best regards,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment