Last active
October 30, 2023 06:48
-
-
Save pofulu/cc204bcb1b35181bb3c2f46cc8d22d3f to your computer and use it in GitHub Desktop.
快速整理出最新有用到的 bundle
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using Newtonsoft.Json.Linq; | |
using UnityEditor; | |
using UnityEditor.AddressableAssets.Settings; | |
using UnityEngine; | |
/// <summary> | |
/// Editor工具,整理用過、沒用過的Addressable Bundle到資料夾裡 | |
/// </summary> | |
public class AddressableBundleCleaner | |
{ | |
public static AddressableAssetSettings Setting => AssetDatabase.LoadAssetAtPath<AddressableAssetSettings>("Assets/AddressableAssetsData/AddressableAssetSettings.asset"); | |
public static BuildTarget BuildTarget => EditorUserBuildSettings.activeBuildTarget; | |
public static string CatalogFolderPath => Path.Combine(new DirectoryInfo(Application.dataPath).Parent.FullName, Setting.RemoteCatalogBuildPath.GetValue(Setting)); | |
public static string[] BundlePaths => Directory.GetFiles(CatalogFolderPath, "*.bundle"); | |
public static string TimeSuffix => DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(8)).ToString("yyyy-MM-dd_HH-mm-ss"); | |
public static string CatalogPath => Path.Combine(CatalogFolderPath, $"catalog_{Setting.PlayerBuildVersion}.json"); | |
public static string CatalogHashPath => Path.Combine(CatalogFolderPath, $"catalog_{Setting.PlayerBuildVersion}.hash"); | |
public static IEnumerable<string> GetCatalogBundles() | |
{ | |
var text = File.ReadAllText(CatalogPath); | |
var json = JObject.Parse(text); | |
dynamic dyna = json as dynamic; | |
var ary = ((JArray)json["m_InternalIds"]).Cast<dynamic>().ToArray(); | |
return string.Join("/", ary).Split('/').Where(text => text.Contains(".bundle")); | |
} | |
public static IEnumerable<string> GetUnusedBundlePaths() | |
{ | |
var catalogBundlePath = GetCatalogBundles().Select(id => Path.Combine(CatalogFolderPath, id)); | |
return BundlePaths.Except(catalogBundlePath); | |
} | |
public static IEnumerable<string> GetUsedBundlePaths() | |
{ | |
var catalogBundlePath = GetCatalogBundles().Select(id => Path.Combine(CatalogFolderPath, id)); | |
return BundlePaths.Intersect(catalogBundlePath); | |
} | |
[MenuItem("Window/Asset Management/Cleaner/Archive Unused Bundle Files")] | |
public static void ArchiveUnusedBundleFiles() | |
{ | |
var folder = Path.Combine(CatalogFolderPath, $"_archive_{BuildTarget}_{TimeSuffix}"); | |
new DirectoryInfo(folder).Create(); | |
foreach (var oldPath in GetUnusedBundlePaths()) | |
{ | |
var fileName = Path.GetFileName(oldPath); | |
var newPath = Path.Combine(folder, fileName); | |
File.Move(oldPath, newPath); | |
} | |
EditorUtility.RevealInFinder(folder); | |
} | |
[MenuItem("Window/Asset Management/Cleaner/Pick Used Bundle Files")] | |
public static void PickUsedBundleFiles() | |
{ | |
var folder = Path.Combine(CatalogFolderPath, $"_latest_{BuildTarget}_{TimeSuffix}"); | |
new DirectoryInfo(folder).Create(); | |
File.Copy(CatalogPath, Path.Combine(folder, $"catalog_{Setting.PlayerBuildVersion}.json")); | |
File.Copy(CatalogHashPath, Path.Combine(folder, $"catalog_{Setting.PlayerBuildVersion}.hash")); | |
foreach (var oldPath in GetUsedBundlePaths()) | |
{ | |
var fileName = Path.GetFileName(oldPath); | |
var newPath = Path.Combine(folder, fileName); | |
File.Copy(oldPath, newPath); | |
} | |
EditorUtility.RevealInFinder(folder); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
可以在
Window/Asset Management/Cleaner/
找到兩個方法需要 Newtonsoft.Json
將這個 Script 丟到名為 Editor 的資料夾裡