Пример #1
0
    public XAssetInfo getAssetInfo(XLoadRes res)
    {
        // 如果有了则直接返回
        if (res.asset != null)
        {
            return(res.asset);
        }

        XAssetKey key = new XAssetKey {
            res = res.res, all = res.all, tp = res.tp
        };

        if (string.IsNullOrEmpty(res.res))
        {
            res.asset = getAssetInfoFromCache(key, XLoadStatus.FAIL);
            return(res.asset);
        }

        // 从AB里面获取一个
        if (isLoadFromAB)
        {
            // 从AB包里面获取
            XBundleInfo info = getAssetBundle(res.res);
            if (info != null)
            {
                // 是否一定要在这里取?--1方便info做引用计数
                res.asset = info.getAssetInfo(key);
            }
            else
            {
                res.asset = getAssetInfoFromCache(key, XLoadStatus.FAIL);
            }
        }
        else
        {
            // 直接在上面在上面进行装载
            res.asset = getAssetInfoFromCache(key, XLoadStatus.NONE);
        }

        return(res.asset);
    }
Пример #2
0
    // 优化协程版本,不开启更多的协程
    IEnumerator coLoad(XLoadDesc desc, Action <XLoadDesc> cb, bool check)
    {
        // asset desc!= null
        if (check)
        {
            fillDesc(desc);
            if (desc.isComplete)
            {
                if (cb != null)
                {
                    cb(desc);
                }
                yield break;
            }
        }
        // 一个一个的装载
        for (int i = 0; i < desc.reses.Count; i++)
        {
            XLoadRes res = desc.reses[i];

            desc.cursor = res;
            XAssetInfo asset = res.asset;
            if (asset.status == XLoadStatus.NONE)
            {
                asset.status = XLoadStatus.LOADING;

                XBundleInfo bundle = res.asset.bundle;
                #region bundle 装载流程
                // 没装载好
                if (bundle.status == XLoadStatus.NONE)
                {
                    bundle.status = XLoadStatus.LOADING;

                    bool childOK = true;
                    // 拍平了的依赖
                    for (int j = 0; j < bundle.dependAll.Count; j++)
                    {
                        XBundleInfo dpab = bundle.dependAll[j];
                        if (dpab.status == XLoadStatus.NONE)
                        {
                            dpab.status = XLoadStatus.LOADING;
                            // 真正装载的路径
                            dpab.www = new WWW(getPath(dpab));

                            // XDebug.LogError("装载依赖"+ getPath(dpab));
                            yield return(dpab.www);

                            if (!string.IsNullOrEmpty(dpab.www.error) || dpab.www.assetBundle == null)
                            {
                                dpab.status = XLoadStatus.FAIL;
                            }
                            else
                            {
                                dpab.ab     = dpab.www.assetBundle;
                                dpab.status = XLoadStatus.SUCESS;
                            }
                            dpab.www.Dispose();
                            dpab.www = null;
                        }

                        if (!dpab.isComplete)
                        {
                            yield return(dpab);
                        }

                        if (dpab.isFail)
                        {
                            bundle.status = XLoadStatus.FAIL;
                            childOK       = false;
                            break;
                        }
                    }
                    // 装载自己
                    if (childOK)
                    {
                        bundle.www = new WWW(getPath(bundle));

                        // XDebug.LogError("装载自己" + getPath(bundle));
                        yield return(bundle.www);

                        if (!string.IsNullOrEmpty(bundle.www.error) || bundle.www.assetBundle == null)
                        {
                            bundle.status = XLoadStatus.FAIL;
                        }
                        else
                        {
                            bundle.ab     = bundle.www.assetBundle;
                            bundle.status = XLoadStatus.SUCESS;
                        }
                        bundle.www.Dispose();
                        bundle.www = null;
                    }
                }
                #endregion

                if (!bundle.isComplete)
                {
                    yield return(bundle);
                }
                #region 装载asset
                // 失败了
                if (bundle.isFail)
                {
                    asset.status = XLoadStatus.FAIL;
                }
                else
                {
                    // bundle成功了
                    if (asset.all)
                    {
                        asset.rq = bundle.ab.LoadAllAssetsAsync(asset.tp);
                        yield return(asset.rq);

                        if (asset.rq.isDone && asset.rq.allAssets != null)
                        {
                            asset.status = XLoadStatus.SUCESS;
                            asset.obs    = asset.rq.allAssets;
                            if (asset.obs != null)
                            {
                                asset.obDict = new Dictionary <string, UnityEngine.Object>();
                                string[] assetNames = bundle.ab.GetAllAssetNames();

                                if (assetNames.Length == asset.obs.Length)
                                {
                                    for (int k = 0; k < asset.obs.Length; k++)
                                    {
                                        asset.obDict[assetNames[k]] = asset.obs[k];
                                    }
                                }
                                else
                                {
                                    // 如果只有一项,本来名称需要相同的
                                    if (assetNames.Length >= 1)
                                    {
                                        for (int k = 0; k < asset.obs.Length; k++)
                                        {
                                            asset.obDict[assetNames[0]] = asset.obs[k];
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            asset.status = XLoadStatus.FAIL;
                        }

                        asset.rq = null;
                    }
                    else
                    {
                        string relname = getRelPathName(asset.res, bundle);
                        asset.rq = bundle.ab.LoadAssetAsync(relname, asset.tp);
                        yield return(asset.rq);

                        if (asset.rq.isDone && asset.rq.asset != null)
                        {
                            asset.status = XLoadStatus.SUCESS;
                            asset.ob     = asset.rq.asset;
                        }
                        else
                        {
                            asset.status = XLoadStatus.FAIL;
                        }

                        asset.rq = null;
                    }
                }
                #endregion
            }

            // 可以让自己直接返回
            if (!asset.isComplete)
            {
                yield return(asset);
            }
        }
        desc._isComplete = true;
        if (cb != null)
        {
            cb(desc);
        }
    }