Ejemplo n.º 1
0
        void SaveAssetBundle(string fileName, byte[] bytes, Action saveLocalComplate = null)
        {
            string     path     = AssetBundleMgr.GetApplicationdataPath() + fileName;
            FileInfo   fileInfo = new FileInfo(path);
            FileStream fs       = fileInfo.Create();

            fs.Write(bytes, 0, bytes.Length);
            fs.Flush();
            fs.Close();
            fs.Dispose();

            if (saveLocalComplate != null)
            {
                saveLocalComplate();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 下载根目录AssetBundle文件
        /// </summary>
        /// <returns></returns>
        IEnumerator DownloadAssetBundles()
        {
            using (UnityWebRequest www = UnityWebRequest.Get(AssetBundlePath + MainAssetBundleName))
            {
                yield return(www.SendWebRequest());

                if (www.isNetworkError)
                {
                    yield break;
                }

                byte[] datas = www.downloadHandler.data;
                SaveAssetBundle(MainAssetBundleName, datas);
                string      localPath       = AssetBundleMgr.GetApplicationdataPath() + MainAssetBundleName;
                AssetBundle mainAssetBundle = AssetBundle.LoadFromFile(localPath);
                if (mainAssetBundle == null)
                {
                    yield break;
                }
                //获取AssetBundleManifest文件
                AssetBundleManifest manifest = mainAssetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");

                //获取AssetBundleManifest中的所有AssetBundle的名称信息
                string[] assets = manifest.GetAllAssetBundles();
                for (int i = 0; i < assets.Length; i++)
                {
                    Debug.Log(AssetBundlePath + assets[i]);
                    //开启协程下载所有的
                    StartCoroutine(DownloadAssetBundleAndSave(AssetBundlePath, assets[i], () =>
                    {
                        //下载完成,按照之前的方法,从本地加载AssetBundle并设置。
                        Image_BackGround.overrideSprite = AssetBundleMgr.LoadResource <Sprite>("UI_1003", "uibackground");
                    }));
                }
            }
        }