Esempio n. 1
0
        /// <summary>
        /// 底层加载函数,异步加载unity压缩过的AssetBundle,或者同步加载Resources类型
        /// </summary>
        /// <param name="newPath">相对于Resources路径下的资源路径</param>
        /// <param name="callBack">回调函数的参数为ResourceItem对象,可以详细拿到AB中的所有Asset</param>
        /// <param name="type">分类标签</param>
        /// <param name="saveAssetBundle">是否保存AssetBundle对象不卸载,可以保留依赖关系</param>
        /// <param name="clearAfterLoaded">加载完成后清除自己的缓存</param>
        public static void LoadCore(string path, Action<ResourceItem> callBack, EResType type = EResType.None, bool saveAssetBundle = false, bool clearAfterLoaded = false, bool dependLoad = true, bool needCRC = false, int tag = 0, bool isImage = false)
        {
            //LoggerHelper.Error("LoadCore: " + path + saveAssetBundle + clearAfterLoaded);
            var isMainifest = IsManifest(path);
            if (!isMainifest && !IsInit)
            {
                Debug.LogError("has not init mainifest, can not load item: " + path);
                return;
            }
            string newPath = path.ToLower();
            string savePath = "";
            # region isImage
            if (isImage)
            {
                string assetName = getImagAssetName(newPath);
                if (string.IsNullOrEmpty(assetName))
                {
                    Debug.LogError("LoadImageAssets, url error: " + newPath);
                    return;
                }

                savePath = getImagSavePath(newPath);

                if (!m_resourceItemDic.ContainsKey(assetName))
                {
                    //先从本地加载图片
                    //Debug.LogError("savePath: " + savePath);
                    if (string.IsNullOrEmpty(savePath))
                    {
                        Debug.LogError("LoadImageAssets, url error 2: " + newPath);
                        return;
                    }

            #if UNITY_WEBPLAYER
                ResourceItem itemNew = new ResourceItem(path, saveAssetBundle);
            #else
                    FileInfo fi = new FileInfo(savePath);
                    //创建缓存保存目录
                    if (!fi.Directory.Exists)
                        fi.Directory.Create();
                    ResourceItem itemNew;
                    if (!fi.Exists)
                    {
                        //如果本地文件不存在,说明是个网络地址
                        itemNew = new ResourceItem(newPath, saveAssetBundle);
                    }
                    else
                    {
                        itemNew = new ResourceItem("file:///" + savePath, saveAssetBundle);
                    }
            #endif
                    itemNew.NeedCRC = needCRC;
                    itemNew.AddTags(tag);
                    m_resourceItemDic.Add(assetName, itemNew);
                }

                newPath = assetName;
            }
            # endregion

            bool hasContains = false;

            if (!m_resourceItemDic.ContainsKey(newPath))
            {
                var itemNew = new ResourceItem(path, saveAssetBundle);
                itemNew.NeedCRC = needCRC;
                itemNew.AddTags(tag);
                m_resourceItemDic.Add(newPath, itemNew);
            }
            else
            {
                hasContains = !isImage;
                if (hasContains)
                {
                    Debug.Log("hascontains: " + newPath);
                }
            }

            var item = m_resourceItemDic[newPath];
            item.SetType(type);
            item.IsClearAfterLoaded = clearAfterLoaded;
            if (!hasContains && !item.IsLoaded)
            {
                if (dependLoad && IsInit)
                {
                    //Debug.LogError("bb: " + item.Path);
                    var dependList = AssetBundleManifestObject.GetAllDependencies(item.Path.ToLower());
                    List<string> distinctList = new List<string>();
                    for (int i = 0; i < dependList.Length; i++)
                    {
                        if (m_resourceItemDic.ContainsKey(dependList[i]))
                        {
                            Debug.Log("contains depend, path: " + item.Path + "------- file: " + dependList[i]);
                        }
                        else
                        {
                            Debug.Log("not contains depend, path: " + item.Path + "------- file: " + dependList[i]);
                            distinctList.Add(dependList[i]);
                        }
                    }
                    //LoadMulti(distinctList.ToArray(), null, () => GlobalDelegate.Instance.View.StartCoroutine(BeginAssetBundleLoadAsync(item)));
                    LoadMulti(distinctList.ToArray(), null, () => beginLoadItem(item));
                }
                else if (isImage)
                {
                    GlobalDelegate.Instance.View.StartCoroutine(BeginImageLoadAsync(item, savePath));
                }
                else
                {
                    //GlobalDelegate.Instance.View.StartCoroutine(BeginAssetBundleLoadAsync(item));
                    beginLoadItem(item);
                }
            }

            item.AddCallBackFunc(callBack);
            if (item.IsCompleted)
            {
                item.LaunchCallBack();
            }
        }