首次提交
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user