示例#1
0
    void GUIBundleCaches()
    {
        int clearCounter = 0;

        GUILayout.BeginVertical("box");
        {
            GUILayout.BeginHorizontal("box");
            {
                if (GUILayout.Button("Save", GUILayout.Height(40)))
                {
                    BundleCaches.Instance.Save();
                }
                if (GUILayout.Button("Load", GUILayout.Height(40)))
                {
                    BundleCaches.Instance.Load();
                }
                if (GUILayout.Button("Add current", GUILayout.Height(40)))
                {
                    BundleCaches.Instance.Add(BundleManager.Instance.Bundles.Select(b => (b.bundleName, b.bundleHash)));
                }
                if (GUILayout.Button("Clear Old Bundle Caches", GUILayout.Height(40)))
                {
                    foreach (var bc in BundleCaches.Instance.Bundles)
                    {
                        if (!BundleManager.Instance.HasBundleWithHash(bc.name, bc.hash))
                        {
                            Debug.Log($"Removing {bc.name} : {bc.hash}");
                            Caching.ClearCachedVersion(bc.name, Hash128.Parse(bc.hash));
                            clearCounter++;
                        }
                    }
                }
            }
            GUILayout.EndHorizontal();

            foreach (var bundleCache in BundleCaches.Instance.Bundles)
            {
                GUILayout.BeginHorizontal("box");
                {
                    if (GUILayout.Button("X", GUILayout.Width(40), GUILayout.Height(40)))
                    {
                        clearCounter++;
                        Caching.ClearCachedVersion(bundleCache.name, Hash128.Parse(bundleCache.hash));
                    }
                    GUILayout.Button($"{bundleCache.name.Substring(0, 6)}");
                    GUILayout.Button($"{bundleCache.hash.Substring(0, 6)}");
                    GUILayout.Button($"{BundleManager.Instance.Bundles.Exists(b => b.bundleName == bundleCache.name && b.bundleHash == bundleCache.hash)}");
                }
                GUILayout.EndVertical();
            }
        }
        GUILayout.EndVertical();

        if (clearCounter > 0)
        {
            cacheSize = Caches.GetAllCachedSize();
            BundleManager.Instance.RefreshBundles();
        }
    }
    public static int ClearCachedVersion_s(IntPtr l)
    {
        int result;

        try
        {
            string assetBundleName;
            LuaObject.checkType(l, 1, out assetBundleName);
            Hash128 hash;
            LuaObject.checkValueType <Hash128>(l, 2, out hash);
            bool b = Caching.ClearCachedVersion(assetBundleName, hash);
            LuaObject.pushValue(l, true);
            LuaObject.pushValue(l, b);
            result = 2;
        }
        catch (Exception e)
        {
            result = LuaObject.error(l, e);
        }
        return(result);
    }
示例#3
0
    private IEnumerator LoadAssetBundleFromCache(Dictionary <string, GameResAssetBundle> bundleGroupMap, string bundleName, string manifestName, string hash, string[] urls, System.Action <bool> onLoadDone)
    {
        var realHash = Hash128.Parse(hash);

        bool success = false;

        foreach (string url in urls)
        {
            float checkTimeOutTime = Time.time;
            float progress         = 0;

            var webRequest = GameWebRequest.SendAssetBundleWebRequest(url, realHash, true);
            while (!webRequest.request.isDone)
            {
                if (!string.IsNullOrEmpty(webRequest.request.error))
                {
                    break;
                }
                if (progress != webRequest.request.downloadProgress)
                {
                    checkTimeOutTime = Time.time;
                }
                else if (Time.time - checkTimeOutTime >= 5)
                {
                    break;
                }
                yield return(null);
            }

            if (!string.IsNullOrEmpty(webRequest.request.error) || !webRequest.request.isDone)
            {
                FTDebug.LogWarning(string.Format("Failed To LoadAssetBundleFromCache Name[{0}] From[{1}] Error[{2}]", bundleName, url, webRequest.request.error));
                GameWebRequest.DestroyAssetBundleWebRequest(webRequest);
                Caching.ClearCachedVersion(bundleName, realHash);
                continue;
            }

            AssetBundle assetBundle = null;
            try
            {
                assetBundle = DownloadHandlerAssetBundle.GetContent(webRequest.request);
                if (assetBundle == null)
                {
                    FTDebug.LogWarning(string.Format("Failed To LoadAssetBundleFromCache Name[{0}] From[{1}] Error[AssetBundle is Null]", bundleName, url));
                    GameWebRequest.DestroyAssetBundleWebRequest(webRequest);
                    Caching.ClearCachedVersion(bundleName, realHash);
                    continue;
                }
            }
            catch (System.Exception e)
            {
                FTDebug.LogWarning(string.Format("Failed To LoadAssetBundleFromCache Name[{0}] From[{1}] Error[{2}]", bundleName, url, e.Message));
                GameWebRequest.DestroyAssetBundleWebRequest(webRequest);
                Caching.ClearCachedVersion(bundleName, realHash);
                continue;
            }

            if (bundleGroupMap.ContainsKey(bundleName) && bundleGroupMap[bundleName].hash != hash)
            {
                UnLoadAssetBundle(bundleName, manifestName);
            }

            if (!bundleGroupMap.ContainsKey(bundleName))
            {
                bundleGroupMap.Add(bundleName, new GameResAssetBundle()
                {
                    hash        = hash,
                    assetBundle = assetBundle,
                });
            }

            GameWebRequest.DestroyAssetBundleWebRequest(webRequest);
            success = true;
            FTDebug.Log(string.Format("LoadAssetBundleFromCache Name[{0}] From[{1}]", bundleName, url));

            // 清除除当前使用的
            Caching.ClearOtherCachedVersions(bundleName, realHash);

            break;
        }

        onLoadDone(success);
    }