Skip to content

Instantly share code, notes, and snippets.

@monry
Created September 5, 2018 09:05
Show Gist options
  • Save monry/f685a25afae495bd93ae1c7ea49d6ece to your computer and use it in GitHub Desktop.
Save monry/f685a25afae495bd93ae1c7ea49d6ece to your computer and use it in GitHub Desktop.
Method to return AssetImporter instance of all selected Asset in Unity Project View
public static IEnumerable<AssetImporter> CollectAssetImporters()
{
if (Selection.objects.Length == 0)
{
throw new InvalidOperationException("Please select target assets or directories.");
}
return Selection
.objects
// 選択済要素をパスに変換
.Select(AssetDatabase.GetAssetPath)
// ディレクトリのみに絞り込み
.Where(AssetDatabase.IsValidFolder)
// ディレクトリ以下の全アセットを検索
.SelectMany(x => AssetDatabase.FindAssets("*", new[] {x}))
// GUID が返ってくるので、パスに変換
.Select(AssetDatabase.GUIDToAssetPath)
// ディレクトリを除外
.Where(x => !AssetDatabase.IsValidFolder(x))
// 選択済要素のウチ、ディレクトリ以外の要素をリストに追加
.Concat(
Selection
.objects
.Select(AssetDatabase.GetAssetPath)
.Where(x => !AssetDatabase.IsValidFolder(x))
)
.Select(AssetImporter.GetAtPath);
}
@monry
Copy link
Author

monry commented Sep 5, 2018

最終行の .Select を外せばパス一覧が取得出来る。

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