首次提交
This commit is contained in:
99
Assets/Scripts/NBC/Asset/Editor/Builder/BuildContext.cs
Normal file
99
Assets/Scripts/NBC/Asset/Editor/Builder/BuildContext.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
|
||||
namespace NBC.Asset.Editor
|
||||
{
|
||||
public class BuildContext
|
||||
{
|
||||
public List<BuildAsset> Assets = new List<BuildAsset>();
|
||||
public List<BuildBundle> Bundles = new List<BuildBundle>();
|
||||
public List<string> Packages = new List<string>();
|
||||
|
||||
public void Add(GroupConfig groupConfig, List<BuildAsset> assets)
|
||||
{
|
||||
foreach (var asset in assets)
|
||||
{
|
||||
asset.Group = groupConfig;
|
||||
AddOrUpdate(asset);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddOrUpdate(BuildAsset asset)
|
||||
{
|
||||
Assets.Add(asset);
|
||||
}
|
||||
|
||||
public void AddBundle(BuildBundle bundle)
|
||||
{
|
||||
Bundles.Add(bundle);
|
||||
}
|
||||
|
||||
public BuildBundle GetBundle(string name)
|
||||
{
|
||||
return Bundles.FirstOrDefault(bundle => bundle.Name == name);
|
||||
}
|
||||
|
||||
public List<BuildBundle> GenBundles()
|
||||
{
|
||||
Bundles.Clear();
|
||||
|
||||
Dictionary<string, BuildBundle> dictionary = new Dictionary<string, BuildBundle>();
|
||||
foreach (var asset in Assets)
|
||||
{
|
||||
if (!dictionary.TryGetValue(asset.Bundle, out var bundle))
|
||||
{
|
||||
bundle = new BuildBundle
|
||||
{
|
||||
Name = asset.Bundle,
|
||||
Tags = asset.Tags
|
||||
};
|
||||
dictionary[bundle.Name] = bundle;
|
||||
}
|
||||
|
||||
bundle.AddAsset(asset);
|
||||
}
|
||||
|
||||
foreach (var bundle in dictionary.Values)
|
||||
{
|
||||
AddBundle(bundle);
|
||||
}
|
||||
|
||||
return Bundles;
|
||||
}
|
||||
|
||||
public void SaveAssets()
|
||||
{
|
||||
var cache = Caches.Get();
|
||||
cache.ClearAssets();
|
||||
cache.AddOrUpdate(Assets);
|
||||
cache.Save();
|
||||
}
|
||||
|
||||
public void SaveBundles()
|
||||
{
|
||||
var cache = Caches.Get();
|
||||
cache.ClearBundles();
|
||||
cache.AddOrUpdate(Bundles);
|
||||
cache.Save();
|
||||
}
|
||||
|
||||
public List<AssetBundleBuild> GetAssetBundleBuilds()
|
||||
{
|
||||
List<AssetBundleBuild> builds = new List<AssetBundleBuild>();
|
||||
|
||||
for (int i = 0; i < Bundles.Count; i++)
|
||||
{
|
||||
var bundle = Bundles[i];
|
||||
var build = new AssetBundleBuild
|
||||
{
|
||||
assetNames = bundle.GetAssetNames(),
|
||||
assetBundleName = bundle.Name
|
||||
};
|
||||
builds.Add(build);
|
||||
}
|
||||
|
||||
return builds;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 334b2db512674787abcafd936a43c2b0
|
||||
timeCreated: 1678170411
|
||||
138
Assets/Scripts/NBC/Asset/Editor/Builder/Builder.cs
Normal file
138
Assets/Scripts/NBC/Asset/Editor/Builder/Builder.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using Debug = UnityEngine.Debug;
|
||||
|
||||
namespace NBC.Asset.Editor
|
||||
{
|
||||
public class Builder
|
||||
{
|
||||
private static readonly Dictionary<BundleMode, Type> _gatherTypes = new Dictionary<BundleMode, Type>();
|
||||
private static readonly Dictionary<int, Type> _buildTaskTypes = new Dictionary<int, Type>();
|
||||
|
||||
#region 内部方法
|
||||
|
||||
[InitializeOnLoadMethod]
|
||||
private static void OnInitialize()
|
||||
{
|
||||
FindGathers();
|
||||
FindTasks();
|
||||
}
|
||||
|
||||
private static void FindGathers()
|
||||
{
|
||||
var types = EditUtil.FindAllSubclass<GatherBase>();
|
||||
foreach (var t in types)
|
||||
{
|
||||
var attributes = t.GetCustomAttributes(typeof(BindAttribute), true);
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
if (attribute is BindAttribute bindAttribute && bindAttribute.BindObject is BundleMode mode)
|
||||
{
|
||||
_gatherTypes[mode] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void FindTasks()
|
||||
{
|
||||
var types = EditUtil.FindAllSubclass<BuildTask>();
|
||||
foreach (var t in types)
|
||||
{
|
||||
var attributes = t.GetCustomAttributes(typeof(IdAttribute), true);
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
if (attribute is IdAttribute idAttribute && idAttribute.Id > 0)
|
||||
{
|
||||
_buildTaskTypes[idAttribute.Id] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Type GetGatherType(BundleMode mode)
|
||||
{
|
||||
return _gatherTypes.TryGetValue(mode, out var t) ? t : null;
|
||||
}
|
||||
|
||||
private static Type GetTaskType(int id)
|
||||
{
|
||||
return _buildTaskTypes.TryGetValue(id, out var t) ? t : null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
internal static BuildAsset[] GatherAsset(PackageConfig packageConfig, GroupConfig groupConfig)
|
||||
{
|
||||
var type = GetGatherType(groupConfig.BundleMode);
|
||||
if (type != null)
|
||||
{
|
||||
if (Activator.CreateInstance(type) is GatherBase instance)
|
||||
{
|
||||
return instance.Run(packageConfig, groupConfig);
|
||||
}
|
||||
}
|
||||
|
||||
return Array.Empty<BuildAsset>();
|
||||
}
|
||||
|
||||
public static void Gather()
|
||||
{
|
||||
RunBuildTasks(TaskId.Gather);
|
||||
}
|
||||
|
||||
|
||||
public static void Build()
|
||||
{
|
||||
RunBuildTasks(
|
||||
TaskId.Gather,
|
||||
TaskId.BuildBundle,
|
||||
TaskId.GenPackageData,
|
||||
TaskId.GenVersionData,
|
||||
TaskId.CopyVersionBundle,
|
||||
TaskId.CopyToStreamingAssets
|
||||
);
|
||||
|
||||
// TaskId.GenVersionData,
|
||||
// TaskId.CopyVersionBundle,
|
||||
// TaskId.CopyToStreamingAssets
|
||||
}
|
||||
|
||||
public static void RunBuildTasks(params int[] ids)
|
||||
{
|
||||
if (ids.Length < 1)
|
||||
{
|
||||
throw new Exception("not set task id!");
|
||||
}
|
||||
|
||||
List<Type> types = ids.Select(GetTaskType).Where(type => type != null).ToList();
|
||||
if (types.Count < 1)
|
||||
{
|
||||
throw new Exception("task id error!");
|
||||
}
|
||||
|
||||
List<BuildTask> buildTasks = new List<BuildTask>();
|
||||
foreach (var type in types)
|
||||
{
|
||||
if (Activator.CreateInstance(type) is BuildTask task)
|
||||
{
|
||||
buildTasks.Add(task);
|
||||
}
|
||||
}
|
||||
|
||||
BuildContext buildContext = new BuildContext();
|
||||
foreach (var task in buildTasks)
|
||||
{
|
||||
var sw = new Stopwatch();
|
||||
sw.Start();
|
||||
task.Run(buildContext);
|
||||
sw.Stop();
|
||||
Debug.Log($"Run {task.GetType().Name} time={sw.ElapsedMilliseconds / 1000f}s");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/NBC/Asset/Editor/Builder/Builder.cs.meta
Normal file
3
Assets/Scripts/NBC/Asset/Editor/Builder/Builder.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8faa489ad1174a76933f9bc929874b8d
|
||||
timeCreated: 1673928193
|
||||
8
Assets/Scripts/NBC/Asset/Editor/Builder/Gathers.meta
Normal file
8
Assets/Scripts/NBC/Asset/Editor/Builder/Gathers.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5c130f0a9594227a41f366c4fb51c84
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
161
Assets/Scripts/NBC/Asset/Editor/Builder/Gathers/GatherBase.cs
Normal file
161
Assets/Scripts/NBC/Asset/Editor/Builder/Gathers/GatherBase.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset.Editor
|
||||
{
|
||||
public abstract class GatherBase
|
||||
{
|
||||
protected PackageConfig PackageConfig;
|
||||
protected GroupConfig GroupConfig;
|
||||
|
||||
public BuildAsset[] Run(PackageConfig packageConfig, GroupConfig groupConfig)
|
||||
{
|
||||
PackageConfig = packageConfig;
|
||||
GroupConfig = groupConfig;
|
||||
return Execute();
|
||||
}
|
||||
|
||||
protected abstract BuildAsset[] Execute();
|
||||
|
||||
protected string GetBundleName(BuildAsset asset, string bundleName = "")
|
||||
{
|
||||
if (string.IsNullOrEmpty(bundleName))
|
||||
{
|
||||
bundleName = asset.Path;
|
||||
}
|
||||
|
||||
var settings = BuildSettings.Instance;
|
||||
bundleName = bundleName.Replace("\\", "/").Replace("/", "_").Replace(".", "_").ToLower();
|
||||
if (settings.ShaderBuildTogether && settings.ShaderExtensions.Exists(asset.Path.EndsWith))
|
||||
{
|
||||
bundleName = "shaders";
|
||||
}
|
||||
|
||||
return $"{PackageConfig.Name.ToLower()}/{bundleName}{BuildSettings.Instance.BundlesExtension}";
|
||||
}
|
||||
|
||||
#region GetAssets
|
||||
|
||||
protected List<BuildAsset> GetAssets()
|
||||
{
|
||||
List<BuildAsset> assets = new List<BuildAsset>();
|
||||
|
||||
var list = GroupConfig.Collectors;
|
||||
foreach (var obj in list)
|
||||
{
|
||||
var arr = GetAssets(obj, GroupConfig.Filter);
|
||||
if (arr != null && arr.Count > 0)
|
||||
{
|
||||
assets.AddRange(arr);
|
||||
}
|
||||
}
|
||||
|
||||
return assets;
|
||||
}
|
||||
|
||||
protected List<BuildAsset> GetAssets(Object obj, string filter = "")
|
||||
{
|
||||
List<BuildAsset> retList = new List<BuildAsset>();
|
||||
var path = AssetDatabase.GetAssetPath(obj);
|
||||
if (Directory.Exists(path))
|
||||
{
|
||||
var assetGuids = AssetDatabase.FindAssets(filter, new[]
|
||||
{
|
||||
path
|
||||
});
|
||||
foreach (var guid in assetGuids)
|
||||
{
|
||||
var childPath = AssetDatabase.GUIDToAssetPath(guid);
|
||||
|
||||
if (string.IsNullOrEmpty(childPath) || Directory.Exists(childPath)) continue;
|
||||
retList.Add(PathToBuildAsset(childPath, obj));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//如果是对象则不做过滤。直接加入列表
|
||||
retList.Add(ObjectToBuildAsset(obj));
|
||||
}
|
||||
|
||||
return retList;
|
||||
}
|
||||
|
||||
|
||||
protected BuildAsset ObjectToBuildAsset(Object collector)
|
||||
{
|
||||
var path = AssetDatabase.GetAssetPath(collector);
|
||||
return PathToBuildAsset(path, collector);
|
||||
}
|
||||
|
||||
protected BuildAsset PathToBuildAsset(string path, Object collector)
|
||||
{
|
||||
var type = AssetDatabase.GetMainAssetTypeAtPath(path);
|
||||
var ret = new BuildAsset
|
||||
{
|
||||
Path = path,
|
||||
Address = ToAddressPath(path, collector),
|
||||
Type = type == null ? "Missing" : type.Name,
|
||||
Tags = GroupConfig.Tags,
|
||||
Group = GroupConfig,
|
||||
// Dependencies = GetDependencies(path)
|
||||
};
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected string[] GetDependencies(string path)
|
||||
{
|
||||
var ret = new HashSet<string>();
|
||||
ret.UnionWith(AssetDatabase.GetDependencies(path));
|
||||
ret.RemoveWhere(s => s.EndsWith(".unity"));
|
||||
ret.Remove(path);
|
||||
return ret.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换文件地址为可寻址地址
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="collector"></param>
|
||||
/// <returns></returns>
|
||||
protected string ToAddressPath(string filePath, Object collector)
|
||||
{
|
||||
var mode = GroupConfig.AddressMode;
|
||||
var collectorName = collector != null ? collector.name : string.Empty;
|
||||
var path = AssetDatabase.GetAssetPath(collector);
|
||||
if (path == filePath)
|
||||
{
|
||||
collectorName = string.Empty;
|
||||
}
|
||||
|
||||
var fileName = Path.GetFileNameWithoutExtension(filePath);
|
||||
var groupName = GroupConfig.Name;
|
||||
var packageName = PackageConfig.Name;
|
||||
switch (mode)
|
||||
{
|
||||
case AddressMode.None:
|
||||
return filePath;
|
||||
case AddressMode.FileName:
|
||||
return fileName;
|
||||
case AddressMode.GroupAndFileName:
|
||||
return $"{groupName}/{fileName}";
|
||||
case AddressMode.PackAndGroupAndFileName:
|
||||
return $"{packageName}/{groupName}/{fileName}";
|
||||
case AddressMode.PackAndFileName:
|
||||
return $"{packageName}/{fileName}";
|
||||
case AddressMode.CollectorAndFileName:
|
||||
return string.IsNullOrEmpty(collectorName) ? fileName : $"{collectorName}/{fileName}";
|
||||
case AddressMode.PackAndCollectorAndFileName:
|
||||
return string.IsNullOrEmpty(collectorName)
|
||||
? $"{packageName}/{fileName}"
|
||||
: $"{packageName}/{collectorName}/{fileName}";
|
||||
}
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a9d7262122d4b439f86e48c9a47ea49
|
||||
timeCreated: 1673942495
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC.Asset.Editor
|
||||
{
|
||||
[Bind(BundleMode.File)]
|
||||
public class GatherFile : GatherBase
|
||||
{
|
||||
protected override BuildAsset[] Execute()
|
||||
{
|
||||
List<BuildAsset> assets = GetAssets();
|
||||
foreach (var asset in assets)
|
||||
{
|
||||
asset.Bundle = GetBundleName(asset);
|
||||
}
|
||||
|
||||
return assets.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c9c33f337ab4656a3dc89e495cbd873
|
||||
timeCreated: 1673943049
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
|
||||
namespace NBC.Asset.Editor
|
||||
{
|
||||
[Bind(BundleMode.Folder)]
|
||||
public class GatherFolder : GatherBase
|
||||
{
|
||||
protected override BuildAsset[] Execute()
|
||||
{
|
||||
List<BuildAsset> assets = GetAssets();
|
||||
var singleAssetPaths = GetSingleAssetPaths();
|
||||
Dictionary<string, List<BuildAsset>> dictionary = new Dictionary<string, List<BuildAsset>>();
|
||||
foreach (var asset in assets)
|
||||
{
|
||||
var dirPath = Path.GetDirectoryName(asset.Path);
|
||||
if (singleAssetPaths.Contains(asset.Path)) dirPath = asset.Path;
|
||||
|
||||
if (string.IsNullOrEmpty(dirPath)) continue;
|
||||
if (!dictionary.TryGetValue(dirPath, out var list))
|
||||
{
|
||||
list = new List<BuildAsset>();
|
||||
dictionary[dirPath] = list;
|
||||
}
|
||||
|
||||
if (!list.Contains(asset)) list.Add(asset);
|
||||
}
|
||||
|
||||
List<BuildAsset> ret = new List<BuildAsset>();
|
||||
|
||||
foreach (var key in dictionary.Keys)
|
||||
{
|
||||
var value = dictionary[key];
|
||||
foreach (var asset in value)
|
||||
{
|
||||
asset.Bundle = GetBundleName(asset, key);
|
||||
ret.Add(asset);
|
||||
}
|
||||
}
|
||||
|
||||
return ret.ToArray();
|
||||
}
|
||||
|
||||
private List<string> GetSingleAssetPaths()
|
||||
{
|
||||
List<string> list = new List<string>();
|
||||
foreach (var obj in GroupConfig.Collectors)
|
||||
{
|
||||
var path = AssetDatabase.GetAssetPath(obj);
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
list.Add(path);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7364f38a2920403597a8453c74af4a97
|
||||
timeCreated: 1673942970
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset.Editor
|
||||
{
|
||||
[Bind(BundleMode.FolderParent)]
|
||||
public class GatherFolderParent : GatherBase
|
||||
{
|
||||
protected override BuildAsset[] Execute()
|
||||
{
|
||||
List<BuildAsset> ret = new List<BuildAsset>();
|
||||
var list = GroupConfig.Collectors;
|
||||
foreach (var obj in list)
|
||||
{
|
||||
var assets = GetAssets(obj, GroupConfig.Filter);
|
||||
if (assets != null && assets.Count > 0)
|
||||
{
|
||||
var bundleName = AssetDatabase.GetAssetPath(obj);
|
||||
foreach (var asset in assets)
|
||||
{
|
||||
asset.Bundle = GetBundleName(asset, bundleName);
|
||||
ret.Add(asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d621c6e03b294f3e845112fdd120fd89
|
||||
timeCreated: 1673943009
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NBC.Asset.Editor
|
||||
{
|
||||
[Bind(BundleMode.Single)]
|
||||
public class GatherSingle : GatherBase
|
||||
{
|
||||
protected override BuildAsset[] Execute()
|
||||
{
|
||||
List<BuildAsset> assets = GetAssets();
|
||||
foreach (var asset in assets)
|
||||
{
|
||||
asset.Bundle = GetBundleName(asset, GroupConfig.Name);
|
||||
}
|
||||
|
||||
return assets.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 74736578b5504c3e849ee5d1e0b7ced3
|
||||
timeCreated: 1673942520
|
||||
@@ -0,0 +1,57 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace NBC.Asset.Editor
|
||||
{
|
||||
[Bind(BundleMode.WithoutSub)]
|
||||
public class GatherWithoutSub : GatherBase
|
||||
{
|
||||
protected override BuildAsset[] Execute()
|
||||
{
|
||||
List<BuildAsset> ret = new List<BuildAsset>();
|
||||
var list = GroupConfig.Collectors;
|
||||
foreach (var obj in list)
|
||||
{
|
||||
var assets = GetAssets(obj, GroupConfig.Filter);
|
||||
if (assets != null && assets.Count > 0)
|
||||
{
|
||||
assets = WithoutSub(obj, assets);
|
||||
var bundleName = AssetDatabase.GetAssetPath(obj);
|
||||
foreach (var asset in assets)
|
||||
{
|
||||
asset.Bundle = GetBundleName(asset, bundleName);
|
||||
ret.Add(asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret.ToArray();
|
||||
}
|
||||
|
||||
private List<BuildAsset> WithoutSub(Object obj, List<BuildAsset> assets)
|
||||
{
|
||||
List<BuildAsset> ret = new List<BuildAsset>();
|
||||
var path = AssetDatabase.GetAssetPath(obj);
|
||||
if (Directory.Exists(path))
|
||||
{
|
||||
foreach (var asset in assets)
|
||||
{
|
||||
var childPath = asset.Path;
|
||||
var dirName = Path.GetDirectoryName(childPath)?.Replace("\\", "/");
|
||||
if (dirName == path)
|
||||
{
|
||||
ret.Add(asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.AddRange(assets);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2a28e18aaf4d45b3903505257ab9542c
|
||||
timeCreated: 1675606827
|
||||
3
Assets/Scripts/NBC/Asset/Editor/Builder/Tasks.meta
Normal file
3
Assets/Scripts/NBC/Asset/Editor/Builder/Tasks.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b102828c7294fe89e65179188906813
|
||||
timeCreated: 1675823185
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2ed116b0b33144cb94cb189f4234f0ca
|
||||
timeCreated: 1675823335
|
||||
11
Assets/Scripts/NBC/Asset/Editor/Builder/Tasks/BuildTask.cs
Normal file
11
Assets/Scripts/NBC/Asset/Editor/Builder/Tasks/BuildTask.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace NBC.Asset.Editor
|
||||
{
|
||||
public abstract class BuildTask
|
||||
{
|
||||
public abstract void Run(BuildContext context);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4a28b318de2d45ef886f30fc4d1ffac9
|
||||
timeCreated: 1675822432
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2af47d5da6e844ce97d51af3a3ded489
|
||||
timeCreated: 1675845425
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f706081edbe481ea7e727651d531e0a
|
||||
timeCreated: 1675823749
|
||||
66
Assets/Scripts/NBC/Asset/Editor/Builder/Tasks/GatherTask.cs
Normal file
66
Assets/Scripts/NBC/Asset/Editor/Builder/Tasks/GatherTask.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 79738d555c83467c954f240b9f3b0d00
|
||||
timeCreated: 1675823940
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9dca3963bf3d46f8a3734326ef1cfd1a
|
||||
timeCreated: 1680321963
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b4670cdc7f941558a69023329f42de2
|
||||
timeCreated: 1675837362
|
||||
Reference in New Issue
Block a user