あるAssetが参照するすべてのAssetを取得する方法です。
Unity2018.3.9
AssetDatabase.GetDependencies
AssetDatabase.GetDependencies()
を使うと参照しているAssetをすべて取得できます。
選択中のAssetが参照しているAssetを得るには次のようにします。
[MenuItem("Assets/Log Dependencies")] public static void LogDependencies() { var assetPath = AssetDatabase.GetAssetPath(Selection.activeObject); foreach (var dependency in AssetDatabase.GetDependencies(assetPath, true)) { Debug.Log(dependency); } }
全ての参照を得るには再帰的に参照検索を行う必要があるため、第二引数をtrueにする必要があります。
こんな感じでAssetのPathが出力されました。
Unityの内部的なAssetも取得できるEditorUtility.CollectDependencies
EditorUtility.CollectDependenciesを使うと、Unityの内部的なAssetも含めて参照を取得できるようです。
例えばこんな感じで使います。
[MenuItem("Assets/Log Dependencies")] public static void LogDependencies() { foreach (var dependency in EditorUtility.CollectDependencies(Selection.objects)) { Debug.Log(dependency); } }
こちらはAssetのパスではなくオブジェクトが得られます。
先ほどと同じAssetに対してこれを使うと以下の出力が得られました。