首次提交

This commit is contained in:
Bob.Song
2026-03-05 18:07:55 +08:00
commit e125bb869e
4534 changed files with 563920 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace NBC.Asset.Editor
{
[Id(TaskId.BuildBundle)]
public class BuildBundleTask : BuildTask
{
private BuildContext _context;
public override void Run(BuildContext context)
{
_context = context;
var bundleDataList = _context.GenBundles();
List<AssetBundleBuild> builds = _context.GetAssetBundleBuilds();
EditUtil.CreateDirectory(BuildSettings.PlatformCachePath);
var manifest = BuildAssetBundles(BuildSettings.PlatformCachePath, builds.ToArray(),
BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
if (manifest != null)
{
var bundles = manifest.GetAllAssetBundles();
foreach (var bundleName in bundles)
{
var bundle = _context.GetBundle(bundleName);
if (bundle != null)
{
var path = BuildSettings.GetCachePath(bundleName);
var hash = Util.ComputeHash(path);
bundle.Dependencies = manifest.GetAllDependencies(bundleName);
bundle.Hash = hash;
bundle.Size = Util.GetFileSize(path);
}
}
}
Histories.AddHistory(bundleDataList);
_context.SaveBundles();
}
private static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds,
BuildAssetBundleOptions options, BuildTarget target)
{
var manifest = BuildPipeline.BuildAssetBundles(outputPath, builds, options, target);
return manifest;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2ed116b0b33144cb94cb189f4234f0ca
timeCreated: 1675823335

View File

@@ -0,0 +1,11 @@
using System;
namespace NBC.Asset.Editor
{
public abstract class BuildTask
{
public abstract void Run(BuildContext context);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4a28b318de2d45ef886f30fc4d1ffac9
timeCreated: 1675822432

View File

@@ -0,0 +1,24 @@
using System.IO;
using UnityEditor;
using UnityEditor.EditorTools;
using UnityEngine;
namespace NBC.Asset.Editor
{
[Id(TaskId.CopyToStreamingAssets)]
public class CopyToStreamingAssets : BuildTask
{
public override void Run(BuildContext context)
{
var last = HistoryUtil.GetLastVersionHistory();
if (last != null)
{
last.CopyToStreamingAssets();
}
else
{
Debug.LogError("copy version is null,version history is null");
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2af47d5da6e844ce97d51af3a3ded489
timeCreated: 1675845425

View File

@@ -0,0 +1,76 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace NBC.Asset.Editor
{
/// <summary>
/// 将生成的版本保存为可用的历史记录
/// </summary>
[Id(TaskId.CopyVersionBundle)]
public class CopyVersionBundleTask : BuildTask
{
private Dictionary<string, string> _copyPaths = new Dictionary<string, string>();
public override void Run(BuildContext context)
{
TryCopy();
}
private void TryCopy()
{
var versionPath = BuildSettings.GetCachePath(Const.VersionFileName);
var versionData = Util.ReadJson<VersionData>(versionPath);
if (IsChange(versionData))
{
var versionPackagePath = BuildSettings.GetCachePath("packages.json");
var lastHistoryData = Histories.LastHistoryData;
_copyPaths.Add(versionPath, BuildSettings.GetBuildPath($"version_{lastHistoryData.Index}.json"));
_copyPaths.Add(versionPackagePath, BuildSettings.GetBuildPath(versionData.NameHash));
var versionPackageData = Util.ReadJson<VersionPackageData>(versionPackagePath);
foreach (var package in versionPackageData.Packages)
{
foreach (var bundle in package.Bundles)
{
var bundleNameAddHash = Util.NameAddHash(bundle.Name, bundle.Hash);
_copyPaths.Add(BuildSettings.GetCachePath(bundle.Name),
BuildSettings.GetBuildPath(bundleNameAddHash));
}
}
CopyAll();
}
}
private void CopyAll()
{
foreach (var path in _copyPaths.Keys)
{
var toPath = _copyPaths[path];
if (File.Exists(path))
{
EditUtil.CreateDirectory(Path.GetDirectoryName(toPath));
if (File.Exists(toPath)) File.Delete(toPath);
File.Copy(path, toPath);
}
}
}
private bool IsChange(VersionData versionData)
{
var lastVersionData = Histories.GetLastVersion();
if (lastVersionData != null)
{
if (versionData.Size != lastVersionData.Size || versionData.Hash != lastVersionData.Hash)
{
return true;
}
return false;
}
return true;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8f706081edbe481ea7e727651d531e0a
timeCreated: 1675823749

View File

@@ -0,0 +1,66 @@
using System;
using System.Linq;
namespace NBC.Asset.Editor
{
[Id(TaskId.Gather)]
public class GatherTask : BuildTask
{
private BuildContext _context;
public override void Run(BuildContext context)
{
_context = context;
GatherAllAssets();
_context.SaveAssets();
}
public void GatherAllAssets()
{
var packages = CollectorSetting.Instance.Packages;
foreach (var package in packages)
{
if (!package.Enable) continue;
foreach (var group in package.Groups)
{
if (group.FilterEnum != FilterEnum.Custom)
{
group.Filter = group.FilterEnum == FilterEnum.All ? "*" : $"t:{group.FilterEnum}";
}
else if (string.IsNullOrEmpty(group.Filter))
{
group.Filter = "*";
}
if (!group.Enable) continue;
var assets = GatherGroupAssets(package, group);
_context.Add(group, assets.ToList());
}
}
foreach (var package in packages)
{
foreach (var group in package.Groups)
{
var tag = group.Tags;
if (string.IsNullOrEmpty(tag)) continue;
var arr = tag.Split(",");
foreach (var s in arr)
{
if (!string.IsNullOrEmpty(s))
{
Defs.AddTag(s);
}
}
}
}
}
private BuildAsset[] GatherGroupAssets(PackageConfig packageConfig, GroupConfig groupConfig)
{
if (groupConfig.Collectors == null || groupConfig.Collectors.Count == 0) return Array.Empty<BuildAsset>();
return Builder.GatherAsset(packageConfig, groupConfig);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 79738d555c83467c954f240b9f3b0d00
timeCreated: 1675823940

View File

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace NBC.Asset.Editor
{
/// <summary>
/// 生成打包的清单文件
/// </summary>
[Id(TaskId.GenPackageData)]
public class GenPackageDataTask : BuildTask
{
private BuildContext _context;
public override void Run(BuildContext context)
{
_context = context;
BuildPackagesData();
}
private void BuildPackagesData()
{
Dictionary<string, BuildPackage> dictionary = new Dictionary<string, BuildPackage>();
foreach (var bundle in _context.Bundles)
{
var package = Path.GetDirectoryName(bundle.Name);
if (string.IsNullOrEmpty(package)) continue;
if (!dictionary.TryGetValue(package, out var buildPackage))
{
buildPackage = new BuildPackage
{
Name = package,
};
dictionary[package] = buildPackage;
}
buildPackage.Bundles.Add(bundle);
}
var packages = CollectorSetting.Instance.Packages;
foreach (var key in dictionary.Keys)
{
var p = packages.Find(p => p.Name == key);
var package = dictionary[key];
package.Size = package.Bundles.Sum(b => b.Size);
package.Def = p.Default ? 1 : 0;
var savePath = BuildSettings.GetCachePath($"{key}.json");
Util.WriteJson(package, savePath);
}
// var packages = CollectorSetting.Instance.Packages;
// foreach (var key in dictionary.Keys)
// {
// var value = dictionary[key];
// var packageData = value.ToPackagesData();
// var savePath = BuildSettings.GetCachePath($"{key}.json");
// Util.WriteJson(packageData, savePath);
// }
//
// foreach (var key in dictionary.Keys)
// {
// var p = packages.Find(p => p.Name == key);
// var packPath = BuildSettings.GetCachePath($"{key}.json");
// VersionPackagesData versionPackagesData = new VersionPackagesData
// {
// Ver = DateTimeOffset.Now.ToUnixTimeSeconds().ToString(),
// Def = p.Default ? 1 : 0,
// Name = key,
// Hash = Util.ComputeHash(packPath),
// Size = Util.GetFileSize(packPath)
// };
// versionData.Packages.Add(versionPackagesData);
// }
//
// var versionSavePath = BuildSettings.GetCachePath("version.json");
// versionData.AppVer = UnityEditor.PlayerSettings.bundleVersion;
// versionData.BuildTime = DateTimeOffset.Now.ToUnixTimeSeconds();
// Util.WriteJson(versionData, versionSavePath);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9dca3963bf3d46f8a3734326ef1cfd1a
timeCreated: 1680321963

View File

@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace NBC.Asset.Editor
{
[Id(TaskId.GenVersionData)]
public class GenVersionDataTask : BuildTask
{
private BuildContext _context;
public override void Run(BuildContext context)
{
_context = context;
BuildVersionData();
}
private void BuildVersionData()
{
VersionData versionData = new VersionData();
var versionPackageData = new VersionPackageData();
var packages = CollectorSetting.Instance.Packages;
// Dictionary<string, PackageTempData> dictionary = new Dictionary<string, PackageTempData>();
foreach (var package in packages)
{
var packPath = BuildSettings.GetCachePath($"{package.Name}.json");
var buildPackage = Util.ReadJson<BuildPackage>(packPath);
if (buildPackage != null)
{
var packageData = buildPackage.ToPackagesData();
versionPackageData.Packages.Add(packageData);
}
}
var versionPackagesPath = BuildSettings.GetCachePath("packages.json");
Util.WriteJson(versionPackageData, versionPackagesPath);
versionData.Hash = Util.ComputeHash(versionPackagesPath);
versionData.Size = Util.GetFileSize(versionPackagesPath);
var versionSavePath = BuildSettings.GetCachePath(Const.VersionFileName);
versionData.AppVer = UnityEditor.PlayerSettings.bundleVersion;
versionData.BuildTime = DateTimeOffset.Now.ToUnixTimeSeconds();
Util.WriteJson(versionData, versionSavePath);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9b4670cdc7f941558a69023329f42de2
timeCreated: 1675837362