示例#1
0
    // 同步载入AssetBundle
    public AssetBundle LoadAssetBundle(string bundleName)
    {
        BundleRef br = null;

        if (bundles.TryGetValue(bundleName, out br) && br.bundle != null)
        {
            br.refNum += 1;
            return(br.bundle);
        }

        // 获取相关依赖,并载入
        string[] dependences = assetBundleManifest.GetAllDependencies(bundleName);
        for (int i = 0; i < dependences.Length; ++i)
        {
            br = null;

            if (string.IsNullOrEmpty(dependences[i]) ||
                string.IsNullOrEmpty(dependences[i].Trim()))
            {
                continue;
            }

            // 已经载入的依赖包,增加引用计数
            if (bundles.TryGetValue(dependences[i], out br) && br.bundle != null)
            {
                br.refNum += 1;
                continue;
            }

            // 优先获取的Patched路径
            string dependPath = GetLatestAssetBundlePath(dependences[i]);

            br        = new BundleRef();
            br.bundle = AssetBundle.LoadFromFile(dependPath);
            br.refNum = 1;

            bundles[dependences[i]] = br;
        }

        string abPath = GetLatestAssetBundlePath(bundleName);

        br        = new BundleRef();
        br.bundle = AssetBundle.LoadFromFile(abPath);
        br.refNum = 1;

        bundles[bundleName] = br;

        return(br.bundle);
    }
示例#2
0
    // 协程等待载入AssetBundle结束
    IEnumerator TillLoadAssetBundleEndCoroutine(
        Coroutine co,
        string bundleName,
        Action <AssetBundle> onFinished)
    {
        BundleRef br = bundles[bundleName];

        while (br.loadCoroutine != null)
        {
            yield return(null);
        }

        if (onFinished != null)
        {
            onFinished(br.bundle);
            onFinished = null;
        }
    }
示例#3
0
    // 异步载入AssetBundle
    void LoadAssetBundleAsync(string bundleName, Action <AssetBundle> onFinished)
    {
        BundleRef br;

        if (!bundles.TryGetValue(bundleName, out br))
        {
            br = new BundleRef();
            bundles[bundleName] = br;
        }

        // 没载入过,则启动异步载入
        if (br.loadCoroutine == null)
        {
            br.loadCoroutine = StartCoroutine(LoadAssetBundleCoroutine(bundleName, onFinished));
        }
        else
        {
            // 等待载入结束
            StartCoroutine(TillLoadAssetBundleEndCoroutine(br.loadCoroutine, bundleName, onFinished));
        }
    }
示例#4
0
    // 协程载入AssetBundle
    IEnumerator LoadAssetBundleCoroutine(string bundleName, Action <AssetBundle> onFinished)
    {
        BundleRef br;

        if (bundles.TryGetValue(bundleName, out br) && br.bundle != null)
        {
            br.refNum += 1;

            if (onFinished != null)
            {
                onFinished(br.bundle);
                onFinished = null;
            }

            br.loadCoroutine = null;
            yield break;
        }

        string[]         dependences = assetBundleManifest.GetAllDependencies(bundleName);
        List <BundleRef> asyncList   = new List <BundleRef>();

        for (int i = 0; i < dependences.Length; ++i)
        {
            br = null;

            if (string.IsNullOrEmpty(dependences[i]) || string.IsNullOrEmpty(dependences[i].Trim()))
            {
                continue;
            }

            // 已经载入过了,增加计数
            if (bundles.TryGetValue(dependences[i], out br) && br.bundle != null)
            {
                br.refNum += 1;
                continue;
            }

            if (br == null)
            {
                br = new BundleRef();
                bundles[dependences[i]] = br;
            }

            if (br.request == null)
            {
                string dependPath = GetLatestAssetBundlePath(dependences[i]);
                br.request = AssetBundle.LoadFromFileAsync(dependPath);
            }

            asyncList.Add(br);
        }

        br = null;
        if (bundles.TryGetValue(bundleName, out br) && br.request == null)
        {
            string abPath = GetLatestAssetBundlePath(bundleName);
            br.request = AssetBundle.LoadFromFileAsync(abPath);
        }

        asyncList.Add(br);

        for (int i = 0; i < asyncList.Count; ++i)
        {
            br = asyncList[i];
            if (br.bundle != null)
            {
                continue;
            }

            if (br.request == null)
            {
                continue;
            }

            if (br.request.isDone)
            {
                br.bundle = br.request.assetBundle;
                continue;
            }

            br.waitNum += 1;
            if (br.waitNum > 1)
            {
                while (br.waitNum > 1 && !br.request.isDone)
                {
                    yield return(null);
                }
            }
            else
            {
                yield return(br.request);
            }

            br.waitNum -= 1;

            AssetBundle loadedAB = (br.bundle == null) ? br.request.assetBundle : br.bundle;
            br.bundle  = loadedAB;
            br.refNum += 1;

            // 请求结束
            if (br.waitNum == 0)
            {
                br.request = null;
            }
        }

        if (onFinished != null)
        {
            onFinished(br.bundle);
            onFinished = null;
        }

        br.loadCoroutine = null;
        asyncList.Clear();
    }
