private static void OnClear(AssetRefHelper helper)
 {
     foreach (var assetRef in helper.refList.ToArray())
     {
         OnUnRef(helper, assetRef);
     }
 }
 private static void OnUnRef(AssetRefHelper helper, AssetRef assetRef)
 {
     if (assetRefDict.TryGetValue(assetRef, out var set))
     {
         set.Remove(helper.gameObject);
     }
 }
 private static void OnRef(AssetRefHelper helper, AssetRef assetRef)
 {
     if (assetRefDict.TryGetValue(assetRef, out var set))
     {
         set.Add(helper.gameObject);
     }
     else
     {
         set = new HashSet <GameObject>();
         set.Add(helper.gameObject);
         assetRefDict[assetRef] = set;
     }
 }
Exemple #4
0
        //抓取信息,一帧调用一次即可
        public AssetFrameInfo CaptureFrame()
        {
            //所有的资源
            Dictionary <UnityEngine.Object, AssetInfo> assetMap = new Dictionary <UnityEngine.Object, AssetInfo>();
            //依赖关系
            Dictionary <UnityEngine.Object, AssetRefList> depMap = new Dictionary <UnityEngine.Object, AssetRefList>();

            float cacheHitPercent = 0.0f;

            foreach (var abLoader in abLoaders)
            {
                var lruInfo = new HashSet <string>(abLoader.GetLruInfo());
                cacheHitPercent += abLoader.cacheHitPercent;
                foreach (var abInfo in abLoader.GetLoadedAbs())
                {
                    if (abInfo == null)
                    {
                        continue;
                    }
                    AssetInfo info = new AssetInfo();
                    info.asset    = abInfo.ab;
                    info.assetRef = GetAssetRef(abInfo.ab);
                    if (info.assetRef != null)
                    {
                        info.isCacheWeak    = weakRefs.Contains(info.assetRef);
                        info.isCacheStrong  = strongRefs.Contains(info.assetRef);
                        info.refGameObjects = AssetRefHelper.FindGameObjects(info.assetRef);
                    }
                    info.isInLru         = lruInfo.Contains(abInfo.abPath);
                    assetMap[info.asset] = info;
                    depMap[info.asset]   = abInfo.deps;
                }
            }

            //整理依赖关系,构造双向查询
            foreach (var pair in assetMap)
            {
                var info  = pair.Value;
                var asset = pair.Key;

                if (depMap.TryGetValue(asset, out var deps))
                {
                    info.refs = new List <AssetInfo>();
                    foreach (var dep in deps.ToArray())
                    {
                        var depInfo = assetMap[dep.asset]; //必须可以取到
                        info.refs.Add(depInfo);
                        if (depInfo.refedByOthers == null)
                        {
                            depInfo.refedByOthers = new List <AssetInfo>();
                        }
                        depInfo.refedByOthers.Add(info);
                    }
                }
            }

            AssetFrameInfo frameInfo = new AssetFrameInfo();

            frameInfo.assetInfos      = assetMap.Values.ToArray();
            frameInfo.cacheHitPercent = cacheHitPercent / abLoaders.Count;
            return(frameInfo);
        }