Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
自动分包; 自动清理多余文件;
Browse files Browse the repository at this point in the history
  • Loading branch information
ialex32x committed Jul 4, 2019
1 parent 00a92ac commit 739d76c
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Assets/Examples/Prefabs/New Material 1.mat
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Material:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: New Material
m_Name: New Material 1
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 4
Expand Down
200 changes: 161 additions & 39 deletions Assets/UnityFS/Editor/BundleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static void Scan(BundleBuilderData data, BundleBuilderData.BundleInfo bun
var extensions = target.extensions?.Split(';');
var filter = new AssetFilter()
{
size = bundle.splitObjects,
extensions = extensions,
types = target.types,
};
Expand Down Expand Up @@ -138,10 +139,36 @@ public static void Scan(BundleBuilderData data, BundleBuilderData.BundleInfo bun
{
if (!ContainsAsset(data, asset))
{
bundle.assets.Add(new BundleBuilderData.BundleAsset()
var index = 0;
BundleBuilderData.BundleSplit split = null;
if (bundle.splits.Count > 0)
{
target = asset,
});
index = bundle.splits.Count - 1;
split = bundle.splits[index];
}
if (split == null || filter.size >= 1 && split.assets.Count >= filter.size)
{
split = new BundleBuilderData.BundleSplit();
index = bundle.splits.Count;
if (filter.size == 0)
{
split.name = bundle.name;
}
else
{
var dot = bundle.name.LastIndexOf('.');
if (dot >= 0)
{
split.name = bundle.name.Substring(0, dot) + "_" + index + bundle.name.Substring(dot);
}
else
{
split.name = bundle.name + "_" + index;
}
}
bundle.splits.Add(split);
}
split.assets.Add(asset);
}
}
}
Expand All @@ -155,19 +182,31 @@ public static AssetBundleBuild[] GenerateAssetBundleBuilds(BundleBuilderData dat
{
continue;
}
var build = new AssetBundleBuild();
build.assetBundleName = bundle.name;
var assetNames = new List<string>();
var assetPaths = new List<string>();
foreach (var asset in bundle.assets)
for (var splitIndex = 0; splitIndex < bundle.splits.Count; splitIndex++)
{
var assetPath = AssetDatabase.GetAssetPath(asset.target);
assetNames.Add(assetPath);
assetPaths.Add(assetPath);
var bundleSplit = bundle.splits[splitIndex];
var assetNames = new List<string>();
for (var assetIndex = 0; assetIndex < bundleSplit.assets.Count; assetIndex++)
{
var asset = bundleSplit.assets[assetIndex];
var assetPath = AssetDatabase.GetAssetPath(asset);
assetNames.Add(assetPath);
}
if (assetNames.Count != 0)
{
var names = assetNames.ToArray();
var build = new AssetBundleBuild();
build.assetBundleName = bundleSplit.name;
build.assetNames = names;
build.addressableNames = names;
builds.Add(build);
// Debug.Log($"{build.assetBundleName}: {build.assetNames.Length}");
}
else
{
Debug.LogWarning($"empty build split {bundle.name}_{splitIndex}");
}
}
build.assetNames = assetNames.ToArray();
build.addressableNames = assetPaths.ToArray();
builds.Add(build);
}
return builds.ToArray();
}
Expand All @@ -177,7 +216,7 @@ public static bool Scan(BundleBuilderData data, BuildTarget targetPlatform)
{
foreach (var bundle in data.bundles)
{
bundle.assets.Clear();
bundle.splits.Clear();
}
foreach (var bundle in data.bundles)
{
Expand Down Expand Up @@ -222,9 +261,62 @@ public static void Build(BundleBuilderData data, string outputPath, BuildTarget
zipArchiveManifest = BuildZipArchives(outputPath, zipArchiveBuilds, targetPlatform);
}
BuildManifest(data, outputPath, assetBundleManifest, zipArchiveManifest);
Cleanup(outputPath, assetBundleManifest, zipArchiveManifest);
Debug.Log($"build bundles finished {DateTime.Now}. {assetBundleBuilds.Length} assetbundles. {zipArchiveBuilds.Length} zip archives.");
}

private static void Cleanup(string outputPath, AssetBundleManifest assetBundleManifest, ZipArchiveManifest zipArchiveManifest)
{
foreach (var file in Directory.GetFiles(outputPath))
{
var fi = new FileInfo(file);
var match = false;
if (
fi.Name == "AssetBundles" ||
fi.Name == "AssetBundles.manifest" ||
fi.Name == "checksum.txt" ||
fi.Name == "manifest.json"
)
{
match = true;
}
if (!match)
{
foreach (var assetBundle in assetBundleManifest.GetAllAssetBundles())
{
if (fi.Name == assetBundle || fi.Name == assetBundle + ".manifest")
{
match = true;
break;
}
}
}
if (!match)
{
foreach (var zipArchive in zipArchiveManifest.archives)
{
if (fi.Name == zipArchive.name)
{
match = true;
break;
}
}
}
if (!match)
{
// Debug.LogWarning("delete unused file: " + fi.Name);
try
{
fi.Delete();
}
catch (Exception exception)
{
Debug.LogError(exception);
}
}
}
}

public static ZipArchiveManifest BuildZipArchives(string outputPath, BundleBuilderData.BundleInfo[] builds, BuildTarget targetPlatform)
{
var manifest = new ZipArchiveManifest();
Expand All @@ -239,9 +331,12 @@ public static ZipArchiveManifest BuildZipArchives(string outputPath, BundleBuild
using (var zip = new ZipOutputStream(File.OpenWrite(filename)))
{
zip.IsStreamOwner = true;
foreach (var asset in bundle.assets)
foreach (var split in bundle.splits)
{
BuildZipArchiveObject(zip, asset.target, archiveEntry);
foreach (var asset in split.assets)
{
BuildZipArchiveObject(zip, asset, archiveEntry);
}
}
zip.Close();
}
Expand Down Expand Up @@ -325,6 +420,26 @@ public static BundleBuilderData.BundleInfo GetBundleInfo(BundleBuilderData data,
return null;
}

// 获取指定包名的包对象信息
public static bool TryGetBundleSplit(BundleBuilderData data, string bundleName, out BundleBuilderData.BundleInfo bundleInfo, out BundleBuilderData.BundleSplit bundleSplit)
{
foreach (var bundle in data.bundles)
{
foreach (var split in bundle.splits)
{
if (split.name == bundleName)
{
bundleInfo = bundle;
bundleSplit = split;
return true;
}
}
}
bundleInfo = null;
bundleSplit = null;
return false;
}

// 生成清单
public static void BuildManifest(BundleBuilderData data, string outputPath,
AssetBundleManifest assetBundleManifest,
Expand All @@ -336,28 +451,32 @@ public static void BuildManifest(BundleBuilderData data, string outputPath,
var assetBundles = assetBundleManifest.GetAllAssetBundles();
foreach (var assetBundle in assetBundles)
{
var bundleInfo = GetBundleInfo(data, assetBundle);
// Debug.Log(bundleInfo.name);
var assetBundlePath = Path.Combine(outputPath, assetBundle);
using (var stream = File.OpenRead(assetBundlePath))
BundleBuilderData.BundleInfo bundleInfo;
BundleBuilderData.BundleSplit bundleSplit;
if (TryGetBundleSplit(data, assetBundle, out bundleInfo, out bundleSplit))
{
var fileInfo = new FileInfo(assetBundlePath);
var checksum = new Utils.Crc16();
checksum.Update(stream);
var bundle = new Manifest.BundleInfo();
bundle.type = Manifest.BundleType.AssetBundle;
bundle.name = assetBundle;
bundle.checksum = checksum.hex;
bundle.size = (int)fileInfo.Length;
bundle.startup = bundleInfo.load == BundleLoad.Startup;
bundle.priority = bundleInfo.priority;
foreach (var asset in bundleInfo.assets)
// Debug.Log(bundleInfo.name);
var assetBundlePath = Path.Combine(outputPath, assetBundle);
using (var stream = File.OpenRead(assetBundlePath))
{
var assetPath = AssetDatabase.GetAssetPath(asset.target);
bundle.assets.Add(assetPath);
var fileInfo = new FileInfo(assetBundlePath);
var checksum = new Utils.Crc16();
checksum.Update(stream);
var bundle = new Manifest.BundleInfo();
bundle.type = Manifest.BundleType.AssetBundle;
bundle.name = bundleSplit.name;
bundle.checksum = checksum.hex;
bundle.size = (int)fileInfo.Length;
bundle.startup = bundleInfo.load == BundleLoad.Startup;
bundle.priority = bundleInfo.priority;
foreach (var asset in bundleSplit.assets)
{
var assetPath = AssetDatabase.GetAssetPath(asset);
bundle.assets.Add(assetPath);
}
bundle.dependencies = assetBundleManifest.GetAllDependencies(assetBundle);
manifest.bundles.Add(bundle);
}
bundle.dependencies = assetBundleManifest.GetAllDependencies(assetBundle);
manifest.bundles.Add(bundle);
}
}
}
Expand Down Expand Up @@ -418,11 +537,14 @@ public static bool ContainsAsset(BundleBuilderData data, Object assetObject)
{
foreach (var bundle in data.bundles)
{
foreach (var asset in bundle.assets)
foreach (var split in bundle.splits)
{
if (asset.target == assetObject)
foreach (var asset in split.assets)
{
return true;
if (asset == assetObject)
{
return true;
}
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions Assets/UnityFS/Editor/BundleBuilderData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ public class BundleAssetTarget
public string extensions = string.Empty; // (仅搜索目录时) 额外包含指定后缀的文件
}

[Serializable]
public class BundleAsset
public class BundleSplit
{
public int id;
public Object target;
public int splitIndex;
public string name; // 分包名
public List<Object> assets = new List<Object>(); // 最终进入打包的所有资源对象
}

[Serializable]
Expand All @@ -47,7 +45,7 @@ public class BundleInfo
public List<BundleAssetTarget> targets = new List<BundleAssetTarget>(); // 打包目标 (可包含文件夹)

[NonSerialized]
public List<BundleAsset> assets = new List<BundleAsset>(); // 最终进入打包的所有资源对象
public List<BundleSplit> splits = new List<BundleSplit>();
}

public int id;
Expand Down Expand Up @@ -78,8 +76,9 @@ public void MarkAsDirty()
}
}

public struct AssetFilter
public class AssetFilter
{
public int size;
public string[] extensions;
public BundleAssetTypes types;
}
Expand Down
31 changes: 21 additions & 10 deletions Assets/UnityFS/Editor/BundleReportWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class BundleAssetsWindow : EditorWindow
{
private BundleBuilderData _data;
private IList<BundleBuilderData.BundleInfo> _bundles;
private Vector2 _sv;

private BuildTarget _targetPlatform;

Expand All @@ -36,6 +37,7 @@ void OnGUI()
EditorGUILayout.HelpBox("Nothing", MessageType.Warning);
return;
}
_sv = GUILayout.BeginScrollView(_sv);
var rescan = false;
GUILayout.BeginHorizontal();
var targetPlatform = (BuildTarget)EditorGUILayout.EnumPopup("Preview Platform", _targetPlatform);
Expand All @@ -61,26 +63,35 @@ void OnGUI()
foreach (var bundle in _bundles)
{
var bundleName = string.IsNullOrEmpty(bundle.name) ? "(null)" : bundle.name;
EditorGUILayout.HelpBox($"{bundleName}, {bundle.assets.Count} assets", MessageType.Info);
var note = EditorGUILayout.TextField("Note", bundle.note);
EditorGUILayout.HelpBox($"{bundleName}", MessageType.Info);
var note = EditorGUILayout.TextField(bundle.note);
if (note != bundle.note)
{
bundle.note = note;
_data.MarkAsDirty();
}
foreach (var asset in bundle.assets)
GUILayout.Space(20f);
for (var splitIndex = 0; splitIndex < bundle.splits.Count; splitIndex++)
{
EditorGUILayout.BeginHorizontal();
var assetPath = string.Empty;
if (asset.target != null)
var split = bundle.splits[splitIndex];
for (var assetIndex = 0; assetIndex < split.assets.Count; assetIndex++)
{
assetPath = AssetDatabase.GetAssetPath(asset.target);
var asset = split.assets[assetIndex];
var assetPath = string.Empty;
if (asset != null)
{
assetPath = AssetDatabase.GetAssetPath(asset);
}
EditorGUILayout.BeginHorizontal();
EditorGUILayout.IntField(splitIndex, GUILayout.Width(30f));
EditorGUILayout.TextField(assetPath);
EditorGUILayout.ObjectField(asset, typeof(Object), false);
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.TextField(assetPath);
EditorGUILayout.ObjectField(asset.target, typeof(Object), false);
EditorGUILayout.EndHorizontal();
}
GUILayout.Space(50f);
}
GUILayout.EndScrollView();
}
}
}
Loading

0 comments on commit 739d76c

Please sign in to comment.