using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace YooAsset.Editor { public class BuildAssetInfo { private bool _isAddAssetTags = false; private readonly HashSet _referenceBundleNames = new HashSet(); /// /// 收集器类型 /// public ECollectorType CollectorType { private set; get; } /// /// 资源包完整名称 /// public string BundleName { private set; get; } /// /// 可寻址地址 /// public string Address { private set; get; } /// /// 资源路径 /// public string AssetPath { private set; get; } /// /// 资源GUID /// public string AssetGUID { private set; get; } /// /// 是否为原生资源 /// public bool IsRawAsset { private set; get; } /// /// 是否为着色器资源 /// public bool IsShaderAsset { private set; get; } /// /// 资源的分类标签 /// public readonly List AssetTags = new List(); /// /// 资源包的分类标签 /// public readonly List BundleTags = new List(); /// /// 依赖的所有资源 /// 注意:包括零依赖资源和冗余资源(资源包名无效) /// public List AllDependAssetInfos { private set; get; } public BuildAssetInfo(ECollectorType collectorType, string bundleName, string address, string assetPath, bool isRawAsset) { CollectorType = collectorType; BundleName = bundleName; Address = address; AssetPath = assetPath; IsRawAsset = isRawAsset; AssetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(assetPath); System.Type assetType = UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(assetPath); if (assetType == typeof(UnityEngine.Shader) || assetType == typeof(UnityEngine.ShaderVariantCollection)) IsShaderAsset = true; else IsShaderAsset = false; } public BuildAssetInfo(string assetPath) { CollectorType = ECollectorType.None; Address = string.Empty; AssetPath = assetPath; IsRawAsset = false; AssetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(assetPath); System.Type assetType = UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(assetPath); if (assetType == typeof(UnityEngine.Shader) || assetType == typeof(UnityEngine.ShaderVariantCollection)) IsShaderAsset = true; else IsShaderAsset = false; } /// /// 设置所有依赖的资源 /// public void SetAllDependAssetInfos(List dependAssetInfos) { if (AllDependAssetInfos != null) throw new System.Exception("Should never get here !"); AllDependAssetInfos = dependAssetInfos; } /// /// 添加资源的分类标签 /// 说明:原始定义的资源分类标签 /// public void AddAssetTags(List tags) { if (_isAddAssetTags) throw new Exception("Should never get here !"); _isAddAssetTags = true; foreach (var tag in tags) { if (AssetTags.Contains(tag) == false) { AssetTags.Add(tag); } } } /// /// 添加资源包的分类标签 /// 说明:传染算法统计到的分类标签 /// public void AddBundleTags(List tags) { foreach (var tag in tags) { if (BundleTags.Contains(tag) == false) { BundleTags.Add(tag); } } } /// /// 资源包名是否存在 /// public bool HasBundleName() { if (string.IsNullOrEmpty(BundleName)) return false; else return true; } /// /// 添加关联的资源包名称 /// public void AddReferenceBundleName(string bundleName) { if (string.IsNullOrEmpty(bundleName)) throw new Exception("Should never get here !"); if (_referenceBundleNames.Contains(bundleName) == false) _referenceBundleNames.Add(bundleName); } /// /// 计算共享资源包的完整包名 /// public void CalculateShareBundleName(ISharedPackRule sharedPackRule, bool uniqueBundleName, string packageName, string shadersBundleName) { if (CollectorType != ECollectorType.None) return; if (IsRawAsset) throw new Exception("Should never get here !"); if (IsShaderAsset) { BundleName = shadersBundleName; } else { if (_referenceBundleNames.Count > 1) { PackRuleResult packRuleResult = sharedPackRule.GetPackRuleResult(AssetPath); BundleName = packRuleResult.GetShareBundleName(packageName, uniqueBundleName); } else { // 注意:被引用次数小于1的资源不需要设置资源包名称 BundleName = string.Empty; } } } /// /// 判断是否为冗余资源 /// public bool IsRedundancyAsset() { if (HasBundleName()) return false; return _referenceBundleNames.Count > 1; } /// /// 获取关联资源包的数量 /// public int GetReferenceBundleCount() { return _referenceBundleNames.Count; } } }