123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- 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<string> _referenceBundleNames = new HashSet<string>();
- /// <summary>
- /// 收集器类型
- /// </summary>
- public ECollectorType CollectorType { private set; get; }
- /// <summary>
- /// 资源包完整名称
- /// </summary>
- public string BundleName { private set; get; }
- /// <summary>
- /// 可寻址地址
- /// </summary>
- public string Address { private set; get; }
- /// <summary>
- /// 资源路径
- /// </summary>
- public string AssetPath { private set; get; }
- /// <summary>
- /// 资源GUID
- /// </summary>
- public string AssetGUID { private set; get; }
- /// <summary>
- /// 是否为原生资源
- /// </summary>
- public bool IsRawAsset { private set; get; }
- /// <summary>
- /// 是否为着色器资源
- /// </summary>
- public bool IsShaderAsset { private set; get; }
- /// <summary>
- /// 资源的分类标签
- /// </summary>
- public readonly List<string> AssetTags = new List<string>();
- /// <summary>
- /// 资源包的分类标签
- /// </summary>
- public readonly List<string> BundleTags = new List<string>();
- /// <summary>
- /// 依赖的所有资源
- /// 注意:包括零依赖资源和冗余资源(资源包名无效)
- /// </summary>
- public List<BuildAssetInfo> 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;
- }
- /// <summary>
- /// 设置所有依赖的资源
- /// </summary>
- public void SetAllDependAssetInfos(List<BuildAssetInfo> dependAssetInfos)
- {
- if (AllDependAssetInfos != null)
- throw new System.Exception("Should never get here !");
- AllDependAssetInfos = dependAssetInfos;
- }
- /// <summary>
- /// 添加资源的分类标签
- /// 说明:原始定义的资源分类标签
- /// </summary>
- public void AddAssetTags(List<string> 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);
- }
- }
- }
- /// <summary>
- /// 添加资源包的分类标签
- /// 说明:传染算法统计到的分类标签
- /// </summary>
- public void AddBundleTags(List<string> tags)
- {
- foreach (var tag in tags)
- {
- if (BundleTags.Contains(tag) == false)
- {
- BundleTags.Add(tag);
- }
- }
- }
- /// <summary>
- /// 资源包名是否存在
- /// </summary>
- public bool HasBundleName()
- {
- if (string.IsNullOrEmpty(BundleName))
- return false;
- else
- return true;
- }
- /// <summary>
- /// 添加关联的资源包名称
- /// </summary>
- public void AddReferenceBundleName(string bundleName)
- {
- if (string.IsNullOrEmpty(bundleName))
- throw new Exception("Should never get here !");
- if (_referenceBundleNames.Contains(bundleName) == false)
- _referenceBundleNames.Add(bundleName);
- }
- /// <summary>
- /// 计算共享资源包的完整包名
- /// </summary>
- 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;
- }
- }
- }
- /// <summary>
- /// 判断是否为冗余资源
- /// </summary>
- public bool IsRedundancyAsset()
- {
- if (HasBundleName())
- return false;
- return _referenceBundleNames.Count > 1;
- }
- /// <summary>
- /// 获取关联资源包的数量
- /// </summary>
- public int GetReferenceBundleCount()
- {
- return _referenceBundleNames.Count;
- }
- }
- }
|