Skip to content

Instantly share code, notes, and snippets.

@pofulu
Last active October 30, 2023 06:48
Show Gist options
  • Save pofulu/cc204bcb1b35181bb3c2f46cc8d22d3f to your computer and use it in GitHub Desktop.
Save pofulu/cc204bcb1b35181bb3c2f46cc8d22d3f to your computer and use it in GitHub Desktop.
快速整理出最新有用到的 bundle
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);
}
}
@pofulu
Copy link
Author

pofulu commented Sep 16, 2021

  1. 可以在 Window/Asset Management/Cleaner/ 找到兩個方法

    • Archive Unused Bundle Files: 把舊的沒用到的 bundle 移動到一個資料夾裡
    • Pick Used Bundle Files: 把最新有用到的 bundle + catalog 複製到一個新資料夾裡
  2. 需要 Newtonsoft.Json

  3. 將這個 Script 丟到名為 Editor 的資料夾裡

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