コード例 #1
0
        public void CheckForContentUpdateAndDownload()
        {
            Addressables.InitializeAsync().Completed += objects =>
            {
                Addressables.ClearDependencyCacheAsync(LabelsToCheck);

                Addressables.CheckForCatalogUpdates().Completed += DoWhenCheckCompleted;
            };
        }
コード例 #2
0
    public void DeepClean()
    {
        foreach (var asset in assetsReferences)
        {
            if (_asyncOperationHandles.ContainsKey(asset))
            {
                Addressables.Release(_asyncOperationHandles[asset]);
                Addressables.ClearDependencyCacheAsync(asset);
            }
        }

        downloadSize.text = "0.0";
        CleanUp();
    }
コード例 #3
0
        /// <summary>
        /// 下载更新资源
        /// </summary>
        /// <returns></returns>
        internal IEnumerator StartDownLoadAB(Action startOnFinish, List <object> allDownLoad = null)
        {
            StartDownload = true;
            if (allDownLoad == null)
            {
                allDownLoad = m_DownLoadList;
            }
            for (int i = 0; i < allDownLoad.Count; ++i)
            {
                //获取这个m_DownLoadList[i]资源所在的AB包大小,可能多个m_DownLoadList[i]在同一个资源包里面
                //当这个m_DownLoadList[i]资源的AB包下载完成后,这个AB包里其他的m_DownLoadList[i]资源获取
                //GetDownloadSizeAsync的大小会是0,用这个0来判断是否开始下载m_DownLoadList[i]资源的AB包
                AsyncOperationHandle sizeHandle = Addressables.GetDownloadSizeAsync(m_DownLoadList[i]);
                yield return(sizeHandle);

                long totalDownloadSize = (long)sizeHandle.Result;
                if (totalDownloadSize > 0)
                {
                    Debug.Log($"资源:{m_DownLoadList[i]}==所在的AB包要更新的大小{totalDownloadSize}");

                    //先卸载这个资源以前的包
                    //一定要卸载,不然会越更新手机中的资源越多
                    Addressables.ClearDependencyCacheAsync(m_DownLoadList[i]);

                    //再重新下载这个资源包
                    var downloadHandle = Addressables.DownloadDependenciesAsync(m_DownLoadList[i]);
                    while (!downloadHandle.IsDone)
                    {
                        nowLoadSize = downloadHandle.PercentComplete * totalDownloadSize;    //正在下载资源的大小
                        yield return(new WaitForEndOfFrame());
                    }
                    Addressables.Release(downloadHandle);
                    m_AlreadyDownDic.Add(m_DownLoadList[i], totalDownloadSize);

                    nowLoadSize      = 0;
                    alreadyLoadSize += totalDownloadSize;       //已经下载资源的大小

                    Debug.Log($"资源:{m_DownLoadList[i].ToString()}所在的AB包下载完成:{(int)totalDownloadSize}");
                }
                else
                {
                    m_AlreadyDownDic.Add(m_DownLoadList[i], totalDownloadSize);
                }
                Addressables.Release(sizeHandle);
            }

            //下载失败后要重新下载的列表
            List <object> downLoadAgainList = new List <object>();

            for (int i = 0; i < m_DownLoadList.Count; ++i)
            {
                if (!m_AlreadyDownDic.ContainsKey(m_DownLoadList[i]))
                {
                    downLoadAgainList.Add(m_DownLoadList[i]);
                }
            }

            //全部文件都是正确的
            if (downLoadAgainList.Count <= 0)
            {
                if (startOnFinish != null)
                {
                    StartDownload = false;
                    startOnFinish();
                }
                if (LoadOver != null)
                {
                    LoadOver();
                }
            }
            else
            {
                if (m_TryDownCount >= DOWNLOADCOUNT)
                {
                    string allName = "";
                    StartDownload = false;
                    foreach (var item in downLoadAgainList)
                    {
                        allName += item + ";";
                    }
                    Debug.LogError("资源重复下载多次次都失败,请检查资源" + allName);
                    ItemError?.Invoke(allName);
                }
                else
                {
                    m_TryDownCount++;
                    //自动重新下载校验失败的文件
                    m_Startmono.StartCoroutine(StartDownLoadAB(startOnFinish, downLoadAgainList));
                }
            }
        }
コード例 #4
0
    /*************************************************************************************************************
     *
     * Example 5: Clear all downloaded assets from the local cache
     *
     * This method simply fires the ClearDependencyCacheAsync() method, taking in a list of IResourceLocations
     * that represents the assets to clear. This is mostly for my own testing purposes, though it would allow a
     * user to clear their cache if they are having issues with downloaded assets or need to free up space on
     * their system.
     *
     * Note: This likely should be handled in a coroutine for better asynchronous handling and handling of any
     * errors or messages that are returned, but at this point all I needed was to be able to clear my cache.
     *
     ************************************************************************************************************/

    public void ClearCache()
    {
        Addressables.ClearDependencyCacheAsync(_allLocations);
    }
