private void DeleteFile(List <string> deleteList, AssetBundleSettingHelper ABSettingHelper) { if (deleteList == null || deleteList.Count == 0) { return; } foreach (var f in deleteList) { var abpi = ABSettingHelper.GetCurPlatformABPath(f); File.Delete(abpi.FullName); StageInfo.AddUC(); } }
public IEnumerator CheckAndUpdateABToPD(AssetBundleSettingHelper ABSettingHelper, AssetBundleHelper abh, Action callback = null) { StageInfo.ToStage(UpdaterStage.CheckCopySAToPD); { //先大致写下思路,android下,streamingAssets目录是在jar包里的,所以不能直接操作文件和路径,得www或者assetbundle.loadfromfile去读取,但是其他地方可以直接读,所以为了照顾android,SA目录下都改用www和assetbundle.loadfromfile //但是有些地方需要搞复制这种事,所以WWW后可以直接写入到新文件中。所以可以发现很多地方用了www //如果使用AB,检查一下自己的私有目录下是否有assetbundle的拷贝,如果没有则把所有的AB拷过去 //bool bCopySAToPDFinished = false; if (!ABSettingHelper.UseAssetBundle || ABSettingHelper.UseSteamingAssetRes) { StageInfo.ToStage(UpdaterStage.NotNeedUpdater); if (callback != null) { callback.Invoke(); } yield break; } var abpiSrc = ABSettingHelper.GetCurABVersionXmlStreamingAssetPath(); var abpiDst = ABSettingHelper.GetCurABVersionXmlPersistentDataPath(); //首先检查目标文件是否存在 if (!File.Exists(abpiDst.FullName)) { //不存在,这个时候无脑删除文件夹,然后拷贝,防止文件夹里残留了奇怪的东西 abh.StartCoroutine(CopyAllABToPD(ABSettingHelper, abh, () => StageInfo.ToStage(UpdaterStage.Copy_Over))); AssetBundleHelper.TestLog("!File.Exists(abpiDst.FullName):" + abpiDst.FullName); } else { //虽然存在,但是对比一下版本号,如果不如SA目录下的新,也是要覆盖过去的 VersionXmlInfo verSrc = null; //AssetBundleHelper.TestLog("abpiSrc.URI:" + abpiSrc.URI); using (WWW www = new WWW(abpiSrc.URI)) { bool bFind = false; while (true) { if (www.error != null) { Debug.LogError(www.error); AssetBundleHelper.TestLog(www.error); bFind = true; break; } else if (www.isDone) { break; } yield return(null); } if (!bFind) { verSrc = GetVersion(www.bytes); } } if (verSrc != null) { //AssetBundleHelper.TestLog("abpiDst.URI:" + abpiDst.URI); VersionXmlInfo verDst = null; verDst = GetVersion(abpiDst.FullName); //AssetBundleHelper.TestLog("verDst.IsUpTodate(verSrc)"); if (verDst.IsUpTodate(verSrc)) { //目标版本比较新,所以什么都不用干 //AssetBundleHelper.TestLog("Is Up To Date"); StageInfo.ToStage(UpdaterStage.Copy_Over); } else { //无脑拷过去覆盖 //CopyAllABToPD(ABSettingHelper); //AssetBundleHelper.TestLog("Copy all:"); abh.StartCoroutine(CopyAllABToPD(ABSettingHelper, abh, () => StageInfo.ToStage(UpdaterStage.Copy_Over))); } } else { StageInfo.ToStage(UpdaterStage.Copy_Over); } } //AssetBundleHelper.TestLog("Wait bCopySAToPDFinished"); while (!StageInfo.IsStage(UpdaterStage.Copy_Over)) { yield return(null); } } StageInfo.ToStage(UpdaterStage.DownloadRes); //然后检查网络上是否有更新的版本 //尝试从网络上获得version的xml string VersionABPrefixUrl = Path.Combine(UrlPrefix, ABSettingHelper.GetPlatformPathName()); string manifestUrl = Path.Combine(VersionABPrefixUrl, ABSettingHelper.GetPlatformPathName()); var VersionUrl = Path.Combine(VersionABPrefixUrl, VersionXmlInfo.FileName + AssetBundleSettingHelper.xmlExtName); //AssetBundleHelper.TestLog(@"Begin WWW VersionUrl:"+ VersionUrl); using (WWW www = new WWW(VersionUrl)) { while (true) { yield return(null); //AssetBundleHelper.TestLog(@"Begin www Time:" + DateTime.Now.ToString()); if (www.error != null) { //Debug.Assert(false, "www.error != null " + www.error); AssetBundleHelper.TestLog(@"www.error != null " + www.error); StageInfo.ToStage(UpdaterStage.DownloadRes_Error); if (callback != null) { callback.Invoke(); } yield break; } else if (www.isDone) { break; } } // AssetBundleHelper.TestLog(@"Begin etVersion(www.bytes):"); var versionOnline = GetVersion(www.bytes); /* AssetBundleHelper.TestLog(@"Begin e11111111111:"+ versionOnline.GetVersionString());*/ var abpiDst = ABSettingHelper.GetCurABVersionXmlPersistentDataPath(); // AssetBundleHelper.TestLog(@"Begin 222222222222222222:"+ abpiDst.FullName); // AssetBundleHelper.TestLog(@"Begin 333333333333:" + Application.persistentDataPath); VersionXmlInfo verDst = null; try { verDst = GetVersion(abpiDst.FullName); } catch (Exception e) { AssetBundleHelper.TestLog(e.Message); } AssetBundleHelper.TestLog(@"Begin abpiDst.FullName:" + abpiDst.FullName); if (verDst != null && verDst.IsUpTodate(versionOnline)) { AssetBundleHelper.TestLog(@"Up To Date Online:" + abpiDst.FullName); StageInfo.ToStage(UpdaterStage.ResIsUpToDate); //本地的是最新的,不需要更新 if (callback != null) { callback.Invoke(); } yield break; } //Debug.Log("需要更新"); //先下载manifest,然后下载所有需要更新的AB AssetBundleHelper.TestLog(@"Begin WWW manifestUrl:" + manifestUrl); using (WWW wwwMF = new WWW(manifestUrl)) { while (true) { yield return(null); if (wwwMF.error != null) { AssetBundleHelper.TestLog(@"wwwMF.error != null " + wwwMF.error); StageInfo.ToStage(UpdaterStage.DownloadRes_Error); if (callback != null) { callback.Invoke(); } yield break;; } else if (wwwMF.isDone) { break; } } var newManifest = GetPathManifest(wwwMF.bytes); var localManifestABPI = ABSettingHelper.GetCurPlatformManifestPath(); var localManifest = GetPathManifest(localManifestABPI.FullName); List <string> updateList = null; List <string> deleteList = null; GetNeedUpdateList(localManifest, newManifest, out updateList, out deleteList); int needUpdateCount = 0; if (updateList != null) { needUpdateCount += updateList.Count(); } if (deleteList != null) { needUpdateCount += deleteList.Count(); } { StageInfo.SetNUC(needUpdateCount); DeleteFile(deleteList, ABSettingHelper); if (updateList != null && updateList.Count > 0) { foreach (var f in updateList) { string dlUrl = Path.Combine(VersionABPrefixUrl, f); using (WWW wwwUPDATE = new WWW(dlUrl)) { while (true) { yield return(null); if (wwwUPDATE.error != null) { AssetBundleHelper.TestLog(@"wwwUPDATE.error != null " + wwwUPDATE.error); StageInfo.ToStage(UpdaterStage.DownloadRes_Error); if (callback != null) { callback.Invoke(); } yield break;; } else if (wwwUPDATE.isDone) { break; } } var abpi = ABSettingHelper.GetCurPlatformABPath(f); UpdateFile(abpi, wwwUPDATE.bytes, ABSettingHelper); if (wwwUPDATE.assetBundle != null) { wwwUPDATE.assetBundle.Unload(false); } } StageInfo.AddUC(); yield return(null); } } } //保存manifest UpdateFile(localManifestABPI, wwwMF.bytes, ABSettingHelper); if (wwwMF.assetBundle != null) { wwwMF.assetBundle.Unload(false); } StageInfo.AddUC(); yield return(null); } //最后需要保存version。xml UpdateFile(abpiDst, www.bytes, ABSettingHelper); if (www.assetBundle != null) { www.assetBundle.Unload(false); } StageInfo.AddUC(); yield return(null); } StageInfo.ToStage(UpdaterStage.Download_Over); if (callback != null) { callback.Invoke(); } }
/// <summary> /// 这是无脑拷贝StreamingAssets资源到PD目录 /// </summary> private IEnumerator CopyAllABToPD(AssetBundleSettingHelper ABSettingHelper, AssetBundleHelper abh, Action callback = null) { StageInfo.ToStage(UpdaterStage.CopySAToPD); //这里是无脑拷贝版本,为了防止资源出错,可以重新搞一次 var abpiSrc = ABSettingHelper.GetCurABVersionXmlStreamingAssetPath(); var abpiDst = ABSettingHelper.GetCurABVersionXmlPersistentDataPath(); //不存在,这个时候无脑删除文件夹,然后拷贝,防止文件夹里残留了奇怪的东西 if (Directory.Exists(abpiDst.Dir_Full)) { Directory.Delete(abpiDst.Dir_Full, true); } // AssetBundleHelper.TestLog("abpiSrc.Dir_Full:" + abpiSrc.Dir_Full); // AssetBundleHelper.TestLog("abpiSrc.FullName:" + abpiSrc.FullName); // AssetBundleHelper.TestLog("Application.dataPath:" + Application.dataPath); // AssetBundleHelper.TestLog("Application.streamingAssetsPath:" + Application.streamingAssetsPath); // AssetBundleHelper.TestLog("Application.persistentDataPath:" + Application.persistentDataPath); var localSAManifestABPI = ABSettingHelper.GetCurPlatformStreamingAssetManifestPath(); var localSAManifest = GetPathManifest(localSAManifestABPI.FullName); AssetBundleHelper.TestLog("ia am here *****" + localSAManifestABPI.FullName); if (localSAManifest != null) { //AssetBundleHelper.TestLog("if(localManifest != null) )))))))))))))))))))))))))))))))))))))))))))"); var ABs = localSAManifest.GetAllAssetBundles(); StageInfo.SetNUC(ABs.Length); foreach (var f in ABs) { var abpi = ABSettingHelper.GetCurPlatformStreamingABPath(f); var abpiTar = ABSettingHelper.GetCurPlatformABPath(f); using (WWW www = new WWW(abpi.URI)) { while (true) { if (www.error != null) { Debug.LogError(www.error); AssetBundleHelper.TestLog(www.error); throw new NullReferenceException(www.url); } else if (www.isDone) { break; } yield return(null); } CreateFile(abpiTar, www.bytes); if (www.assetBundle != null) { www.assetBundle.Unload(false); } } StageInfo.AddUC(); yield return(null); } //最后把manifeat和version也拷过去 //manifest using (WWW www = new WWW(localSAManifestABPI.URI)) { while (true) { if (www.error != null) { Debug.LogError(www.error); AssetBundleHelper.TestLog(www.error); throw new NullReferenceException(www.url); } else if (www.isDone) { break; } yield return(null); } var localManifestABPI = ABSettingHelper.GetCurPlatformManifestPath(); CreateFile(localManifestABPI, www.bytes); if (www.assetBundle != null) { www.assetBundle.Unload(false); } StageInfo.AddUC(); yield return(null); } //version XML using (WWW www = new WWW(abpiSrc.URI)) { while (true) { if (www.error != null) { Debug.LogError(www.error); AssetBundleHelper.TestLog(www.error); throw new NullReferenceException(www.url); } else if (www.isDone) { break; } yield return(null); } //var localManifestABPI = ABSettingHelper.GetCurPlatformManifestPath(); CreateFile(abpiDst, www.bytes); StageInfo.AddUC(); yield return(null); } } else { //SA目录里没有manifest的时候就什么都别干了 } //CopyFolder(abpiSrc.Dir_Full, abpiDst.Dir_Full); if (callback != null) { callback.Invoke(); } }
private T LoadResource_Sync_Real <T>(string assetName) where T : UnityEngine.Object { if (ABSettingHelper.UseAssetBundle) { if (manifest == null) { throw new NullReferenceException("manifest == null"); } var abpi = ABSettingHelper.GetCurPlatformABPath(assetName); string[] dps = manifest.GetAllDependencies(abpi.DependencyName); for (int i = 0; i < dps.Length; i++) { ABPInfo abpiTT = ABSettingHelper.GetCurPlatformABPath(dps[i]); { if (!AssetBundleDic.ContainsKey(abpiTT.URI)) { var ab = AssetBundle.LoadFromFile(abpiTT.FullName); if (ab != null) { AssetBundleDic.Add(abpiTT.URI, ab, -1); } else { Debug.LogError("Not Found File:" + abpiTT.FullName); } } } } if (!AssetBundleDic.ContainsKey(abpi.URI)) { var ab = AssetBundle.LoadFromFile(abpi.FullName); if (ab != null) { AssetBundleDic.Add(abpi.URI, ab, -1); } else { Debug.LogError("Not Found File:" + abpi.FullName); } } if (!AssetBundleDic.ContainsKey(abpi.URI, false)) { Debug.LogError("!AssetBundleDic.ContainsKey(name) :" + abpi.URI); return(null); } var obj = AssetBundleDic.GetAssetBundle(abpi.URI).LoadAsset <T>(abpi.AssetName); return(obj); } else { #if UNITY_EDITOR var obj = UnityEditor.AssetDatabase.LoadAssetAtPath <T>(assetName); if (obj == null) { Debug.LogError("Asset not load:" + assetName); } return(obj); #else throw new Exception("What!!!!!!!!!!!! look here!!!!!!!!"); #endif } //return null; }