示例#5
0
 public void AddRef(BundleRef br)
 {
     br.Retain();
     bundleRefs.Add(br);
 }
示例#6
0
    public IEnumerator AsyncLoadBundle(string path, LoadBundleDelegate lbd)
    {
        if (bundles.ContainsKey(path))
        {
            BundleHolder bh = bundles[path];
            if (bh.bundle)
            {
                lbd?.Invoke(bh);
            }
            else
            {
                bh.afterLoad += lbd;
            }
        }
        else
        {
            BundleHolder bh = new BundleHolder();
            bh.br         = this;
            bh.bundleName = path;
            AddBundle(path, bh);

            bool fail     = false;
            int  depCount = 0;
            if (depMap.ContainsKey(path))
            {
                List <string> depList = depMap[path];
                depCount = depList.Count;
                foreach (string depName in depList)
                {
                    StartCoroutine(AsyncLoadBundle(depName, delegate(BundleHolder bh1)
                    {
                        if (bh1 == null)
                        {
                            fail = true;
                            return;
                        }

                        if (fail)
                        {
                            bh1.TryRelease();
                        }
                        else
                        {
                            BundleRef br = new BundleRef();
                            br.bh        = bh1;
                            bh.AddRef(br);
                        }
                    }));
                }
            }

            string readPath = Path.Combine(Application.dataPath, "../../Bundle", path);
            AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(readPath);

            while (true)
            {
                if (fail)
                {
                    bh.TryRelease();
                    lbd?.Invoke(null);
                    bh.AfterLoad(null);
                    yield break;
                }
                else
                {
                    if (bh.bundleRefs.Count == depCount)
                    {
                        if (abcr.isDone)
                        {
                            AssetBundle ab = abcr.assetBundle;
                            if (ab != null)
                            {
                                bh.bundle = ab;
                                lbd?.Invoke(bh);
                                bh.AfterLoad(bh);
                                yield break;
                            }
                            else
                            {
                                bh.TryRelease();
                                lbd?.Invoke(null);
                                bh.AfterLoad(null);
                                yield break;
                            }
                        }
                        else
                        {
                            yield return(null);
                        }
                    }
                    else
                    {
                        yield return(null);
                    }
                }
            }
        }
    }
    void OnGUI()
    {
        if (!BundleService.IsValidate())
        {
            return;
        }

        JWObjDictionary <string, BundleRef> bundleDict = BundleService.GetInstance().BundleDict;

        GUILayout.Space(3f);
        GUILayout.BeginVertical();


        // list title
        GUILayout.BeginHorizontal("Table Title", GUILayout.MinHeight(20f));
        GUILayout.Label("Index", _labelStyle, GUILayout.Width(60f));
        GUILayout.Label("RefCnt", _labelStyle, GUILayout.Width(60f));
        GUILayout.Label("Tag", _labelStyle, GUILayout.Width(120f));
        GUILayout.Label("Name", _labelStyle, GUILayout.Width(60f));
        GUILayout.EndHorizontal();

        ResPackConfig config = ResService.GetInstance().PackConfig;

        if (config == null)
        {
            return;
        }

        // list
        mScroll = GUILayout.BeginScrollView(mScroll);

        int index = 0;

        foreach (var kv in bundleDict)
        {
            BundleRef      bundle = kv.Value;
            BundlePackInfo pi     = config.GetPackInfo(bundle.Path) as BundlePackInfo;
            if (pi == null)
            {
                continue;
            }

            index++;

            GUILayout.BeginHorizontal("Table Row", GUILayout.MinHeight(20f));

            // index
            GUILayout.Label(index.ToString(), _labelStyle, GUILayout.Width(60f));

            // ref count
            GUILayout.Label(bundle.RefCnt.ToString(), _labelStyle, GUILayout.Width(60f));

            // tag
            //GUILayout.Label(pi.Tag, _labelStyle, GUILayout.Width(120f));

            // path
            GUILayout.Label(pi.Path, _labelStyle);

            GUILayout.EndHorizontal();
        }

        GUILayout.EndScrollView();
        GUILayout.EndVertical();
        GUILayout.Space(3f);
    }