コード例 #5
0
    private void OnGUI()
    {
        if (GUILayout.Button("CheckCatalogUpdate"))
        {
            Debug.Log("CheckCatalogUpdate");

            var opChecklist = Addressables.CheckForCatalogUpdates();
            opChecklist.Completed += handle =>
            {
                var checkList = handle.Result;
                Debug.LogFormat("CheckForCatalogUpdates: {0}, Status: {1}", checkList.Count, handle.Status);
                for (int i = 0; i < checkList.Count; i++)
                {
                    Debug.LogFormat("[{0}] Check Catalog: {1}", i, checkList[i]);
                }

                if (checkList.Count > 0)
                {
                    var opUpdate = Addressables.UpdateCatalogs(checkList);
                    opUpdate.Completed += operationHandle =>
                    {
                        Debug.LogFormat("UpdateCatalogs Status: {0}", operationHandle.Status);
                        if (operationHandle.Status == AsyncOperationStatus.Succeeded)
                        {
                            var locations = operationHandle.Result;
                            Debug.LogFormat("UpdateCatalogs: {0}", locations?.Count ?? 0);
                            for (int i = 0; i < checkList.Count; i++)
                            {
                                Debug.LogFormat("[{0}] Resource Locator: {1}", i, checkList[i]);
                            }
                        }
                    };
                }
            };
        }

        if (GUILayout.Button("Download"))
        {
            var opInit = Addressables.InitializeAsync();
            opInit.Completed += opInitHandle =>
            {
                var resLocator = opInitHandle.Result;
                Debug.Log("Download:" + resLocator.Keys);
                var opGetDownloadSize = Addressables.GetDownloadSizeAsync(resLocator.Keys);

                opGetDownloadSize.Completed += handle =>
                {
                    Debug.LogFormat("GetDownloadSizeAsync Status: {0} Size: {1} bytes", handle.GetDownloadStatus(), handle.Result);

                    var allLocations = new List <IResourceLocation>();
                    foreach (var key in resLocator.Keys)
                    {
                        IList <IResourceLocation> locations;
                        if (resLocator.Locate(key, typeof(object), out locations))
                        {
                            foreach (var loc in locations)
                            {
                                if (loc.HasDependencies)
                                {
                                    allLocations.AddRange(loc.Dependencies);
                                }
                            }
                        }
                    }


                    var allPrimaryKeys = allLocations.Distinct().ToList();
                    for (int i = 0; i < allPrimaryKeys.Count; i++)
                    {
                        Debug.LogFormat("[{0}]DownloadDependency PrimaryKey: {1}", i, allPrimaryKeys[i]);
                    }

                    var opDownload = Addressables.DownloadDependenciesAsync(allPrimaryKeys);

                    opDownload.Completed += operationHandle =>
                    {
                        Debug.LogFormat("DownloadDependenciesAsync: {0}", operationHandle.Result);
                    };
                };
            };
        }

        if (GUILayout.Button("ClearDependencyCache"))
        {
            var opInit = Addressables.InitializeAsync();
            opInit.Completed += opInitHandle =>
            {
                var resLocator = opInitHandle.Result;

                var allLocations = new List <IResourceLocation>();
                foreach (var key in resLocator.Keys)
                {
                    IList <IResourceLocation> locations;
                    if (resLocator.Locate(key, typeof(object), out locations))
                    {
                        foreach (var loc in locations)
                        {
                            if (loc.HasDependencies)
                            {
                                allLocations.AddRange(loc.Dependencies);
                            }
                        }
                    }
                }

                Debug.LogFormat("ClearDependencyCacheAsync: {0}", allLocations.Count);

                var opClear = Addressables.ClearDependencyCacheAsync(allLocations, false);
                opClear.Completed += handle =>
                {
                    Debug.LogFormat("ClearDependencyCacheAsync Completed: {0}", handle.Result);
                    Addressables.Release(handle);
                };
            };
        }

        if (GUILayout.Button("Caching.ClearCache"))
        {
            var seconds = 1;
            var result  = Caching.ClearCache(seconds);
            Debug.LogFormat("Caching.ClearCache: {0}", result);
        }

        if (GUILayout.Button("Load"))
        {
            Debug.Log("Load");

            var handle = Addressables.LoadAssetAsync <GameObject>("Assets/CanNotChangePost/A_1.prefab");
            handle.Completed += onLoadDone;

            handles.Add(handle);
        }
        if (GUILayout.Button("UnLoad"))
        {
            if (handles.Count > 0)
            {
                var handle = handles[handles.Count - 1];

                if (handle.IsValid())
                {
                    Debug.Log("UnLoad OK");

                    Addressables.Release(handle);
                }
                else
                {
                    Debug.Log("UnLoad Fail");
                }
            }


            if (instances.Count > 0)
            {
                Destroy(instances[instances.Count - 1]);
                instances.RemoveAt(instances.Count - 1);
            }
        }
    }
コード例 #6
0
 /// <summary>
 /// Clear the cached AssetBundles for a list of Addressable keys.  Operation may be performed async if Addressables
 /// is initializing or updating.
 /// </summary>
 /// <param name="keys">The keys to clear the cache for.</param>
 public static void ClearDependencyCacheAsync(IList <object> keys)
 {
     Addressables.ClearDependencyCacheAsync(keys);
 }
コード例 #7
0
 /// <summary>
 /// Clear the cached AssetBundles for a list of Addressable locations.  Operation may be performed async if Addressables
 /// is initializing or updating.
 /// </summary>
 /// <param name="locations">The locations to clear the cache for.</param>
 public static void ClearDependencyCacheAsync(IList <IResourceLocation> locations)
 {
     Addressables.ClearDependencyCacheAsync(locations);
 }
コード例 #8
0
 /// <summary>
 /// Clear the cached AssetBundles for a given key.  Operation may be performed async if Addressables
 /// is initializing or updating.
 /// </summary>
 /// <param name="key">The key to clear the cache for.</param>
 public static void ClearDependencyCacheAsync(object key)
 {
     Addressables.ClearDependencyCacheAsync(key);
 }