Last active
May 6, 2024 05:57
-
-
Save kankikuchi/41da6ee4d741e872745e274505c88e58 to your computer and use it in GitHub Desktop.
Addressable Assetsのアドレスの重複を検知する【Unity】【Addressable Assets】
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
// AddressDuplicateDetecter.cs | |
// http://kan-kikuchi.hatenablog.com/entry/AddressDuplicateDetecter | |
// | |
// Created by kan.kikuchi on 2019.09.16. | |
using System.IO; | |
using System.Linq; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEditor.AddressableAssets.Settings; | |
using UnityEngine; | |
/// <summary> | |
/// Addressable Assetsのアドレスの重複を検知するクラス | |
/// </summary> | |
public class AddressDuplicateDetecter : AssetPostprocessor { | |
//監視対象のディレクトリへのパス | |
private static readonly string TARGET_DIRECTORY_PATH = "Assets/AddressableAssetsData/AssetGroups"; | |
//================================================================================= | |
//変更の監視 | |
//================================================================================= | |
#if !UNITY_CLOUD_BUILD | |
//対象のディレクトリ以下の変更をチェック | |
private static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths){ | |
List<string[]> assetsList = new List<string[]> (){ | |
importedAssets, deletedAssets, movedAssets, movedFromAssetPaths | |
}; | |
List<string> targetDirectoryNameList = new List<string> (){ | |
TARGET_DIRECTORY_PATH, | |
}; | |
//変更があったら検知 | |
if(ExistsDirectoryInAssets(assetsList, targetDirectoryNameList)) { | |
Detect(); | |
} | |
} | |
//入力されたassetsのパスの中に、親ディレクトリの名前ががtargetDirectoryNameListのものが一つでもあるか | |
private static bool ExistsDirectoryInAssets(List<string[]> assetPathsList, List<string> targetDirectoryNameList){ | |
return assetPathsList | |
.Any (assetPaths => assetPaths | |
.Select(Path.GetDirectoryName) | |
.Intersect(targetDirectoryNameList) | |
.Any() | |
); | |
} | |
#endif | |
//================================================================================= | |
//作成 | |
//================================================================================= | |
//アドレスの重複を検知 | |
[MenuItem("Tools/Detect/Address Duplicate")] | |
private static void Detect() { | |
//アドレスとそのグループをまとめるやつ | |
var addressGroupDict = new Dictionary<string, string>(); | |
//対象のディレクトリ以下のファイルを全て取得 | |
foreach (var filePath in Directory.GetFiles (TARGET_DIRECTORY_PATH, "*", SearchOption.AllDirectories)) { | |
//AddressableAssetGroupだけをチェック | |
var group = AssetDatabase.LoadAssetAtPath<AddressableAssetGroup>(filePath); | |
if(group == null){ | |
continue; | |
} | |
//グループ名取得 | |
var groupName = group.Name; | |
//グループに設定されているものを全てチェック | |
foreach (var entry in group.entries) { | |
var address = entry.address; | |
//重複チェック | |
if (addressGroupDict.ContainsKey(address)) { | |
Debug.LogError($"{address}というアドレスが重複しています。(グループは{addressGroupDict[address]}と{groupName})"); | |
} | |
addressGroupDict[address] = groupName; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment