Skip to content

Instantly share code, notes, and snippets.

@d12frosted
Forked from winkels/FindDependentAssets.cs
Created May 5, 2017 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d12frosted/63f170017f332c0fb4e5870fe8bba318 to your computer and use it in GitHub Desktop.
Save d12frosted/63f170017f332c0fb4e5870fe8bba318 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment