ResourceHelper.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System;
  6. using UnityEngine.Networking;
  7. public enum E_AssetBundleLoadType
  8. {
  9. /// <summary>
  10. /// www方式异步加载
  11. /// </summary>
  12. WWW = 0,
  13. /// <summary>
  14. /// LoadFromFile方式异步加载
  15. /// </summary>
  16. LoadFromFileAsync = 1,
  17. /// <summary>
  18. /// LoadFromFile方式同步加载
  19. /// </summary>
  20. LoadFromFileSynchro = 2,
  21. /// <summary>
  22. /// 网络加载
  23. /// </summary>
  24. NetUrlMode = 3,
  25. /// <summary>
  26. /// 新版资源包加载方式
  27. /// </summary>
  28. NewAssetBundle = 4,
  29. }
  30. /// <summary>
  31. /// 资源管理器
  32. /// By WJ 2019-12-12 09:03:30
  33. /// </summary>
  34. public class ResourceHelper : MonoSingleton<ResourceHelper>
  35. {
  36. /// <summary>
  37. /// 正在加载的相同AssetBundle列表
  38. /// </summary>
  39. private Dictionary<string, List<Action<AssetBundle>>> mSameAssetBundleAcList = new Dictionary<string, List<Action<AssetBundle>>>();
  40. /// <summary>
  41. /// 已加载的AssetBundle
  42. /// </summary>
  43. private Dictionary<string, AssetBundle> mAssetBundleList = new Dictionary<string, AssetBundle>();
  44. /// <summary>
  45. /// 正在加载的资源名称列表
  46. /// </summary>
  47. private List<string> mLoadingAssetList = new List<string>();
  48. /// <summary>
  49. /// 加载进度
  50. /// </summary>
  51. private float mLoadProgress = 0;
  52. /// <summary>
  53. /// 超时时间
  54. /// </summary>
  55. private float mTimeOutTime = 0;
  56. #region 新版加载方式
  57. /// <summary>
  58. /// 主文件
  59. /// </summary>
  60. private AssetBundleManifest abManifest = null;
  61. /// <summary>
  62. /// 主文件请求
  63. /// </summary>
  64. private AssetBundle manifesAssetBundle = null;
  65. /// <summary>
  66. /// 资源包的请求
  67. /// </summary>
  68. private Dictionary<string, AssetBundle> loadNewAssetBundles = new Dictionary<string, AssetBundle>();
  69. /// <summary>
  70. /// 资源包后缀
  71. /// </summary>
  72. private static string AssetBundleSuffix = ".unity3d";
  73. #endregion
  74. ///// <summary>
  75. ///// 加载资源
  76. ///// </summary>
  77. ///// <param name="_name">名称</param>
  78. ///// <param name="ac">加载完成后的回调</param>
  79. ///// <param name="multiLoad">同一文件是否支持多重加载,false时同一时间只会加载一份</param>
  80. ///// <param name="type">加载的类型,默认LoadFromFile异步加载</param>
  81. //public void LoadAssetBundle(string _name, Action<AssetBundle> ac, bool multiLoad = true, E_AssetBundleLoadType type = E_AssetBundleLoadType.LoadFromFileAsync)
  82. //{
  83. // switch (type)
  84. // {
  85. // case E_AssetBundleLoadType.WWW:
  86. // LoadAssetBundleWWW(_name, ac, multiLoad);
  87. // break;
  88. // case E_AssetBundleLoadType.LoadFromFileAsync:
  89. // LoadAssetBundleAsync(_name, ac, multiLoad);
  90. // break;
  91. // case E_AssetBundleLoadType.LoadFromFileSynchro:
  92. // LoadAssetBundleSynchro(_name, ac);
  93. // break;
  94. // //case E_AssetBundleLoadType.NetUrlMode:
  95. // // LoadAssetBundleUrl(_name, ac);
  96. // // break;
  97. // //case E_AssetBundleLoadType.NewAssetBundle:
  98. // // LoadAssetBundleNew(_name, ac);
  99. // // break;
  100. // }
  101. //}
  102. /// <summary>
  103. /// 新版加载资源包
  104. /// </summary>
  105. /// <param name="_type">所属类型名称</param>
  106. /// <param name="_name">加载的资源名称</param>
  107. /// <param name="ac">加载完成的回调</param>
  108. public void LoadAssetBundle(string resName, Action<AssetBundle> ac = null)
  109. {
  110. if (string.IsNullOrEmpty(resName))
  111. {
  112. return;
  113. }
  114. string assetBundleName = resName.ToLower() + AssetBundleSuffix;
  115. string mainPath = GetNewPath("", "");
  116. if (manifesAssetBundle == null)
  117. {
  118. manifesAssetBundle = AssetBundle.LoadFromFile(mainPath + "NewRes");
  119. }
  120. // 加载主文件
  121. if (abManifest == null)
  122. {
  123. abManifest = manifesAssetBundle.LoadAsset<AssetBundleManifest>("assetbundlemanifest");
  124. }
  125. var hash = abManifest.GetAssetBundleHash(assetBundleName);
  126. // 新版资源,查找依赖
  127. if (hash.isValid)
  128. {
  129. Debug.Log(resName+"存在依赖关系,使用新版加载方式");
  130. // 再查找依赖资源包加载并创建
  131. string[] strs = abManifest.GetAllDependencies(assetBundleName);
  132. foreach (var itemName in strs)
  133. {
  134. if (loadNewAssetBundles.ContainsKey(itemName) == false)
  135. {
  136. try
  137. {
  138. AssetBundle ylAssetBundle = AssetBundle.LoadFromFile(mainPath + itemName);
  139. loadNewAssetBundles.Add(itemName, ylAssetBundle);
  140. }
  141. catch (Exception e)
  142. {
  143. UI_CueDialog.Instance().Open("加载异常:" + e.Message);
  144. }
  145. }
  146. }
  147. }
  148. // 旧版资源包,用正常名称
  149. else
  150. {
  151. Debug.Log(resName + "不存在依赖关系,使用旧版加载方式");
  152. assetBundleName = resName + AssetBundleSuffix;
  153. }
  154. if (loadNewAssetBundles.ContainsKey(assetBundleName) == false)
  155. {
  156. // 最后再加载并创建自身资源包
  157. AssetBundle ab = AssetBundle.LoadFromFile(mainPath + assetBundleName);
  158. loadNewAssetBundles.Add(assetBundleName, ab);
  159. if (ac == null)
  160. {
  161. GameObject go = ab.LoadAsset<GameObject>(resName);
  162. if (go != null)
  163. {
  164. Instantiate(go);
  165. }
  166. }
  167. else
  168. {
  169. ac(ab);
  170. }
  171. }
  172. else
  173. {
  174. AssetBundle ab = loadNewAssetBundles[assetBundleName];
  175. if (ac == null)
  176. {
  177. GameObject go = ab.LoadAsset<GameObject>(resName);
  178. if (go != null)
  179. {
  180. Instantiate(go);
  181. }
  182. }
  183. else
  184. {
  185. ac(ab);
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// 获取加载进度
  191. /// </summary>
  192. /// <returns></returns>
  193. public float GetLoadProgress()
  194. {
  195. return mLoadProgress;
  196. }
  197. ///// <summary>
  198. ///// 加载网络资源
  199. ///// </summary>
  200. ///// <param name="url"></param>
  201. ///// <param name="ac"></param>
  202. ///// <param name="version"></param>
  203. //private void LoadAssetBundleUrl(string _name, Action<AssetBundle> ac)
  204. //{
  205. // if (_name == string.Empty)
  206. // {
  207. // ac(null);
  208. // return;
  209. // }
  210. // if (mAssetBundleList.ContainsKey(_name))
  211. // {
  212. // ac(mAssetBundleList[_name]);
  213. // }
  214. // else
  215. // {
  216. // if (mLoadingAssetList.Contains(_name) == false)
  217. // {
  218. // mLoadingAssetList.Add(_name);
  219. // StartCoroutine(IELoadAssetBundleUrl(_name, ac));
  220. // }
  221. // else
  222. // {
  223. // if (mSameAssetBundleAcList.ContainsKey(_name))
  224. // {
  225. // if (mSameAssetBundleAcList[_name].Contains(ac) == false)
  226. // {
  227. // mSameAssetBundleAcList[_name].Add(ac);
  228. // }
  229. // }
  230. // else
  231. // {
  232. // List<Action<AssetBundle>> acList = new List<Action<AssetBundle>>();
  233. // acList.Add(ac);
  234. // mSameAssetBundleAcList.Add(_name, acList);
  235. // }
  236. // }
  237. // }
  238. //}
  239. ///// <summary>
  240. ///// 协同加载网络资源
  241. ///// </summary>
  242. ///// <param name="url"></param>
  243. ///// <param name="ac"></param>
  244. ///// <param name="version"></param>
  245. ///// <returns></returns>
  246. //IEnumerator IELoadAssetBundleUrl(string _name, Action<AssetBundle> ac, string fileSuffix = ".unity3d")
  247. //{
  248. // // 重新加载
  249. // bool reLoad = true;
  250. // while (reLoad)
  251. // {
  252. // string url = "";
  253. // uint version = 0;
  254. // string streamingAssetsPath = Application.streamingAssetsPath + "/Res/" + _name + fileSuffix;
  255. // if (Launcher.netUpdateFileInfo != null)
  256. // {
  257. // if (Application.isEditor && File.Exists(streamingAssetsPath))
  258. // {
  259. // url = "file://" + streamingAssetsPath;
  260. // }
  261. // else
  262. // {
  263. // if (RuntimePlatform.IPhonePlayer == Application.platform)
  264. // {
  265. // url = Launcher.netUrl + "UpdateAndroid/" + _name + fileSuffix;
  266. // }
  267. // else
  268. // {
  269. // url = Launcher.netUrl + "UpdateAndroid/" + _name + fileSuffix;
  270. // }
  271. // }
  272. // foreach (FileMD5Info info in Launcher.netUpdateFileInfo.Infos)
  273. // {
  274. // if (info.Name == _name + fileSuffix)
  275. // {
  276. // if(info.PackSizeM >= 1)
  277. // {
  278. // UI_LoadingWindow.Instance().Show(1);
  279. // }
  280. // version = (uint)info.PackVersion;
  281. // break;
  282. // }
  283. // }
  284. // }
  285. // else
  286. // {
  287. // version = Launcher.netVersion;
  288. // }
  289. // UnityWebRequest request = null;
  290. // // 编辑器下,有工程资源,不走缓存机制
  291. // if (Application.isEditor && File.Exists(streamingAssetsPath))
  292. // {
  293. // request = UnityWebRequestAssetBundle.GetAssetBundle(url);
  294. // }
  295. // else
  296. // {
  297. // request = UnityWebRequestAssetBundle.GetAssetBundle(url, version, 0);
  298. // }
  299. // yield return request.SendWebRequest();
  300. // // 加载完成,移除正在加载的资源
  301. // mLoadingAssetList.Remove(_name);
  302. // // 等待加载完成
  303. // while (request.isDone == false)
  304. // {
  305. // yield return new WaitForSeconds(0.05f);
  306. // }
  307. // // 没有错误
  308. // if (request.error == null)
  309. // {
  310. // AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
  311. // mAssetBundleList.Add(_name, ab);
  312. // if (ac != null)
  313. // {
  314. // ac(ab);
  315. // }
  316. // reLoad = false;
  317. // // 关闭Loading
  318. // if (mLoadingAssetList.Count == 0)
  319. // {
  320. // UI_LoadingWindow.Instance().Hide(1);
  321. // }
  322. // // 返回相同AC
  323. // if (mSameAssetBundleAcList.ContainsKey(_name))
  324. // {
  325. // for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
  326. // {
  327. // mSameAssetBundleAcList[_name][i](ab);
  328. // }
  329. // mSameAssetBundleAcList[_name].Clear();
  330. // }
  331. // }
  332. // else
  333. // {
  334. // bool waiting = false;
  335. // if (request.isNetworkError)
  336. // {
  337. // // 不可写入
  338. // if (request.error == "Unable to write data")
  339. // {
  340. // reLoad = false;
  341. // }
  342. // else
  343. // {
  344. // reLoad = true;
  345. // waiting = true;
  346. // UI_CueDialog.Instance().Open("加载" + _name + " 失败,是否重试?", "", E_DialogType.TwoBntton, () =>
  347. // {
  348. // // 同意的回调
  349. // waiting = false;
  350. // }, () =>
  351. // {
  352. // // 取消的回调
  353. // Application.Quit();
  354. // });
  355. // }
  356. // }
  357. // else if (request.isHttpError)
  358. // {
  359. // reLoad = true;
  360. // waiting = true;
  361. // UI_CueDialog.Instance().Open("加载" + _name + " 失败,是否重试?", "", E_DialogType.TwoBntton, () =>
  362. // {
  363. // // 同意的回调
  364. // waiting = false;
  365. // }, () =>
  366. // {
  367. // // 取消的回调
  368. // Application.Quit();
  369. // });
  370. // }
  371. // Debug.LogError(_name + " 加载错误: " + request.error);
  372. // while (waiting)
  373. // {
  374. // yield return new WaitForSeconds(0.1f);
  375. // }
  376. // // 不重新加载,则返回回调
  377. // if (reLoad == false)
  378. // {
  379. // if (ac != null)
  380. // {
  381. // ac(null);
  382. // }
  383. // // 关闭Loading
  384. // UI_LoadingWindow.Instance().Hide(1);
  385. // }
  386. // }
  387. // }
  388. //}
  389. /// <summary>
  390. /// WWW加载AssetBundle
  391. /// </summary>
  392. /// <param name="_name">名称,不包含后缀</param>
  393. /// <param name="ac"> Lambda返回值</param>
  394. private void LoadAssetBundleWWW(string _name, Action<AssetBundle> ac, bool multiLoad = true)
  395. {
  396. if (_name == string.Empty)
  397. {
  398. ac(null);
  399. return;
  400. }
  401. if (mAssetBundleList.ContainsKey(_name))
  402. {
  403. ac(mAssetBundleList[_name]);
  404. }
  405. else
  406. {
  407. if (mLoadingAssetList.Contains(_name) == false)
  408. {
  409. mLoadingAssetList.Add(_name);
  410. StartCoroutine(IELoadAssetBundleWWW(_name, ac, multiLoad));
  411. }
  412. else
  413. {
  414. if (mSameAssetBundleAcList.ContainsKey(_name))
  415. {
  416. if (mSameAssetBundleAcList[_name].Contains(ac) == false)
  417. {
  418. mSameAssetBundleAcList[_name].Add(ac);
  419. }
  420. }
  421. else
  422. {
  423. List<Action<AssetBundle>> acList = new List<Action<AssetBundle>>();
  424. acList.Add(ac);
  425. mSameAssetBundleAcList.Add(_name, acList);
  426. }
  427. }
  428. }
  429. }
  430. /// <summary>
  431. /// 协同加载AssetBundle
  432. /// </summary>
  433. /// <param name="_name">名称,不包含后缀</param>
  434. /// <param name="ac"> Lambda返回值</param>
  435. /// <returns>返回资源AssetBundle</returns>
  436. private IEnumerator IELoadAssetBundleWWW(string _name, Action<AssetBundle> ac, bool multiLoad = true)
  437. {
  438. WWW www = new WWW(GetPath(_name));
  439. yield return www;
  440. // 加载完成
  441. mLoadingAssetList.Remove(_name);
  442. if (www != null && www.assetBundle != null)
  443. {
  444. mAssetBundleList.Add(_name, www.assetBundle);
  445. ac(www.assetBundle);
  446. // 返回相同AC
  447. if (mSameAssetBundleAcList.ContainsKey(_name))
  448. {
  449. for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
  450. {
  451. if (multiLoad)
  452. {
  453. mSameAssetBundleAcList[_name][i](www.assetBundle);
  454. }
  455. }
  456. mSameAssetBundleAcList[_name].Clear();
  457. }
  458. }
  459. else
  460. {
  461. ac(null);
  462. // 返回相同AC
  463. if (mSameAssetBundleAcList.ContainsKey(_name))
  464. {
  465. for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
  466. {
  467. if (multiLoad)
  468. {
  469. mSameAssetBundleAcList[_name][i](null);
  470. }
  471. }
  472. mSameAssetBundleAcList[_name].Clear();
  473. }
  474. Debug.LogWarning("没有找到资源__" + _name);
  475. }
  476. }
  477. /// <summary>
  478. /// 同步读取资源
  479. /// </summary>
  480. /// <param name="_name">资源名称(不带后缀)</param>
  481. /// <returns>Object</returns>
  482. private void LoadAssetBundleSynchro(string _name, Action<AssetBundle> ac)
  483. {
  484. if (string.IsNullOrEmpty(_name))
  485. {
  486. ac(null);
  487. return;
  488. }
  489. String path = GetPath2(_name);
  490. if (mAssetBundleList.ContainsKey(_name))
  491. {
  492. ac(mAssetBundleList[_name]);
  493. }
  494. else
  495. {
  496. AssetBundle ab = AssetBundle.LoadFromFile(path);
  497. if (ab != null)
  498. {
  499. mAssetBundleList.Add(_name, ab);
  500. ac(ab);
  501. }
  502. else
  503. {
  504. ac(null);
  505. }
  506. }
  507. }
  508. /// <summary>
  509. /// 异步读取资源
  510. /// </summary>
  511. /// <param name="_name">名称,不包含后缀</param>
  512. /// <param name="ac"> Lambda返回值(必须经过实例化才可用)</param>
  513. private void LoadAssetBundleAsync(string _name, Action<AssetBundle> ac, bool multiLoad = true)
  514. {
  515. if (string.IsNullOrEmpty(_name))
  516. {
  517. ac(null);
  518. return;
  519. }
  520. if (mAssetBundleList.ContainsKey(_name))
  521. {
  522. ac(mAssetBundleList[_name]);
  523. }
  524. else
  525. {
  526. if (mLoadingAssetList.Contains(_name) == false)
  527. {
  528. mLoadingAssetList.Add(_name);
  529. StartCoroutine(IELoadAssetBundleAsync(_name, ac, multiLoad));
  530. }
  531. else
  532. {
  533. if (mSameAssetBundleAcList.ContainsKey(_name))
  534. {
  535. if (mSameAssetBundleAcList[_name].Contains(ac) == false)
  536. {
  537. mSameAssetBundleAcList[_name].Add(ac);
  538. }
  539. }
  540. else
  541. {
  542. List<Action<AssetBundle>> acList = new List<Action<AssetBundle>>();
  543. acList.Add(ac);
  544. mSameAssetBundleAcList.Add(_name, acList);
  545. }
  546. }
  547. }
  548. }
  549. /// <summary>
  550. /// 协同加载AssetBundle
  551. /// </summary>
  552. /// <param name="_name">名称,不包含后缀</param>
  553. /// <param name="ac"> Lambda返回值</param>
  554. /// <returns>返回资源AssetBundle</returns>
  555. private IEnumerator IELoadAssetBundleAsync(string _name, Action<AssetBundle> ac, bool multiLoad = true)
  556. {
  557. String path = GetPath2(_name);
  558. AssetBundleCreateRequest abcRequest = AssetBundle.LoadFromFileAsync(path);
  559. // 等待加载完成
  560. while (abcRequest != null && abcRequest.isDone == false)
  561. {
  562. mLoadProgress = abcRequest.progress;
  563. yield return new WaitForSeconds(0.015f);
  564. }
  565. mLoadProgress = 1.0f;
  566. // 加载完成
  567. mLoadingAssetList.Remove(_name);
  568. if (abcRequest != null && abcRequest.assetBundle != null)
  569. {
  570. mAssetBundleList.Add(_name, abcRequest.assetBundle);
  571. ac(abcRequest.assetBundle);
  572. // 返回相同AC
  573. if (mSameAssetBundleAcList.ContainsKey(_name))
  574. {
  575. for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
  576. {
  577. if (multiLoad)
  578. {
  579. mSameAssetBundleAcList[_name][i](abcRequest.assetBundle);
  580. }
  581. }
  582. mSameAssetBundleAcList[_name].Clear();
  583. }
  584. }
  585. else
  586. {
  587. ac(null);
  588. // 返回相同AC
  589. if (mSameAssetBundleAcList.ContainsKey(_name))
  590. {
  591. for (int i = 0; i < mSameAssetBundleAcList[_name].Count; i++)
  592. {
  593. mSameAssetBundleAcList[_name][i](null);
  594. }
  595. mSameAssetBundleAcList[_name].Clear();
  596. }
  597. Debug.LogWarning("没有找到资源__" + _name);
  598. }
  599. }
  600. /// <summary>
  601. /// 加载外部纯文本 by:jgao
  602. /// </summary>
  603. /// <param name="strName">url路径</param>
  604. /// <param name="ac">回调</param>
  605. /// <param name="postData">数据</param>
  606. public void LoadText(string strName, Action<string> ac, Dictionary<string, string> postData = null)
  607. {
  608. if (strName == string.Empty)
  609. {
  610. return;
  611. }
  612. StartCoroutine(IELoadText(strName, ac, postData));
  613. }
  614. /// <summary>
  615. /// 加载外部纯文本 by:jgao
  616. /// </summary>
  617. /// <param name="name">名称</param>
  618. /// <param name="ac">回调</param>
  619. /// <param name="postData">数据</param>
  620. /// <returns>返回资源AssetBundle</returns>
  621. private IEnumerator IELoadText(string name, Action<string> ac, Dictionary<string, string> postData)
  622. {
  623. WWW www;
  624. if (postData == null)
  625. {
  626. www = new WWW(name);
  627. }
  628. else
  629. {
  630. WWWForm form = new WWWForm();
  631. foreach (KeyValuePair<string, string> kv in postData)
  632. {
  633. form.AddField(kv.Key, kv.Value);
  634. }
  635. www = new WWW(name, form);
  636. }
  637. DateTime timer = DateTime.MinValue;
  638. float lastProgress = 0.0f;
  639. // 超时检测(苹果支付时不考虑超时问题)
  640. while (false == www.isDone && postData == null)
  641. {
  642. if (www.error != null)
  643. {
  644. Debug.LogWarning("网络超时,请检查网络后重试!");
  645. yield break;
  646. }
  647. if (www.progress.Equals(0))
  648. {
  649. if (lastProgress.Equals(0) && timer == DateTime.MinValue)
  650. {
  651. lastProgress = www.progress;
  652. timer = DateTime.Now;
  653. }
  654. else
  655. {
  656. double diff = (DateTime.Now - timer).TotalMilliseconds;
  657. if (diff > Const.NetDefaultTimeOut)
  658. {
  659. Debug.LogWarning("网络超时,请检查网络后重试!");
  660. yield break;
  661. }
  662. }
  663. }
  664. else
  665. {
  666. // 进度值一直不动,超过10s认为断网
  667. if (www.progress.Equals(lastProgress))
  668. {
  669. double diff = (DateTime.Now - timer).TotalMilliseconds;
  670. if (diff > Const.NetDefaultTimeOut)
  671. {
  672. Debug.LogWarning("网络超时,请检查网络后重试!");
  673. yield break;
  674. }
  675. }
  676. else
  677. {
  678. // 正常下载,重置状态
  679. lastProgress = www.progress;
  680. timer = DateTime.Now;
  681. }
  682. }
  683. yield return null;
  684. }
  685. yield return www;
  686. if (www.error == null)
  687. {
  688. ac(www.text);
  689. www.Dispose();
  690. }
  691. else
  692. {
  693. Debug.LogWarning(www.error);
  694. }
  695. }
  696. /// <summary>
  697. /// 卸载AssetBundle
  698. /// </summary>
  699. /// <param name="_name">名称,不包含后缀</param>
  700. /// <param name="forceUnload">强力卸载,为真时即便是常驻内存也将被卸载</param>
  701. /// <returns>是否卸载成功</returns>
  702. public bool UnloadAssetBundle(string _name)
  703. {
  704. if (mAssetBundleList.ContainsKey(_name))
  705. {
  706. mAssetBundleList[_name].Unload(true);
  707. mAssetBundleList.Remove(_name);
  708. return true;
  709. }
  710. return false;
  711. }
  712. /// <summary>
  713. /// 清除所有AssetBundle
  714. /// </summary>
  715. /// <param name="forceUnload">强力卸载,为真时即便是常驻内存也将被卸载</param>
  716. public void ClearAllAssetBundle()
  717. {
  718. List<string> KeyList = new List<string>(mAssetBundleList.Keys);
  719. for (int i = 0; i < KeyList.Count; i++)
  720. {
  721. UnloadAssetBundle(KeyList[i]);
  722. }
  723. }
  724. /// <summary>
  725. /// 获取异步加载资源路径(用于 www 加载)
  726. /// </summary>
  727. /// <param name="strName">资源名称</param>
  728. /// <param name="fileSuffix">文件后缀</param>
  729. /// <returns>返回实际路径</returns>
  730. public static string GetPath(string strName, string fileSuffix = ".unity3d")
  731. {
  732. string strPath = string.Empty;
  733. string persistentDataPath = Application.persistentDataPath + "/Res/" + strName + fileSuffix;
  734. string streamingAssetsPath = Application.streamingAssetsPath + "/Res/" + strName + fileSuffix;
  735. if (Application.isEditor)
  736. {
  737. if (File.Exists(streamingAssetsPath))
  738. {
  739. strPath = "file://" + streamingAssetsPath;
  740. }
  741. else
  742. {
  743. strPath = "file:///" + persistentDataPath;
  744. }
  745. }
  746. else
  747. {
  748. if (File.Exists(persistentDataPath))
  749. {
  750. strPath = "file://" + persistentDataPath;
  751. }
  752. else
  753. {
  754. if (RuntimePlatform.Android == Application.platform)
  755. {
  756. strPath = streamingAssetsPath;
  757. }
  758. else
  759. {
  760. strPath = "file://" + streamingAssetsPath;
  761. }
  762. }
  763. }
  764. return strPath;
  765. }
  766. /// <summary>
  767. /// 获取加载资源路径(用于 LoadFromFile 的同步和异步加载)
  768. /// </summary>
  769. /// <param name="strName">资源名称</param>
  770. /// <param name="fileSuffix">文件后缀</param>
  771. /// <returns>返回实际路径</returns>
  772. public static string GetPath2(string strName, string fileSuffix = ".unity3d")
  773. {
  774. string path = "";
  775. string persistentDataPath = Application.persistentDataPath + "/Res/" + strName + fileSuffix;// 沙盒路径
  776. string streamingAssetsPath = Application.streamingAssetsPath + "/Res/" + strName + fileSuffix;
  777. if (Application.isEditor)
  778. {
  779. if (File.Exists(streamingAssetsPath))
  780. {
  781. path = streamingAssetsPath;
  782. }
  783. else
  784. {
  785. path = persistentDataPath;
  786. }
  787. }
  788. else
  789. {
  790. if (File.Exists(persistentDataPath))
  791. {
  792. path = persistentDataPath;
  793. }
  794. else
  795. {
  796. if (RuntimePlatform.Android == Application.platform)
  797. {
  798. path = Application.dataPath + "!assets" + "/Res/" + strName + fileSuffix;
  799. }
  800. else
  801. {
  802. path = streamingAssetsPath;
  803. }
  804. }
  805. }
  806. return path;
  807. }
  808. /// <summary>
  809. /// 获取加载资源路径(用于 LoadFromFile 的同步和异步加载)
  810. /// </summary>
  811. /// <param name="strName">资源名称</param>
  812. /// <param name="fileSuffix">文件后缀</param>
  813. /// <returns>返回实际路径</returns>
  814. public static string GetNewPath(string strName, string fileSuffix = ".unity3d")
  815. {
  816. string path = "";
  817. //string persistentDataPath = Application.persistentDataPath + "/NewRes/" + strName + fileSuffix;// 沙盒路径
  818. string streamingAssetsPath = Application.streamingAssetsPath + "/NewRes/" + strName + fileSuffix;
  819. if (Application.isEditor)
  820. {
  821. //if (File.Exists(streamingAssetsPath))
  822. {
  823. path = streamingAssetsPath;
  824. }
  825. //else
  826. //{
  827. // path = persistentDataPath;
  828. //}
  829. }
  830. else
  831. {
  832. //if (File.Exists(persistentDataPath))
  833. //{
  834. // path = persistentDataPath;
  835. //}
  836. //else
  837. {
  838. if (RuntimePlatform.Android == Application.platform)
  839. {
  840. path = Application.dataPath + "!assets" + "/NewRes/" + strName + fileSuffix;
  841. }
  842. else
  843. {
  844. path = streamingAssetsPath;
  845. }
  846. }
  847. }
  848. return path;
  849. }
  850. }