LoginSignup
1
2

More than 5 years have passed since last update.

[Unity] 複数のアセットから参照されているアセットを見つけるエディタ拡張

Last updated at Posted at 2019-04-15

概要

Projectビューで選択中のアセットから共通して参照されているアセットを参照されている数が多い順にログに出力するスクリプト。

using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public static class AssetUtility
{
    [MenuItem("Assets/GetCommonDependencies")]
    public static void GetCommonDependenciesInSelection()
    {
        var assetPaths = Selection.assetGUIDs.Select(guid => AssetDatabase.GUIDToAssetPath(guid)).Distinct();
        GetCommonDependencies(assetPaths);
    }

    public static void GetCommonDependencies(IEnumerable<string> assetPaths)
    {
        var dependencies = new List<string[]>();
        foreach (var assetPath in assetPaths)
        {
            dependencies.Add(AssetDatabase.GetDependencies(assetPath, true));
        }

        var assetPathCounts = new Dictionary<string, int>();

        foreach (var temp in dependencies)
        {
            foreach (var assetPath in temp)
            {
                int count;
                assetPathCounts.TryGetValue(assetPath, out count);
                assetPathCounts[assetPath] = count + 1;
            }
        }

        foreach (var kvp in assetPathCounts.OrderByDescending(kvp => kvp.Value))
        {
            if (kvp.Value > 1)
            {
                Debug.Log($"{kvp.Key}: {kvp.Value} references");
            }
        }
    }
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2