예제 #1
0
        // Start is called before the first frame update
        void Start()
        {
            // 建议优先加载依赖包 1
            SingleABLoader DependencyABName1LoadObj = new SingleABLoader(_AssetName2DependencyABName1, (abName1) => {
                Debug.Log("依赖包 1 资源加载完毕");

                // 然后优先加载依赖包 2
                SingleABLoader DependencyABName2LoadObj = new SingleABLoader(_AssetName2DependencyABName2, (abName2) => {
                    Debug.Log("依赖包 2 资源加载完毕");

                    // 正式加载资源
                    _LoadObj = new SingleABLoader(_ABName1, (abName) => {
                        Debug.Log("资源加载完毕");
                        // 加载AB资源包中的资源,并克隆
                        UnityEngine.Object tmpobj1 = _LoadObj.LoadAsset(_AssetName1, false);
                        Instantiate(tmpobj1);
                        UnityEngine.Object tmpobj2 = _LoadObj.LoadAsset(_AssetName2, false);
                        Instantiate(tmpobj2);

                        // 查询资源包所有资源名称
                        foreach (string str in _LoadObj.RetriveAllAssetName())
                        {
                            Debug.Log(str);
                        }
                    });
                    StartCoroutine(_LoadObj.LoadAssetBundle());
                });
                StartCoroutine(DependencyABName2LoadObj.LoadAssetBundle());
            });

            StartCoroutine(DependencyABName1LoadObj.LoadAssetBundle());
        }
예제 #2
0
        /// <summary>
        /// 加载 AB 包(包括依赖项)
        /// </summary>
        /// <param name="abName"></param>
        /// <returns></returns>
        public IEnumerator LoadAssetBundle(string abName)
        {
            // AB 包关系的建立
            if (_DicABRelation.ContainsKey(abName) == false)
            {
                ABRelation abRelationObj = new ABRelation(abName);
                _DicABRelation.Add(abName, abRelationObj);
            }

            ABRelation tmpABRelationObj = _DicABRelation[abName];

            // 得到指定AB所有的依赖关系(查询Manifest清单文件)
            string[] strDependencyArray = ABManifestLoader.GetInstance().RetrivalDependency(abName);
            foreach (string item_Dependency in strDependencyArray)
            {
                // 添加“依赖项”
                tmpABRelationObj.AddDependency(item_Dependency);
                // 添加引用(背以来) (方便递归调用)
                yield return(LoadReference(item_Dependency, abName));
            }

            // 真正加载 AB 包
            if (_DicSingleABLoaderCache.ContainsKey(abName) == true)
            {
                yield return(_DicSingleABLoaderCache[abName].LoadAssetBundle());
            }
            else
            {
                _CurrentSingleABLoader = new SingleABLoader(abName, CompleteLoadAB);
                _DicSingleABLoaderCache.Add(abName, _CurrentSingleABLoader);
                yield return(_CurrentSingleABLoader.LoadAssetBundle());
            }
        }