Created
November 17, 2014 05:20
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Dear winkels,
I wonder where can i find this class FindDependentAssetsWindow.
Best regards,