Пример #1
0
        /// <summary>
        /// Resources 异步加载资源 (注意加载路径中不能带有扩展名否则加载失败)
        /// </summary>
        /// <param name="assetFullUri"></param>
        /// <param name="loadCallback"></param>
        /// <param name="procressCallback"></param>
        public static void ResourcesLoadAssetAsync(string assetFullUri, System.Action <ILoadAssetRecord> loadCallback, System.Action <string, float> procressCallback)
        {
            LoadResourcesAssetRecord resourcesAssetRecord = LoadResourcesAssetFromCache(assetFullUri);

            if (resourcesAssetRecord != null && resourcesAssetRecord.IsRecordEnable)
            {
                loadCallback?.Invoke(resourcesAssetRecord);
                return;
            }

            ResourceRequest request = Resources.LoadAsync(assetFullUri.GetPathWithOutExtension());

            AsyncManager.StartAsyncOperation(request, () =>
            {
                resourcesAssetRecord = LoadResourcesAssetRecord.GetLoadResourcesAssetRecord(assetFullUri, request.asset);
                RecordResourcesLoadAsset(assetFullUri, resourcesAssetRecord);
                loadCallback?.Invoke(resourcesAssetRecord);
            }, (procress) => { procressCallback?.Invoke(assetFullUri, procress); });
        }
Пример #2
0
        /// <summary>
        ///  异步加载 AssetBundleRecordInfor 资源
        /// </summary>
        /// <param name="assetBundleNameUri">AssetBundle 文件名</param>
        /// <param name="assetBundleDirectoryUri">AssetBundle 相对目录(可能是null)</param>
        /// <param name="completeCallback"></param>
        private static void LoadAssetBundleRecordAsync(string assetBundleNameUri, string assetBundleDirectoryUri, Action <AssetBundleRecordInfor> completeCallback)
        {
            if (string.IsNullOrEmpty(assetBundleNameUri))
            {
                Debug.LogError("LoadAssetBundleRecordAsync Fail,Parameter is null");
                completeCallback?.Invoke(null);
                return;
            }

            //缓存中取
            AssetBundleRecordInfor assetBundleRecordInfor = LoadAssetBundleRecordInforFromCache(assetBundleNameUri);

            if (assetBundleRecordInfor != null && assetBundleRecordInfor.IsAssetBundleEnable)
            {
                completeCallback?.Invoke(assetBundleRecordInfor);
                return;
            }

            // 依赖加载AssetBundle 并递归记录依赖关系
            Dictionary <string, string> dependenceBundlesPath = GetAssetBundleAllDependencies(assetBundleNameUri);
            //key = assetBundleName
            Dictionary <string, AssetBundleRecordInfor> allDependenceAssetBundleRecords = null;

            if (dependenceBundlesPath != null && dependenceBundlesPath.Count > 0)
            {
                allDependenceAssetBundleRecords = new Dictionary <string, AssetBundleRecordInfor>(dependenceBundlesPath.Count);
                int depdenceABundleCount = dependenceBundlesPath.Count;
                foreach (var dependencePath in dependenceBundlesPath)
                {
                    LoadAssetBundleRecordAsync(dependencePath.Key, dependencePath.Value, (dependenceRecord) =>
                    {
                        if (dependenceRecord == null)
                        {
                            Debug.LogError($"加载{dependencePath.Key} 的依赖资源{dependencePath.Value} 失败");
                            completeCallback?.Invoke(null);
                            return;
                        } //某个依赖加载失败

                        allDependenceAssetBundleRecords[dependencePath.Key] = dependenceRecord;
                        --depdenceABundleCount;
                        //全部以来加载完
                        if (depdenceABundleCount == 0)
                        {
                            AssetBundleCreateRequest requst = null;

                            if (string.IsNullOrEmpty(assetBundleDirectoryUri))
                            {
                                requst = LoadAssetBundleFromFileAsync(assetBundleNameUri);
                            }
                            else
                            {
                                requst = LoadAssetBundleFromFileAsync(assetBundleDirectoryUri.CombinePathEx(assetBundleNameUri));
                            }

                            if (requst == null)
                            {
                                Debug.LogError($"LoadAssetBundleRecordAsync Fail,AssetBundle NOT Exit {assetBundleNameUri}");
                                completeCallback?.Invoke(null);
                                return;
                            }

                            AsyncManager.StartAsyncOperation(requst, () => completeCallback?.Invoke(RecordAssetBundleDepdence(assetBundleNameUri, requst.assetBundle, allDependenceAssetBundleRecords)), null);
                        }
                    });
                }
            }
        }