AssetsMgr.cs 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. using System.Text;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using UnityEngine.Playables;
  8. using UnityEngine.SceneManagement;
  9. using UnityEngine.Video;
  10. using UnityEngine.Networking;
  11. using Object = UnityEngine.Object;
  12. using Game.Config;
  13. public enum ELoadType : int
  14. {
  15. OTHER = 0,
  16. UI = 1,
  17. ACTOR = 2,
  18. FX = 3,
  19. AssetBundle = 4,
  20. }
  21. public delegate void AssetCallback<T>(T callback);
  22. public delegate void AssetCallbackWithParams<T>(T callback, long seqId, string assetPath, params string[] assetName);
  23. class Bundle
  24. {
  25. AssetBundle mAsset;
  26. public AssetBundle Asset
  27. {
  28. get { return mAsset; }
  29. }
  30. float mLastUsedTime;
  31. public float LastUsedTime
  32. {
  33. get { return mLastUsedTime; }
  34. set
  35. {
  36. mLastUsedTime = value;
  37. Priority = 0;
  38. }
  39. }
  40. public string mBundleName = string.Empty;
  41. public string BundleName
  42. {
  43. get { return mBundleName; }
  44. }
  45. public int mPriority = 0;
  46. public int Priority
  47. {
  48. get { return mPriority; }
  49. set
  50. {
  51. mPriority = value;
  52. mLastUsedTime = Time.realtimeSinceStartup;
  53. }
  54. }
  55. public Bundle(AssetBundle bundle, string bundleName)
  56. {
  57. mBundleName = bundleName;
  58. mAsset = bundle;
  59. mPriority = 0;
  60. mLastUsedTime = Time.realtimeSinceStartup;
  61. }
  62. public void Unload()
  63. {
  64. if (mAsset != null)
  65. {
  66. mAsset.Unload(false);
  67. mAsset = null;
  68. }
  69. }
  70. }
  71. public class LoadAssetsTask
  72. {
  73. bool hasReturnParam = false;
  74. public bool HasReturnParam
  75. {
  76. get { return hasReturnParam; }
  77. }
  78. Delegate mCallback;
  79. public Delegate Callback
  80. {
  81. get { return mCallback; }
  82. }
  83. string mAssetPath;
  84. public string AssetPath
  85. {
  86. get { return mAssetPath; }
  87. }
  88. string[] mAssetName;
  89. public string[] AssetName
  90. {
  91. get { return mAssetName; }
  92. }
  93. long mLoadingSeqId = 0;
  94. public long LoadingSeqId
  95. {
  96. get { return mLoadingSeqId; }
  97. }
  98. public LoadAssetsTask(long seqId, string assetPath,string[] assetName, Delegate callback, bool returnParam_)
  99. {
  100. mLoadingSeqId = seqId;
  101. mAssetPath = assetPath;
  102. mAssetName = assetName;
  103. mCallback = callback;
  104. hasReturnParam = returnParam_;
  105. }
  106. public void Invoke<T>(T goes)
  107. {
  108. if (hasReturnParam)
  109. {
  110. Callback.DynamicInvoke(goes, mLoadingSeqId, mAssetPath, mAssetName);
  111. }
  112. else
  113. {
  114. Callback.DynamicInvoke(goes);
  115. }
  116. }
  117. }
  118. public class LoadTask
  119. {
  120. string mBundleName;
  121. public string BundleName
  122. {
  123. get { return mBundleName; }
  124. }
  125. List<LoadAssetsTask> mAssetsTask = new List<LoadAssetsTask>();
  126. public List<LoadAssetsTask> AssetsTask
  127. {
  128. get { return mAssetsTask; }
  129. }
  130. public int AssetsTaskCnt
  131. {
  132. get { return mAssetsTask.Count; }
  133. }
  134. Type mDelegateType;
  135. public Type DelegateType
  136. {
  137. get { return mDelegateType; }
  138. }
  139. ELoadType mLoadType;
  140. public ELoadType LoadType
  141. {
  142. get { return mLoadType; }
  143. }
  144. float mLoadStartTime;
  145. public float LoadStartTime
  146. {
  147. get { return mLoadStartTime; }
  148. set
  149. {
  150. mLoadStartTime = value;
  151. }
  152. }
  153. private AssetBundle mBundle = null;
  154. public AssetBundle Bundle
  155. {
  156. get { return mBundle; }
  157. set { mBundle = value; }
  158. }
  159. private bool isDone = false;
  160. public bool IsDone
  161. {
  162. get { return isDone; }
  163. set { isDone = value; }
  164. }
  165. //依赖的ab包是否加载完成
  166. private bool isDependencyDone = true;
  167. public bool IsDependencyDone
  168. {
  169. get { return isDependencyDone; }
  170. set { isDependencyDone = value; }
  171. }
  172. //需要加载ab包是否有依赖包
  173. private bool isDependency = false;
  174. public bool IsDependency
  175. {
  176. get { return isDependency; }
  177. set { isDependency = value; }
  178. }
  179. public LoadTask(string bundleName)
  180. {
  181. mBundleName = bundleName;
  182. // mBundleName = bundleName.ToLower();
  183. // if (!mBundleName.Contains(".unity3d"))
  184. // {
  185. // mBundleName = mBundleName + ".unity3d";
  186. // }
  187. mLoadType = ELoadType.AssetBundle;
  188. //DebugHelper.LogError("bundleName:" + bundleName);
  189. }
  190. public LoadTask(long seqId, string bundleName,string assetPath, string[] assetName, Delegate callback, bool callbackWithParam, Type type, ELoadType loadType = ELoadType.OTHER)
  191. {
  192. mBundleName = bundleName;
  193. // mBundleName = bundleName.ToLower();
  194. // if (!mBundleName.Contains(".unity3d"))
  195. // {
  196. // mBundleName = mBundleName + ".unity3d";
  197. // }
  198. mAssetsTask.Add(new LoadAssetsTask(seqId, assetPath, assetName, callback, callbackWithParam));
  199. mDelegateType = type;
  200. mLoadType = loadType;
  201. //DebugHelper.LogError("bundleName:" + bundleName);
  202. }
  203. public void ChangeTask(long seqId, string bundleName, string assetPath, string[] assetName, Delegate callback, bool callbackWithParam, Type type, ELoadType loadType = ELoadType.OTHER)
  204. {
  205. if (mLoadType == ELoadType.AssetBundle) return;
  206. mAssetsTask.Add(new LoadAssetsTask(seqId, assetPath, assetName, callback, callbackWithParam));
  207. mDelegateType = type;
  208. mLoadType = loadType;
  209. }
  210. public LoadAssetsTask FindTask(long seqId)
  211. {
  212. for (int idx = 0; idx < mAssetsTask.Count; idx++)
  213. {
  214. LoadAssetsTask task = mAssetsTask[idx];
  215. if (task.LoadingSeqId == seqId)
  216. {
  217. return task;
  218. }
  219. }
  220. return null;
  221. }
  222. public void RemoveTask(long seqId)
  223. {
  224. LoadAssetsTask task = FindTask(seqId);
  225. if (task != null)
  226. {
  227. mAssetsTask.Remove(task);
  228. }
  229. }
  230. public bool IsExist(long seqId)
  231. {
  232. for (int idx = 0; idx < mAssetsTask.Count; idx++)
  233. {
  234. LoadAssetsTask task = mAssetsTask[idx];
  235. if (task.LoadingSeqId == seqId)
  236. {
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. }
  243. public class LoadAssetbundleTask
  244. {
  245. private LoadTask mLoader = null;
  246. private AssetBundleCreateRequest mAsync = null;
  247. private bool mbIsDone = false;
  248. private bool mbNotified = false;
  249. public LoadTask Loader
  250. {
  251. get { return mLoader; }
  252. }
  253. public bool IsDone
  254. {
  255. get { return mbIsDone; }
  256. }
  257. public LoadAssetbundleTask(LoadTask info)
  258. {
  259. mLoader = info;
  260. }
  261. public void Update()
  262. {
  263. if (mAsync == null && mLoader.IsDependencyDone)
  264. {
  265. mAsync = AssetsMgr.Instance.CreatePackageLoader(mLoader.BundleName);
  266. if (mAsync == null && !mbNotified)
  267. {
  268. Ending(null);
  269. mbNotified = true;
  270. }
  271. else if (mAsync != null && mAsync.isDone)
  272. {
  273. if (!mbNotified)
  274. {
  275. Ending(null);
  276. mbNotified = true;
  277. }
  278. }
  279. }
  280. else if (mAsync != null && mAsync.isDone)
  281. {
  282. if (!mbNotified)
  283. {
  284. Ending(mAsync.assetBundle);
  285. mbNotified = true;
  286. }
  287. }
  288. }
  289. public void Dispose()
  290. {
  291. mLoader = null;
  292. }
  293. void Ending(AssetBundle bundle)
  294. {
  295. mLoader.Bundle = bundle;
  296. mbIsDone = true;
  297. AssetsMgr.Instance.NotifyAssetbundleLoaded(mLoader, this);
  298. }
  299. }
  300. public class AssetsMgr : SingletonMono<AssetsMgr>
  301. {
  302. private List<string> mResidentAssetBundleList = new List<string>();
  303. private Dictionary<string, string> mAssetsMappingDict = new Dictionary<string, string>();
  304. private Dictionary<string, Bundle> mBundlesLoaded = new Dictionary<string, Bundle>();
  305. private Dictionary<string, LoadTask> mBundlesLoading = new Dictionary<string, LoadTask>();
  306. private Dictionary<string, LoadTask> mCurLoadingBundleDic = new Dictionary<string, LoadTask>();
  307. private List<LoadTask> mLoadTasks = new List<LoadTask>();
  308. private Queue<LoadTask> mLoadFinshedTask = new Queue<LoadTask>();
  309. private List<string> expired = new List<string>();
  310. private List<LoadAssetbundleTask> mAssetbundleLoadTasks = new List<LoadAssetbundleTask>();
  311. private Dictionary<string, LanguageAssetBundleName> abNameInfo = new Dictionary<string, LanguageAssetBundleName>(1024);
  312. private static readonly int GCRepeatRate = 7;//主城重复清理gc时间间隔.
  313. private static readonly int GCReleaseRate = 2;//切换场景gc时间间隔.
  314. private static readonly int MaxLoadingCoroutineCount = 10;
  315. private int mLoadingCoroutineCount = 0;
  316. private AssetBundleManifest assetBundleManifest = null;
  317. private const string c_ResVersionCodeKey = "resversioncode";
  318. private VersionCode m_ResVersionCode = VersionCode.zeroVersionCode;
  319. private string resLanguage = "";
  320. public VersionCode resVersionCode
  321. {
  322. get { return m_ResVersionCode; }
  323. }
  324. public string GetReslanguage()
  325. {
  326. string languagekey = "language";
  327. if (string.IsNullOrEmpty(resLanguage))
  328. {
  329. if (abNameInfo.ContainsKey(languagekey))
  330. {
  331. if (abNameInfo[languagekey].BaseName == "auto")
  332. {
  333. SetLocalizedLanguage();
  334. }
  335. else
  336. {
  337. resLanguage = abNameInfo[languagekey].BaseName;
  338. }
  339. }
  340. else
  341. {
  342. resLanguage = "base";
  343. }
  344. }
  345. return resLanguage;
  346. }
  347. public void SetLocalizedLanguage()
  348. {
  349. switch (Application.systemLanguage)
  350. {
  351. case SystemLanguage.Chinese:
  352. case SystemLanguage.ChineseSimplified:
  353. case SystemLanguage.ChineseTraditional:
  354. {
  355. resLanguage = "base";
  356. }
  357. break;
  358. case SystemLanguage.Afrikaans:
  359. case SystemLanguage.Arabic:
  360. case SystemLanguage.Basque:
  361. case SystemLanguage.Belarusian:
  362. case SystemLanguage.Bulgarian:
  363. case SystemLanguage.Catalan:
  364. case SystemLanguage.Czech:
  365. case SystemLanguage.Danish:
  366. case SystemLanguage.Dutch:
  367. case SystemLanguage.English:
  368. case SystemLanguage.Estonian:
  369. case SystemLanguage.Faroese:
  370. case SystemLanguage.Finnish:
  371. case SystemLanguage.French:
  372. case SystemLanguage.German:
  373. case SystemLanguage.Greek:
  374. case SystemLanguage.Hebrew:
  375. case SystemLanguage.Icelandic:
  376. case SystemLanguage.Indonesian:
  377. case SystemLanguage.Italian:
  378. case SystemLanguage.Japanese:
  379. case SystemLanguage.Korean:
  380. case SystemLanguage.Latvian:
  381. case SystemLanguage.Lithuanian:
  382. case SystemLanguage.Norwegian:
  383. case SystemLanguage.Polish:
  384. case SystemLanguage.Portuguese:
  385. case SystemLanguage.Romanian:
  386. case SystemLanguage.Russian:
  387. case SystemLanguage.SerboCroatian:
  388. case SystemLanguage.Slovak:
  389. case SystemLanguage.Slovenian:
  390. case SystemLanguage.Spanish:
  391. case SystemLanguage.Swedish:
  392. case SystemLanguage.Thai:
  393. case SystemLanguage.Turkish:
  394. case SystemLanguage.Ukrainian:
  395. case SystemLanguage.Vietnamese:
  396. case SystemLanguage.Unknown:
  397. case SystemLanguage.Hungarian:
  398. default:
  399. {
  400. resLanguage = "en_";
  401. }
  402. break;
  403. }
  404. }
  405. public string GetlanguageAbName_EN(string abname)
  406. {
  407. string ret = "";
  408. if (abNameInfo.ContainsKey(abname))
  409. {
  410. ret = abNameInfo[abname].En;
  411. }
  412. return ret;
  413. }
  414. public override void InitMgr()
  415. {
  416. base.InitMgr();
  417. #if UNITY_EDITOR
  418. if (Constants.AssetbundleMode)
  419. {
  420. if (null == assetBundleManifest)
  421. {
  422. GetAssetBundleManifest("assetbundle", ref assetBundleManifest);
  423. }
  424. //StartCoroutine(GetAssetsMapping());
  425. GetAssetsMappingSync();
  426. }
  427. #else
  428. if (null == assetBundleManifest)
  429. {
  430. GetAssetBundleManifest("assetbundle", ref assetBundleManifest);
  431. //StartCoroutine(GetAssetsMapping());
  432. GetAssetsMappingSync();
  433. }
  434. #endif
  435. }
  436. public void InitAbNameCfg(Action action = null)
  437. {
  438. DownloadMgr.Instance.ReadDataFromFile(LanguageAssetBundleName.FileName_S(),data =>
  439. {
  440. if (data != null)
  441. {
  442. CsvReader csvReader = new CsvReader(LanguageAssetBundleName.FileName_S(), data);
  443. LanguageAssetBundleName.OnCsvLoad(csvReader);
  444. LanguageAssetBundleName.Foreach(it =>
  445. {
  446. abNameInfo.Add(it.BaseName, it);
  447. //Debug.Log(it.BaseName);
  448. });
  449. LanguageAssetBundleName.Clear();
  450. }
  451. action?.Invoke();
  452. });
  453. }
  454. private string GetAssetsMappingName()
  455. {
  456. #if UNITY_IOS
  457. Debug.Log("============afi============");
  458. return "{0}afi.bytes";
  459. #else
  460. return "{0}assetsmapping.bytes";
  461. #endif
  462. }
  463. private void GetAssetsMappingSync()
  464. {
  465. string path = string.Format(GetAssetsMappingName(), FileSystem.LocalDocumentPath);
  466. if (!FileSystem.Exists(path))
  467. {
  468. path = string.Format(GetAssetsMappingName(), FileSystem.LocalPackagePath);
  469. }
  470. if (!path.Contains("file://"))
  471. {
  472. path = "file://" + path;
  473. }
  474. try
  475. {
  476. WWW www = new WWW(path);
  477. while (!www.isDone) { }
  478. if (string.IsNullOrEmpty(www.error))
  479. {
  480. string data = www.text;
  481. string[] ContentLines = www.text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  482. for (int idx = 0; idx < ContentLines.Length; idx++)
  483. {
  484. string content = ContentLines[idx];
  485. string[] temp = content.Split(',');
  486. mAssetsMappingDict.Add(temp[0].ToLower(), temp[1]);
  487. }
  488. if (mAssetsMappingDict.ContainsKey(c_ResVersionCodeKey))
  489. {
  490. m_ResVersionCode = mAssetsMappingDict[c_ResVersionCodeKey];
  491. mAssetsMappingDict.Remove(c_ResVersionCodeKey);
  492. }
  493. AssetsObscureUtil.InitAssetsObscureConfig(ref mAssetsMappingDict);
  494. }
  495. else
  496. {
  497. DebugHelper.LogError("Load AssetsMapping Fail:" + www.error + " filePath:" + path);
  498. }
  499. }
  500. catch (Exception e)
  501. {
  502. DebugHelper.LogError("Load AssetsMapping Fail:" + e.ToString() + " filePath:" + path);
  503. }
  504. }
  505. public void InitDependenciesAsync(Action<bool> completeCB)
  506. {
  507. #if UNITY_EDITOR
  508. if (!Constants.AssetbundleMode)
  509. {
  510. if (completeCB != null) completeCB(true);
  511. return;
  512. }
  513. #endif
  514. StartCoroutine(AsyncInitDependencies(completeCB));
  515. }
  516. private IEnumerator AsyncInitDependencies(Action<bool> completeCB)
  517. {
  518. string path = null;
  519. if (null == assetBundleManifest)
  520. {
  521. path = string.Format(GetAssetsMappingName(), FileSystem.LocalDocumentPath);
  522. if (!FileSystem.Exists(path))
  523. {
  524. path = string.Format(GetAssetsMappingName(), FileSystem.LocalPackagePath);
  525. }
  526. if (!path.Contains("file://"))
  527. {
  528. path = "file://" + path;
  529. }
  530. WWW www = new WWW(path);
  531. yield return www;
  532. if (string.IsNullOrEmpty(www.error))
  533. {
  534. byte[] bytes = www.bytes;
  535. byte value;
  536. int length = bytes.Length;
  537. for (int i = 0, iMax = Mathf.FloorToInt(length * 0.5f); i < iMax; i += 2)
  538. {
  539. value = bytes[i];
  540. bytes[i] = bytes[length - i - 1];
  541. bytes[length - i - 1] = value;
  542. }
  543. UTF8Encoding encoding = new UTF8Encoding(false);
  544. string data = encoding.GetString(bytes);
  545. string[] ContentLines = data.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  546. for (int idx = 0; idx < ContentLines.Length; idx++)
  547. {
  548. string content = ContentLines[idx];
  549. string[] temp = content.Split(',');
  550. mAssetsMappingDict.Add(temp[0].ToLower(), temp[1]);
  551. }
  552. if (mAssetsMappingDict.ContainsKey(c_ResVersionCodeKey))
  553. {
  554. m_ResVersionCode = mAssetsMappingDict[c_ResVersionCodeKey];
  555. mAssetsMappingDict.Remove(c_ResVersionCodeKey);
  556. }
  557. AssetsObscureUtil.InitAssetsObscureConfig(ref mAssetsMappingDict);
  558. }
  559. else
  560. {
  561. DebugHelper.LogError("Load AssetsMapping Fail:" + www.error + " filePath:" + path);
  562. }
  563. string assetbundleName = AssetsObscureUtil.GetABFileName("assetbundle");
  564. ulong offset = AssetsObscureUtil.GetABOffset(assetbundleName);
  565. path = string.Format("{0}{1}", FileSystem.LocalDocumentPath, assetbundleName);
  566. if (!FileSystem.Exists(path))
  567. {
  568. path = string.Format("{0}{1}", FileSystem.LocalPackagePath, assetbundleName);
  569. }
  570. var assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(path, 0, offset);
  571. yield return assetBundleCreateRequest;
  572. var assetBundleRequest = assetBundleCreateRequest.assetBundle.LoadAssetAsync("AssetBundleManifest");
  573. yield return assetBundleRequest;
  574. assetBundleManifest = (AssetBundleManifest)assetBundleRequest.asset;
  575. if (completeCB != null) completeCB(true);
  576. }
  577. }
  578. public void InitResidentAssetBundleList(Action<bool> completeCB)
  579. {
  580. #if UNITY_EDITOR
  581. if (!Constants.AssetbundleMode)
  582. {
  583. if (completeCB != null) completeCB(true);
  584. return;
  585. }
  586. #endif
  587. StartCoroutine(AsyncInitResidentAssetBundleList(completeCB));
  588. }
  589. private IEnumerator AsyncInitResidentAssetBundleList(Action<bool> completeCB)
  590. {
  591. string shaderName = AssetsObscureUtil.GetABFileName("shader.unity3d");
  592. ulong offset = AssetsObscureUtil.GetABOffset(shaderName);
  593. mResidentAssetBundleList.Clear();
  594. mResidentAssetBundleList.Add(shaderName);
  595. string path = string.Format("{0}{1}", FileSystem.LocalDocumentPath, shaderName);
  596. if (!FileSystem.Exists(path))
  597. {
  598. path = string.Format("{0}{1}", FileSystem.LocalPackagePath, shaderName);
  599. }
  600. AssetBundleCreateRequest shaderABCreateRequest = AssetBundle.LoadFromFileAsync(path, 0, offset);
  601. yield return shaderABCreateRequest;
  602. AssetBundle shaderAB = shaderABCreateRequest.assetBundle;
  603. if (shaderAB != null)
  604. {
  605. Bundle bundle = new Bundle(shaderAB, shaderName);
  606. mBundlesLoaded.Add(shaderName, bundle);
  607. }
  608. Shader.WarmupAllShaders();
  609. if (completeCB != null) completeCB(true);
  610. }
  611. public void AddResidentAsset(string assetName)
  612. {
  613. mResidentAssetBundleList.Add(assetName);
  614. }
  615. private string GetABName(string assetName, Type type)
  616. {
  617. string abName = null;
  618. if (!FileUtils.HasExtension(assetName))
  619. {
  620. assetName += GetFileTypeExtention(type);
  621. }
  622. mAssetsMappingDict.TryGetValue(assetName.ToLower(), out abName);
  623. return abName;
  624. }
  625. private string GetFileTypeExtention(Type type)
  626. {
  627. string ext = "";
  628. if (type.Equals(typeof(List<GameObject>)) || type.Equals(typeof(GameObject)))
  629. {
  630. ext = ".prefab";
  631. }
  632. else if (type.Equals(typeof(List<TextAsset>)))
  633. {
  634. ext = ".bytes";
  635. }
  636. else if (type.Equals(typeof(List<AudioClip>)) || type.Equals(typeof(AudioClip)))
  637. {
  638. ext = ".ogg";
  639. }else if(type.Equals(typeof(List<VideoClip>)) || type.Equals(typeof(VideoClip)))
  640. {
  641. ext = ".mp4";
  642. }
  643. else if (type.Equals(typeof(List<Texture2D>)) || type.Equals(typeof(Texture2D)))
  644. {
  645. ext = ".png";
  646. }
  647. else if (type.Equals(typeof(List<PlayableAsset>)) || type.Equals(typeof(PlayableAsset)))
  648. {
  649. ext = ".playable";
  650. }else if(type.Equals(typeof(List<Material>)) || type.Equals(typeof(Material)))
  651. {
  652. ext = ".mat";
  653. }else if(type.Equals(typeof(List<Sprite>)) || type.Equals(typeof(Sprite)))
  654. {
  655. ext = ".png";
  656. }
  657. else if(type.Equals(typeof(List<RuntimeAnimatorController>)) || type.Equals(typeof(RuntimeAnimatorController)))
  658. {
  659. ext = ".controller";
  660. }
  661. return ext;
  662. }
  663. protected override void Dispose()
  664. {
  665. if (mBundlesLoaded != null)
  666. {
  667. foreach (var item in mBundlesLoaded)
  668. {
  669. if (item.Value != null)
  670. item.Value.Unload();
  671. }
  672. mBundlesLoaded.Clear();
  673. }
  674. mBundlesLoading = null;
  675. mBundlesLoaded = null;
  676. if (mLoadTasks != null)
  677. mLoadTasks.Clear();
  678. if (expired != null)
  679. expired.Clear();
  680. StopAllCoroutines();
  681. base.Dispose();
  682. }
  683. void GetAssetBundleManifest(string fileName, ref AssetBundleManifest maniFest)
  684. {
  685. if (null == assetBundleManifest)
  686. {
  687. string path = string.Format("{0}{1}", FileSystem.LocalDocumentPath, fileName);
  688. //Debug.Log(path);
  689. AssetBundle ab = null;
  690. if (FileSystem.Exists(path))
  691. {
  692. ab = AssetBundle.LoadFromFile(path);
  693. }
  694. else
  695. {
  696. path = string.Format("{0}{1}", FileSystem.LocalPackagePath, fileName);
  697. ab = AssetBundle.LoadFromFile(path);
  698. }
  699. //DebugHelper.Log(path);
  700. if (null != ab)
  701. maniFest = (AssetBundleManifest)ab.LoadAsset("AssetBundleManifest");
  702. }
  703. }
  704. IEnumerator GetAssetsMapping()
  705. {
  706. string path = string.Format(GetAssetsMappingName(), FileSystem.LocalDocumentPath);
  707. if (!FileSystem.Exists(path))
  708. {
  709. path = string.Format(GetAssetsMappingName(), FileSystem.LocalPackagePath);
  710. }
  711. if(!path.Contains("file://"))
  712. {
  713. path = "file://" + path;
  714. }
  715. WWW www = new WWW(path);
  716. yield return www;
  717. if (string.IsNullOrEmpty(www.error))
  718. {
  719. //DebugHelper.LogError("GetAssetsMapping-------------");
  720. string[] ContentLines = www.text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  721. //Debug.LogError(www.text);
  722. for (int idx = 0; idx < ContentLines.Length; idx++)
  723. {
  724. string content = ContentLines[idx];
  725. string[] temp = content.Split(',');
  726. mAssetsMappingDict.Add(temp[0].ToLower(), temp[1]);
  727. }
  728. }
  729. else
  730. {
  731. DebugHelper.LogError("Load AssetsMapping Fail:" + www.error+" filePath:"+ path);
  732. }
  733. }
  734. public IEnumerator LoadSubSceneLocalAssetData(string levelName)
  735. {
  736. AsyncOperation ao = SceneManager.LoadSceneAsync(levelName, LoadSceneMode.Additive);
  737. yield return ao;
  738. if (!ao.isDone)
  739. {
  740. DebugHelper.LogError("Load Scene " + levelName + " Failed");
  741. }
  742. ao = null;
  743. }
  744. public IEnumerator LoadSubSceneFromAssetBundle(string levelName)
  745. {
  746. if (!mBundlesLoaded.ContainsKey(levelName))
  747. {
  748. AssetBundleCreateRequest abRequest = null;
  749. string sceneABName = AssetsObscureUtil.GetABFileName(levelName.ToLower() + ".unity3d");
  750. ulong offset = AssetsObscureUtil.GetABOffset(sceneABName);
  751. //加载场景依赖包
  752. string[] deps = assetBundleManifest.GetAllDependencies(sceneABName);
  753. if (deps.Length > 0)
  754. {
  755. for (int idx = 0; idx < deps.Length; idx++)
  756. {
  757. string depABName = deps[idx];
  758. if (mBundlesLoaded.ContainsKey(depABName)) continue;
  759. if (mCurLoadingBundleDic.ContainsKey(depABName))
  760. {
  761. continue;
  762. }
  763. string path = string.Format("{0}{1}", FileSystem.LocalDocumentPath, depABName);
  764. ulong depOffset = AssetsObscureUtil.GetABOffset(depABName);
  765. if (FileSystem.Exists(path))
  766. {
  767. abRequest = AssetBundle.LoadFromFileAsync(path, 0, depOffset);
  768. }
  769. else
  770. {
  771. path = string.Format("{0}{1}", FileSystem.LocalPackagePath, depABName);
  772. abRequest = AssetBundle.LoadFromFileAsync(path, 0, depOffset);
  773. }
  774. syncReqDict.Add(depABName, abRequest);
  775. mCurLoadingBundleDic.Add(depABName, new LoadTask(depABName));
  776. }
  777. foreach (var p in syncReqDict)
  778. {
  779. AssetBundleCreateRequest req = p.Value;
  780. yield return req;
  781. Bundle depBundle = new Bundle(req.assetBundle, p.Key);
  782. mBundlesLoaded.Add(p.Key, depBundle);
  783. mCurLoadingBundleDic.Remove(p.Key);
  784. }
  785. syncReqDict.Clear();
  786. }
  787. //加载场景ab
  788. string sceneABPath = FileSystem.LocalDocumentPath + sceneABName;
  789. if (FileSystem.Exists(sceneABPath))
  790. {
  791. abRequest = AssetBundle.LoadFromFileAsync(sceneABPath, 0, offset);
  792. }
  793. else
  794. {
  795. sceneABPath = FileSystem.LocalPackagePath + sceneABName;
  796. abRequest = AssetBundle.LoadFromFileAsync(sceneABPath, 0, offset);
  797. }
  798. yield return abRequest;
  799. Bundle bundle = new Bundle(abRequest.assetBundle, levelName);
  800. mBundlesLoaded.Add(levelName, bundle);
  801. }
  802. else
  803. {
  804. DebugHelper.Log("[LoadSceneAssetbundle].mBundlesLoaded.ContainsKey {0}", levelName);
  805. mBundlesLoaded[levelName].LastUsedTime = Time.realtimeSinceStartup;
  806. }
  807. yield return 1;
  808. AsyncOperation ao = SceneManager.LoadSceneAsync(levelName, LoadSceneMode.Additive);
  809. yield return ao;
  810. ao = null;
  811. }
  812. public IEnumerator LoadSceneLocalAssetData(string levelName)
  813. {
  814. AsyncOperation ao = SceneManager.LoadSceneAsync(levelName, LoadSceneMode.Single);
  815. yield return ao;
  816. if (!ao.isDone)
  817. {
  818. DebugHelper.LogError("Load Scene " + levelName + " Failed");
  819. }
  820. ao = null;
  821. }
  822. Dictionary<string, AssetBundleCreateRequest> syncReqDict = new Dictionary<string, AssetBundleCreateRequest>();
  823. public IEnumerator LoadSceneAssetbundle(string levelName)
  824. {
  825. if (!mBundlesLoaded.ContainsKey(levelName))
  826. {
  827. AssetBundleCreateRequest abRequest = null;
  828. string sceneABName = AssetsObscureUtil.GetABFileName(levelName.ToLower() + ".unity3d");
  829. ulong offset = AssetsObscureUtil.GetABOffset(sceneABName);
  830. //加载场景依赖包
  831. string[] deps = assetBundleManifest.GetAllDependencies(sceneABName);
  832. if (deps.Length > 0)
  833. {
  834. for (int idx = 0; idx < deps.Length; idx++)
  835. {
  836. string depABName = deps[idx];
  837. if (mBundlesLoaded.ContainsKey(depABName)) continue;
  838. if (mCurLoadingBundleDic.ContainsKey(depABName))
  839. {
  840. continue;
  841. }
  842. string path = FileSystem.LocalDocumentPath + depABName;
  843. ulong depOffset = AssetsObscureUtil.GetABOffset(depABName);
  844. if (FileSystem.Exists(path))
  845. {
  846. abRequest = AssetBundle.LoadFromFileAsync(path, 0, depOffset);
  847. }
  848. else
  849. {
  850. path = FileSystem.LocalPackagePath + depABName;
  851. abRequest = AssetBundle.LoadFromFileAsync(path, 0, depOffset);
  852. }
  853. syncReqDict.Add(depABName, abRequest);
  854. mCurLoadingBundleDic.Add(depABName, new LoadTask(depABName));
  855. }
  856. foreach (var p in syncReqDict)
  857. {
  858. AssetBundleCreateRequest req = p.Value;
  859. yield return req;
  860. Bundle depBundle = new Bundle(req.assetBundle, p.Key);
  861. mBundlesLoaded.Add(p.Key, depBundle);
  862. mCurLoadingBundleDic.Remove(p.Key);
  863. }
  864. syncReqDict.Clear();
  865. }
  866. //加载场景ab
  867. string sceneABPath = FileSystem.LocalDocumentPath + sceneABName;
  868. if (FileSystem.Exists(sceneABPath))
  869. {
  870. abRequest = AssetBundle.LoadFromFileAsync(sceneABPath, 0, offset);
  871. }
  872. else
  873. {
  874. sceneABPath = FileSystem.LocalPackagePath + sceneABName;
  875. abRequest = AssetBundle.LoadFromFileAsync(sceneABPath, 0, offset);
  876. }
  877. yield return abRequest;
  878. Bundle bundle = new Bundle(abRequest.assetBundle, levelName);
  879. mBundlesLoaded.Add(levelName, bundle);
  880. }
  881. else
  882. {
  883. DebugHelper.Log("[LoadSceneAssetbundle].mBundlesLoaded.ContainsKey {0}", levelName);
  884. mBundlesLoaded[levelName].LastUsedTime = Time.realtimeSinceStartup;
  885. }
  886. yield return 1;
  887. AsyncOperation ao = SceneManager.LoadSceneAsync(levelName, LoadSceneMode.Single);
  888. yield return ao;
  889. ao = null;
  890. }
  891. public Shader FindShader(string shaderName, string shaderPath)
  892. {
  893. string shaderABName = AssetsObscureUtil.GetABFileName("shader.unity3d");
  894. #if UNITY_EDITOR
  895. if (!Constants.AssetbundleMode) //editor mode LocalModeOrAssetbundleMode; Local;
  896. {
  897. return Shader.Find(shaderName);
  898. }
  899. else //TestMode with local assetBundle mode;
  900. {
  901. if (!mBundlesLoaded.ContainsKey(shaderABName))
  902. return null;
  903. AssetBundle bundle = mBundlesLoaded[shaderABName].Asset;
  904. if(bundle!=null)
  905. {
  906. Shader shader = bundle.LoadAsset<Shader>(shaderPath + ".shader");
  907. return shader;
  908. }
  909. return null;
  910. }
  911. #else
  912. if (!mBundlesLoaded.ContainsKey(shaderABName))
  913. return null;
  914. AssetBundle bundle = mBundlesLoaded[shaderABName].Asset;
  915. if(bundle!=null)
  916. {
  917. Shader shader = bundle.LoadAsset<Shader>(shaderPath + ".shader");
  918. return shader;
  919. }
  920. return null;
  921. #endif
  922. }
  923. public GameObject GetAssetFromResource(string assetName)
  924. {
  925. GameObject asset = Resources.Load<GameObject>(assetName);
  926. return asset;
  927. }
  928. public T GetAssetFromResources<T>(string assetName) where T : UnityEngine.Object
  929. {
  930. T asset = Resources.Load<T>(assetName);
  931. return asset;
  932. }
  933. public int GetAsset<T>(AssetCallbackWithParams<T> callback, ELoadType type, long seqId, string pathName, params string[] assetName)
  934. {
  935. //Debug.Log($"pathName = [{pathName}] assetName = [{assetName}]");
  936. #if UNITY_EDITOR
  937. if (!Application.isPlaying)//editor mode Not Playing;
  938. {
  939. StartCoroutine(LoadFromLocalAssetData<T>(callback, seqId, pathName, assetName));
  940. return 1;
  941. }
  942. else if (!Constants.AssetbundleMode) //editor mode LocalModeOrAssetbundleMode; Local;
  943. {
  944. StartCoroutine(LoadFromLocalAssetData<T>(callback, seqId, pathName, assetName));
  945. return 1;
  946. }
  947. else //TestMode with local assetBundle mode;
  948. {
  949. return LoadFromAssetbundle<T>(callback, type, seqId, pathName, assetName);
  950. }
  951. #else
  952. return LoadFromAssetbundle<T>(callback, type, seqId, pathName, assetName);
  953. #endif
  954. }
  955. public void CancelLoadAsset(long seqId)
  956. {
  957. foreach (var p in mBundlesLoading)
  958. {
  959. LoadTask task = p.Value;
  960. if (task != null && task.IsExist(seqId))
  961. {
  962. task.RemoveTask(seqId);
  963. if (task.AssetsTaskCnt == 0)
  964. {
  965. mBundlesLoading.Remove(task.BundleName);
  966. mLoadTasks.Remove(task);
  967. }
  968. return;
  969. }
  970. }
  971. }
  972. IEnumerator LoadFromLocalAssetData<T>(AssetCallbackWithParams<T> callback, long seqId, string pathName, params string[] assetName)
  973. {
  974. #if UNITY_EDITOR
  975. if (typeof(T).Equals(typeof(AssetBundle)))
  976. {
  977. DebugHelper.LogError("LoadFromLocalAssetData 不能加载类型是AssetBundle的资源");
  978. yield break;
  979. }
  980. if (typeof(T).Equals(typeof(List<GameObject>)))
  981. {
  982. List<GameObject> gos = new List<GameObject>();
  983. if (assetName.Length <= 0)
  984. {
  985. List<string> files = FileSystem.getAllFilesPath(pathName);
  986. for (int i = 0; i < files.Count; ++i)
  987. {
  988. if (files[i] != "")
  989. {
  990. GameObject g = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(files[i]);
  991. gos.Add(g);
  992. }
  993. }
  994. }
  995. else
  996. {
  997. string path = "";
  998. for (int i = 0; i < assetName.Length; ++i)
  999. {
  1000. if (!string.IsNullOrEmpty(assetName[i]))
  1001. {
  1002. if (!string.IsNullOrEmpty(pathName))
  1003. {
  1004. path = string.Format("{0}/{1}.prefab", pathName, assetName[i]);
  1005. }
  1006. else
  1007. {
  1008. path = string.Format("{0}.prefab", assetName[i]);
  1009. }
  1010. GameObject g = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(path);
  1011. gos.Add(g);
  1012. }
  1013. }
  1014. }
  1015. if(Application.isPlaying)
  1016. yield return new WaitForEndOfFrame();
  1017. callback.DynamicInvoke(gos, seqId, pathName, assetName);
  1018. }
  1019. else if (typeof(T).Equals(typeof(List<Texture2D>)))
  1020. {
  1021. List<Texture2D> gos = new List<Texture2D>();
  1022. string path = "";
  1023. for (int i = 0; i < assetName.Length; ++i)
  1024. {
  1025. if (!string.IsNullOrEmpty(assetName[i]))
  1026. {
  1027. if(!string.IsNullOrEmpty(pathName))
  1028. {
  1029. path = string.Format("{0}/{1}.png", pathName, assetName[i]);
  1030. }
  1031. else
  1032. {
  1033. path = string.Format("{0}.png", assetName[i]);
  1034. }
  1035. Texture2D g = UnityEditor.AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  1036. gos.Add(g);
  1037. }
  1038. }
  1039. if (Application.isPlaying)
  1040. yield return new WaitForEndOfFrame();
  1041. callback.DynamicInvoke(gos, seqId, pathName, assetName);
  1042. }
  1043. else if (typeof(T).Equals(typeof(List<Sprite>)))
  1044. {
  1045. List<Sprite> gos = new List<Sprite>();
  1046. string path = "";
  1047. for (int i = 0; i < assetName.Length; ++i)
  1048. {
  1049. if (!string.IsNullOrEmpty(assetName[i]))
  1050. {
  1051. if (!string.IsNullOrEmpty(pathName))
  1052. {
  1053. path = string.Format("{0}/{1}.png", pathName, assetName[i]);
  1054. }
  1055. else
  1056. {
  1057. path = string.Format("{0}.png", assetName[i]);
  1058. }
  1059. Sprite sp = UnityEditor.AssetDatabase.LoadAssetAtPath<Sprite>(path);
  1060. gos.Add(sp);
  1061. }
  1062. }
  1063. if (Application.isPlaying)
  1064. yield return new WaitForEndOfFrame();
  1065. callback.DynamicInvoke(gos, seqId, pathName, assetName);
  1066. }
  1067. else if (typeof(T).Equals(typeof(List<TextAsset>)))
  1068. {
  1069. List<TextAsset> gos = new List<TextAsset>();
  1070. if (assetName.Length <= 0)
  1071. {
  1072. List<string> files = FileSystem.getAllFilesPath(pathName);
  1073. for (int i = 0; i < files.Count; ++i)
  1074. {
  1075. if (files[i] != "")
  1076. {
  1077. TextAsset g = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(files[i]);
  1078. gos.Add(g);
  1079. }
  1080. }
  1081. }
  1082. else
  1083. {
  1084. if (assetName[0] != "")
  1085. {
  1086. string path = "";
  1087. for (int i = 0; i < assetName.Length; ++i)
  1088. {
  1089. if (assetName[i] != "")
  1090. {
  1091. if(!string.IsNullOrEmpty(pathName))
  1092. path = string.Format("{0}/{1}", pathName, assetName[i]);
  1093. else
  1094. path = string.Format("{0}", assetName[i]);
  1095. TextAsset g = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(path);
  1096. if (g != null)
  1097. gos.Add(g);
  1098. }
  1099. }
  1100. }
  1101. }
  1102. if (Application.isPlaying)
  1103. yield return new WaitForEndOfFrame();
  1104. callback.DynamicInvoke(gos, seqId, pathName, assetName);
  1105. }
  1106. else if (typeof(T).Equals(typeof(List<PlayableAsset>)))
  1107. {
  1108. List<PlayableAsset> gos = new List<PlayableAsset>();
  1109. string path = "";
  1110. if (assetName.Length > 0)
  1111. {
  1112. for (int i = 0; i < assetName.Length; ++i)
  1113. {
  1114. if (assetName[i] != "")
  1115. {
  1116. if(!string.IsNullOrEmpty(pathName))
  1117. path = string.Format("{0}/{1}.playable", pathName, assetName[i]);
  1118. else
  1119. path = string.Format("{0}.playable", assetName[i]);
  1120. PlayableAsset g = UnityEditor.AssetDatabase.LoadAssetAtPath<PlayableAsset>(path);
  1121. gos.Add(g);
  1122. }
  1123. }
  1124. }
  1125. else
  1126. {
  1127. string[] paths = Directory.GetFiles(pathName);
  1128. foreach (string subPath in Directory.GetFiles(pathName))
  1129. {
  1130. path = subPath.Replace('\\', '/');
  1131. string ext = Path.GetExtension(path);
  1132. if ((ext != ".meta") && (ext != ".unity"))
  1133. {
  1134. PlayableAsset g = UnityEditor.AssetDatabase.LoadAssetAtPath<PlayableAsset>(path);
  1135. gos.Add(g);
  1136. }
  1137. }
  1138. }
  1139. if (Application.isPlaying)
  1140. yield return new WaitForEndOfFrame();
  1141. callback.DynamicInvoke(gos, seqId, pathName, assetName);
  1142. }
  1143. else if (typeof(T).Equals(typeof(List<RuntimeAnimatorController>)))
  1144. {
  1145. if (assetName == null || assetName.Length == 0)
  1146. {
  1147. DebugHelper.LogError("error assetName == null ");
  1148. callback.DynamicInvoke(null, seqId, pathName, assetName);
  1149. yield break;
  1150. }
  1151. List<RuntimeAnimatorController> racs = new List<RuntimeAnimatorController>();
  1152. string path = "";
  1153. for (int i = 0; i < assetName.Length; ++i)
  1154. {
  1155. if (!string.IsNullOrEmpty(assetName[i]))
  1156. {
  1157. if (!string.IsNullOrEmpty(pathName))
  1158. {
  1159. path = string.Format("{0}/{1}.controller", pathName, assetName[i]);
  1160. }
  1161. else
  1162. {
  1163. path = string.Format("{0}.controller", assetName[i]);
  1164. }
  1165. RuntimeAnimatorController rac = UnityEditor.AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(path);
  1166. racs.Add(rac);
  1167. }
  1168. }
  1169. if (Application.isPlaying)
  1170. yield return new WaitForEndOfFrame();
  1171. callback.DynamicInvoke(racs, seqId, pathName, assetName);
  1172. }
  1173. else if (typeof(T).Equals(typeof(List<AudioClip>)))
  1174. {
  1175. List<AudioClip> gos = new List<AudioClip>();
  1176. for (int i = 0; i < assetName.Length; ++i)
  1177. {
  1178. if (!string.IsNullOrEmpty(assetName[i]))
  1179. {
  1180. string path = string.Format("{0}.ogg", pathName);
  1181. if (!path.Contains(assetName[i]))
  1182. {
  1183. if (!string.IsNullOrEmpty(pathName))
  1184. path = string.Format("{0}/{1}.ogg", pathName, assetName[i]);
  1185. else
  1186. path = string.Format("{0}.ogg", assetName[i]);
  1187. }
  1188. AudioClip ac = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>(path);
  1189. gos.Add(ac);
  1190. }
  1191. }
  1192. if (Application.isPlaying)
  1193. yield return new WaitForEndOfFrame();
  1194. callback.DynamicInvoke(gos, seqId, pathName, assetName);
  1195. }
  1196. else if (typeof(T).IsSubclassOf(typeof(ScriptableObject)))
  1197. {
  1198. string path = pathName;
  1199. if (!path.Contains(assetName[0])) {
  1200. if(!string.IsNullOrEmpty(pathName))
  1201. path = string.Format("{0}/{1}", pathName, assetName[0]);
  1202. else
  1203. path = string.Format("{0}", assetName[0]);
  1204. }
  1205. ScriptableObject so = UnityEditor.AssetDatabase.LoadAssetAtPath<ScriptableObject>(path);
  1206. if (Application.isPlaying)
  1207. yield return new WaitForEndOfFrame();
  1208. callback.DynamicInvoke(so, pathName, assetName);
  1209. }
  1210. else if (typeof(T).Equals(typeof(GameObject)))//访问单个资源
  1211. {
  1212. if (assetName.Length > 0 && !string.IsNullOrEmpty(assetName[0]))
  1213. {
  1214. string path = string.Format("{0}.prefab", pathName);
  1215. if (!File.Exists(path)) {
  1216. if(!string.IsNullOrEmpty(pathName))
  1217. path = string.Format("{0}/{1}.prefab", pathName, assetName[0]);
  1218. else
  1219. path = string.Format("{0}.prefab", assetName[0]);
  1220. }
  1221. GameObject g = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(path);
  1222. if (Application.isPlaying)
  1223. yield return new WaitForEndOfFrame();
  1224. callback.DynamicInvoke(g, seqId, pathName, assetName);
  1225. }
  1226. }
  1227. else if (typeof(T).Equals(typeof(AudioClip)))//访问单个资源
  1228. {
  1229. if (assetName.Length > 0 && !string.IsNullOrEmpty(assetName[0]))
  1230. {
  1231. string path = string.Format("{0}.ogg", pathName);
  1232. if (!path.Contains(assetName[0])) {
  1233. if(!string.IsNullOrEmpty(pathName))
  1234. path = string.Format("{0}/{1}.ogg", pathName, assetName[0]);
  1235. else
  1236. path = string.Format("{0}.ogg", assetName[0]);
  1237. }
  1238. AudioClip ac = UnityEditor.AssetDatabase.LoadAssetAtPath<AudioClip>(path);
  1239. if (Application.isPlaying)
  1240. yield return new WaitForEndOfFrame();
  1241. callback.DynamicInvoke(ac, seqId, pathName, assetName);
  1242. }
  1243. }
  1244. else if (typeof(T).Equals(typeof(VideoClip)))//访问单个资源
  1245. {
  1246. if (assetName.Length > 0 && !string.IsNullOrEmpty(assetName[0]))
  1247. {
  1248. string path = string.Format("{0}.mp4", pathName);
  1249. if (!path.Contains(assetName[0])) {
  1250. if(!string.IsNullOrEmpty(pathName))
  1251. path = string.Format("{0}/{1}.mp4", pathName, assetName[0]);
  1252. else
  1253. path = string.Format("{0}.mp4", assetName[0]);
  1254. }
  1255. VideoClip vc = UnityEditor.AssetDatabase.LoadAssetAtPath<VideoClip>(path);
  1256. if (Application.isPlaying)
  1257. yield return new WaitForEndOfFrame();
  1258. callback.DynamicInvoke(vc, seqId, pathName, assetName);
  1259. }
  1260. }
  1261. else if (typeof(T).Equals(typeof(Texture2D)))//访问单个资源
  1262. {
  1263. string path = string.Format("{0}.png", pathName);
  1264. if (!pathName.Contains(assetName[0])) {
  1265. if(!string.IsNullOrEmpty(pathName))
  1266. path = string.Format("{0}/{1}.png", pathName, assetName[0]);
  1267. else
  1268. path = string.Format("{0}.png", assetName[0]);
  1269. }
  1270. Texture2D ac = UnityEditor.AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  1271. if (Application.isPlaying)
  1272. yield return new WaitForEndOfFrame();
  1273. callback.DynamicInvoke(ac, seqId, pathName, assetName);
  1274. }
  1275. else if (typeof(T).Equals(typeof(Sprite)))//访问单个资源
  1276. {
  1277. string path = string.Format("{0}.png", pathName);
  1278. if (!pathName.Contains(assetName[0])) {
  1279. if(!string.IsNullOrEmpty(pathName))
  1280. path = string.Format("{0}/{1}.png", pathName, assetName[0]);
  1281. else
  1282. path = string.Format("{0}.png", assetName[0]);
  1283. }
  1284. Sprite sp = UnityEditor.AssetDatabase.LoadAssetAtPath<Sprite>(path);
  1285. if (Application.isPlaying)
  1286. yield return new WaitForEndOfFrame();
  1287. callback.DynamicInvoke(sp, seqId, pathName, assetName);
  1288. }
  1289. else if (typeof(T).Equals(typeof(Material)))//访问单个资源
  1290. {
  1291. string path = string.Format("{0}.mat", pathName);
  1292. if (!pathName.Contains(assetName[0])) {
  1293. if(!string.IsNullOrEmpty(pathName))
  1294. path = string.Format("{0}/{1}.mat", pathName, assetName[0]);
  1295. else
  1296. path = string.Format("{0}.mat", assetName[0]);
  1297. }
  1298. Material mat = UnityEditor.AssetDatabase.LoadAssetAtPath<Material>(path);
  1299. if (Application.isPlaying)
  1300. yield return new WaitForEndOfFrame();
  1301. callback.DynamicInvoke(mat, seqId, pathName, assetName);
  1302. }
  1303. else if (typeof(T).Equals(typeof(RuntimeAnimatorController)))//访问单个资源
  1304. {
  1305. string path = string.Format("{0}.controller", pathName);
  1306. if (!pathName.Contains(assetName[0])) {
  1307. if(!string.IsNullOrEmpty(pathName))
  1308. path = string.Format("{0}/{1}.controller", pathName, assetName[0]);
  1309. else
  1310. path = string.Format("{0}.controller", assetName[0]);
  1311. }
  1312. RuntimeAnimatorController rac = UnityEditor.AssetDatabase.LoadAssetAtPath<RuntimeAnimatorController>(path);
  1313. if (Application.isPlaying)
  1314. yield return new WaitForEndOfFrame();
  1315. callback.DynamicInvoke(rac, seqId, pathName, assetName);
  1316. }
  1317. else
  1318. {
  1319. //DebugHelper.LogError("LoadFromLocalAssetData type is Error: "+ pathName);
  1320. }
  1321. #else
  1322. yield break;
  1323. #endif
  1324. }
  1325. public T LoadAssetSync<T>(string pathName, string assetName) where T : UnityEngine.Object
  1326. {
  1327. T asset = null;
  1328. #if UNITY_EDITOR
  1329. if (!Constants.AssetbundleMode)
  1330. {
  1331. if (!string.IsNullOrEmpty(assetName))
  1332. {
  1333. string path;
  1334. if (FileUtils.HasExtension(assetName))
  1335. {
  1336. path = string.Format("{0}/{1}", pathName, assetName);
  1337. }
  1338. else
  1339. {
  1340. string ext = GetFileTypeExtention(typeof(T));
  1341. path = string.Format("{0}/{1}{2}", pathName, assetName, ext);
  1342. }
  1343. asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
  1344. }
  1345. return asset;
  1346. }
  1347. #endif
  1348. if (!string.IsNullOrEmpty(assetName))
  1349. {
  1350. string abName = GetABName(pathName + "/" + assetName, typeof(T));
  1351. ulong offset = AssetsObscureUtil.GetABOffset(abName);
  1352. if (!string.IsNullOrEmpty(abName))
  1353. {
  1354. string abPath = string.Format("{0}{1} ", FileSystem.LocalDocumentPath, abName);
  1355. if (!FileSystem.Exists(abPath))
  1356. {
  1357. abPath = string.Format("{0}{1}", FileSystem.LocalPackagePath, abName);
  1358. }
  1359. AssetBundle ab = null;
  1360. if (mBundlesLoaded.ContainsKey(abName))
  1361. {
  1362. Bundle bundle = mBundlesLoaded[abName];
  1363. ab = bundle.Asset;
  1364. }
  1365. else
  1366. {
  1367. string[] deps = assetBundleManifest.GetAllDependencies(abName);
  1368. if (deps != null)
  1369. {
  1370. for (int i = 0; i < deps.Length; ++i)
  1371. {
  1372. string depabName = deps[i];
  1373. if (mBundlesLoaded.ContainsKey(depabName))
  1374. continue;
  1375. string depabPath = string.Format("{0}{1} ", FileSystem.LocalDocumentPath, depabName);
  1376. ulong depOffset = AssetsObscureUtil.GetABOffset(depabName);
  1377. if (!FileSystem.Exists(depabPath))
  1378. {
  1379. depabPath = string.Format("{0}{1}", FileSystem.LocalPackagePath, depabName);
  1380. }
  1381. AssetBundle depab = AssetBundle.LoadFromFile(depabPath, 0, depOffset);
  1382. Bundle depabBundle = new Bundle(depab, depabName);
  1383. mBundlesLoaded.Add(depabName, depabBundle);
  1384. }
  1385. }
  1386. ab = AssetBundle.LoadFromFile(abPath, 0, offset);
  1387. Bundle abBundle = new Bundle(ab, abName);
  1388. mBundlesLoaded.Add(abName, abBundle);
  1389. }
  1390. if (!FileUtils.HasExtension(assetName))
  1391. {
  1392. assetName += GetFileTypeExtention(typeof(T));
  1393. }
  1394. if (!string.IsNullOrEmpty(pathName))
  1395. assetName = pathName + "/" + assetName;
  1396. asset = ab.LoadAsset<T>(assetName);
  1397. }
  1398. else
  1399. {
  1400. DebugHelper.LogError(assetName+" 资源没有放入ab包");
  1401. }
  1402. }
  1403. return asset;
  1404. }
  1405. public Object[] LoadABSync(string pathName, params string[] assetName)
  1406. {
  1407. Object[] objs = null;
  1408. #if UNITY_EDITOR
  1409. if (!Constants.AssetbundleMode)
  1410. {
  1411. objs = new Object[assetName.Length];
  1412. for (int i = 0; i < assetName.Length; ++i)
  1413. {
  1414. if (!string.IsNullOrEmpty(assetName[i]))
  1415. {
  1416. string path = string.Format("{0}/{1}.prefab", pathName, assetName[i]);
  1417. Object g = UnityEditor.AssetDatabase.LoadAssetAtPath<Object>(path);
  1418. objs[i] = g;
  1419. }
  1420. }
  1421. return objs;
  1422. }
  1423. #endif
  1424. string abName = pathName.Replace("Assets/Content/", "");
  1425. abName = abName.Replace("/", "");
  1426. abName = abName.ToLower() + ".unity3d";
  1427. abName = AssetsObscureUtil.GetABFileName(abName);
  1428. ulong offset = AssetsObscureUtil.GetABOffset(abName);
  1429. string abPath = string.Format("{0}{1} ", FileSystem.LocalDocumentPath, abName);
  1430. if (!FileSystem.Exists(abPath))
  1431. {
  1432. abPath = string.Format("{0}{1}", FileSystem.LocalPackagePath, abName);
  1433. }
  1434. AssetBundle ab = null;
  1435. if (mBundlesLoaded.ContainsKey(abName))
  1436. {
  1437. Bundle bundle = mBundlesLoaded[abName];
  1438. ab = bundle.Asset;
  1439. }
  1440. else
  1441. {
  1442. string[] deps = assetBundleManifest.GetAllDependencies(abName);
  1443. if (deps != null)
  1444. {
  1445. for (int i = 0; i < deps.Length; ++i)
  1446. {
  1447. string depabName = deps[i];
  1448. if (mBundlesLoaded.ContainsKey(depabName))
  1449. continue;
  1450. string depabPath = string.Format("{0}{1} ", FileSystem.LocalDocumentPath, depabName);
  1451. ulong depOffset = AssetsObscureUtil.GetABOffset(depabName);
  1452. if (!FileSystem.Exists(depabPath))
  1453. {
  1454. depabPath = string.Format("{0}{1}", FileSystem.LocalPackagePath, depabName);
  1455. }
  1456. AssetBundle depab = AssetBundle.LoadFromFile(depabPath, 0, depOffset);
  1457. Bundle depabBundle = new Bundle(depab, depabName);
  1458. mBundlesLoaded.Add(depabName, depabBundle);
  1459. }
  1460. }
  1461. ab = AssetBundle.LoadFromFile(abPath, 0, offset);
  1462. Bundle abBundle = new Bundle(ab, abName);
  1463. mBundlesLoaded.Add(abName, abBundle);
  1464. }
  1465. if (assetName == null || assetName.Length == 0)
  1466. {
  1467. objs = ab.LoadAllAssets();
  1468. }
  1469. else
  1470. {
  1471. objs = new Object[assetName.Length];
  1472. for (int i = 0; i < assetName.Length; i++)
  1473. {
  1474. objs[i] = ab.LoadAsset(assetName[i]);
  1475. }
  1476. }
  1477. return objs;
  1478. }
  1479. Dictionary<string, List<string>> tempDic = new Dictionary<string, List<string>>();
  1480. int LoadFromAssetbundle<T>(AssetCallbackWithParams<T> callback, ELoadType loadType, long seqId, string pathName, params string[] assetName)
  1481. {
  1482. string abName = null;
  1483. if (assetName == null || assetName.Length == 0)
  1484. {
  1485. abName = pathName.Replace("Assets/Content/", "");
  1486. abName = abName.Replace("/", "").ToLower();
  1487. if (!abName.Contains(".unity3d"))
  1488. {
  1489. abName = abName + ".unity3d";
  1490. }
  1491. abName = AssetsObscureUtil.GetABFileName(abName);
  1492. AddLoadTask<T>(seqId, abName,pathName,callback, loadType, assetName);
  1493. return 1;
  1494. }
  1495. else
  1496. {
  1497. tempDic.Clear();
  1498. for (int idx = 0; idx < assetName.Length; idx++)
  1499. {
  1500. string fullPath = assetName[idx];
  1501. if(!string.IsNullOrEmpty(pathName))
  1502. {
  1503. fullPath = pathName + "/" + assetName[idx];
  1504. }
  1505. string tempAB = GetABName(fullPath, typeof(T));
  1506. if(!string.IsNullOrEmpty(tempAB))
  1507. {
  1508. List<string> assetList = null;
  1509. if (!tempDic.TryGetValue(tempAB,out assetList))
  1510. {
  1511. assetList = new List<string>();
  1512. tempDic.Add(tempAB, assetList);
  1513. }
  1514. assetList.Add(assetName[idx]);
  1515. }
  1516. else
  1517. {
  1518. DebugHelper.LogError(string.Format("{0} 没有打入ab包中", fullPath));
  1519. }
  1520. }
  1521. if(tempDic.Count > 0)
  1522. {
  1523. foreach (var p in tempDic)
  1524. {
  1525. AddLoadTask<T>(seqId, p.Key, pathName, callback, loadType, p.Value.ToArray());
  1526. }
  1527. }
  1528. return tempDic.Count;
  1529. }
  1530. }
  1531. void AddLoadTask<T>(long seqId, string abName,string pathName, AssetCallbackWithParams<T> callback, ELoadType loadType, params string[] assetName)
  1532. {
  1533. LoadTask task = new LoadTask(seqId, abName, pathName, assetName, callback as Delegate, true, typeof(T), loadType);
  1534. if (!mBundlesLoaded.ContainsKey(task.BundleName))
  1535. {
  1536. if(mCurLoadingBundleDic.ContainsKey(task.BundleName))
  1537. {
  1538. LoadTask task1 = mCurLoadingBundleDic[task.BundleName];
  1539. task1.ChangeTask(seqId, abName, pathName, assetName, callback as Delegate, true, typeof(T), loadType);
  1540. }
  1541. else
  1542. {
  1543. if (mBundlesLoading.ContainsKey(task.BundleName))
  1544. {
  1545. mBundlesLoading[task.BundleName].AssetsTask.AddRange(task.AssetsTask);
  1546. return;
  1547. }
  1548. else
  1549. {
  1550. if (mCurLoadingBundleDic.ContainsKey(task.BundleName))
  1551. {
  1552. DebugHelper.LogError("mCurLoadingBundleDic 包函数了:" + task.BundleName);
  1553. }
  1554. mBundlesLoading.Add(task.BundleName, task);
  1555. }
  1556. mLoadTasks.Add(task);
  1557. }
  1558. }
  1559. else
  1560. {
  1561. if (mBundlesLoaded.ContainsKey(task.BundleName))
  1562. {
  1563. mBundlesLoaded[task.BundleName].Priority = 0;
  1564. }
  1565. string[] deps = assetBundleManifest.GetAllDependencies(task.BundleName);
  1566. if (deps.Length > 0)
  1567. {
  1568. string depName = string.Empty;
  1569. for (int i = 0; i < deps.Length; ++i)
  1570. {
  1571. depName = deps[i];
  1572. if (mBundlesLoaded.ContainsKey(depName))
  1573. {
  1574. mBundlesLoaded[depName].Priority = 0;
  1575. }
  1576. }
  1577. }
  1578. mLoadFinshedTask.Enqueue(task);
  1579. }
  1580. }
  1581. void Update()
  1582. {
  1583. while (mLoadFinshedTask.Count > 0)
  1584. {
  1585. LoadTask task = mLoadFinshedTask.Dequeue();
  1586. if (mBundlesLoaded.ContainsKey(task.BundleName))
  1587. {
  1588. AsyncLoad(task);
  1589. }
  1590. }
  1591. while (mLoadingCoroutineCount < MaxLoadingCoroutineCount && mLoadTasks.Count > 0)
  1592. {
  1593. LoadTask task = mLoadTasks[0];
  1594. task.LoadStartTime = Time.realtimeSinceStartup;
  1595. LoadAssetTask(task);
  1596. mLoadTasks.RemoveAt(0);
  1597. }
  1598. for (int i = 0; i < mAssetbundleLoadTasks.Count; ++i)
  1599. {
  1600. mAssetbundleLoadTasks[i].Update();
  1601. }
  1602. }
  1603. #region inner_methods
  1604. public AssetBundleCreateRequest CreatePackageLoader(string bundleName)
  1605. {
  1606. AssetBundleCreateRequest abRequest = null;
  1607. try
  1608. {
  1609. string path = string.Format("{0}{1}", FileSystem.LocalDocumentPath, bundleName);
  1610. ulong offset = AssetsObscureUtil.GetABOffset(bundleName);
  1611. if (FileSystem.Exists(path))
  1612. {
  1613. abRequest = AssetBundle.LoadFromFileAsync(path, 0, offset);
  1614. }
  1615. else
  1616. {
  1617. path = string.Format("{0}{1}", FileSystem.LocalPackagePath, bundleName);
  1618. abRequest = AssetBundle.LoadFromFileAsync(path, 0, offset);
  1619. }
  1620. }
  1621. catch (Exception e)
  1622. {
  1623. DebugHelper.LogError("Load AB: " + e.ToString());
  1624. return null;
  1625. }
  1626. return abRequest;
  1627. }
  1628. List<string> temp = new List<string>();
  1629. public void NotifyAssetbundleLoaded(LoadTask task, LoadAssetbundleTask abTask)
  1630. {
  1631. //DebugHelper.LogError("[bhy] loadingAb Finish: " + task.BundleName + " IsDependencyDone:"+task.IsDependency);
  1632. #if PROFILE
  1633. UnityEngine.Profiling.Profiler.BeginSample("Loaded" + task.BundleName);
  1634. #endif
  1635. mAssetbundleLoadTasks.Remove(abTask);
  1636. if (mCurLoadingBundleDic.ContainsKey(task.BundleName))
  1637. {
  1638. mCurLoadingBundleDic.Remove(task.BundleName);
  1639. }
  1640. mBundlesLoaded.Add(task.BundleName, new Bundle(task.Bundle, task.BundleName));
  1641. for (int i = 0; i < mAssetbundleLoadTasks.Count; ++i)
  1642. {
  1643. if (!mAssetbundleLoadTasks[i].Loader.IsDependencyDone)
  1644. {
  1645. CheckDependencyOnAssetbundleLoaded(mAssetbundleLoadTasks[i].Loader);
  1646. }
  1647. }
  1648. if (task.LoadType != ELoadType.AssetBundle && task.IsDependencyDone)
  1649. {
  1650. LoadAssetTaskFinish(task);
  1651. }else if(task.LoadType == ELoadType.AssetBundle && task.IsDependencyDone)
  1652. {
  1653. if(mBundlesLoading.ContainsKey(task.BundleName))
  1654. {
  1655. LoadTask loadingTask = mBundlesLoading[task.BundleName];
  1656. if(loadingTask.IsDependencyDone)
  1657. {
  1658. loadingTask.Bundle = task.Bundle;
  1659. LoadAssetTaskFinish(loadingTask);
  1660. }
  1661. }
  1662. }
  1663. temp.Clear();
  1664. foreach (var p in mBundlesLoading)
  1665. {
  1666. if (p.Value.IsDependency)
  1667. {
  1668. if (CheckDependencyPackageInfo(p.Value))
  1669. {
  1670. temp.Add(p.Key);
  1671. }
  1672. }
  1673. }
  1674. for (int idx = 0; idx < temp.Count; idx++)
  1675. {
  1676. mBundlesLoading.Remove(temp[idx]);
  1677. }
  1678. #if PROFILE
  1679. UnityEngine.Profiling.Profiler.EndSample();
  1680. #endif
  1681. }
  1682. void LoadAssetTask(LoadTask task)
  1683. {
  1684. mLoadingCoroutineCount++;
  1685. if (CheckDependencyPackageInfo(task))
  1686. {
  1687. mBundlesLoading.Remove(task.BundleName);
  1688. }
  1689. }
  1690. void LoadAssetTaskFinish(LoadTask task)
  1691. {
  1692. mLoadingCoroutineCount--;
  1693. mLoadFinshedTask.Enqueue(task);
  1694. mBundlesLoading.Remove(task.BundleName);
  1695. }
  1696. bool CheckDependencyPackageInfo(LoadTask task)
  1697. {
  1698. string bundleName = task.BundleName;
  1699. string[] deps = assetBundleManifest.GetAllDependencies(bundleName);
  1700. if (deps == null || deps.Length == 0)
  1701. {
  1702. task.IsDependency = false;
  1703. task.IsDependencyDone = true;
  1704. }
  1705. else
  1706. {
  1707. task.IsDependency = true;
  1708. task.IsDependencyDone = true;
  1709. for (int i = 0; i < deps.Length; ++i)
  1710. {
  1711. string depPackage = deps[i];
  1712. if (mBundlesLoaded.ContainsKey(depPackage))
  1713. continue;
  1714. task.IsDependencyDone = false;
  1715. NotifyLoadAssetbundle(depPackage);
  1716. }
  1717. }
  1718. if (task.IsDependencyDone)
  1719. {
  1720. if (mBundlesLoaded.ContainsKey(task.BundleName))
  1721. {
  1722. mLoadingCoroutineCount--;
  1723. mLoadFinshedTask.Enqueue(task);
  1724. return true;
  1725. }
  1726. else
  1727. {
  1728. NotifyLoadAssetbundle(task.BundleName, task);
  1729. }
  1730. }
  1731. return false;
  1732. }
  1733. void NotifyLoadAssetbundle(string bundleName, LoadTask task = null)
  1734. {
  1735. if (mBundlesLoaded.ContainsKey(bundleName) || mCurLoadingBundleDic.ContainsKey(bundleName))
  1736. {
  1737. return;
  1738. }
  1739. LoadTask t = task;
  1740. if (t == null)
  1741. {
  1742. t = new LoadTask(bundleName);
  1743. }
  1744. mCurLoadingBundleDic.Add(bundleName, t);
  1745. mAssetbundleLoadTasks.Add(new LoadAssetbundleTask(t));
  1746. }
  1747. void CheckDependencyOnAssetbundleLoaded(LoadTask task)
  1748. {
  1749. string bundleName = task.BundleName;
  1750. string[] deps = assetBundleManifest.GetAllDependencies(bundleName);
  1751. if (deps == null || deps.Length == 0)
  1752. {
  1753. task.IsDependency = false;
  1754. task.IsDependencyDone = true;
  1755. }
  1756. else
  1757. {
  1758. task.IsDependencyDone = true;
  1759. for (int i = 0; i < deps.Length; ++i)
  1760. {
  1761. string depPackage = deps[i];
  1762. if (mBundlesLoaded.ContainsKey(depPackage))
  1763. continue;
  1764. task.IsDependencyDone = false;
  1765. NotifyLoadAssetbundle(depPackage);
  1766. }
  1767. }
  1768. }
  1769. void AsyncLoad(LoadTask task)
  1770. {
  1771. Bundle bundle = mBundlesLoaded[task.BundleName];
  1772. if (bundle == null)
  1773. {
  1774. DebugHelper.LogWarning("[AssetsMgr].DoCallBack Bundle not exists for name [{0}]", task.BundleName);
  1775. return;
  1776. }
  1777. try
  1778. {
  1779. if (task.DelegateType.Equals(typeof(List<GameObject>)))
  1780. {
  1781. for (int j = 0; j < task.AssetsTask.Count; ++j)
  1782. {
  1783. LoadAssetsTask lat = task.AssetsTask[j];
  1784. StartCoroutine(AsyncMultiAssetFromAB<GameObject>(task.BundleName, bundle.Asset, lat));
  1785. }
  1786. }
  1787. else if (task.DelegateType.Equals(typeof(List<TextAsset>)))
  1788. {
  1789. for (int j = 0; j < task.AssetsTask.Count; j++)
  1790. {
  1791. LoadAssetsTask lat = task.AssetsTask[j];
  1792. StartCoroutine(AsyncMultiAssetFromAB<TextAsset>(task.BundleName, bundle.Asset, lat));
  1793. }
  1794. }
  1795. else if (task.DelegateType.Equals(typeof(List<AudioClip>)))
  1796. {
  1797. for (int j = 0; j < task.AssetsTask.Count; j++)
  1798. {
  1799. LoadAssetsTask lat = task.AssetsTask[j];
  1800. StartCoroutine(AsyncMultiAssetFromAB<AudioClip>(task.BundleName, bundle.Asset, lat));
  1801. }
  1802. }
  1803. else if (task.DelegateType.Equals(typeof(List<Texture2D>)))
  1804. {
  1805. for (int j = 0; j < task.AssetsTask.Count; j++)
  1806. {
  1807. LoadAssetsTask lat = task.AssetsTask[j];
  1808. StartCoroutine(AsyncMultiAssetFromAB<Texture2D>(task.BundleName, bundle.Asset, lat));
  1809. }
  1810. }
  1811. else if (task.DelegateType.Equals(typeof(List<Sprite>)))
  1812. {
  1813. for (int j = 0; j < task.AssetsTask.Count; j++)
  1814. {
  1815. LoadAssetsTask lat = task.AssetsTask[j];
  1816. StartCoroutine(AsyncMultiAssetFromAB<Sprite>(task.BundleName, bundle.Asset, lat));
  1817. }
  1818. }
  1819. else if (task.DelegateType.Equals(typeof(List<PlayableAsset>)))
  1820. {
  1821. for (int j = 0; j < task.AssetsTask.Count; j++)
  1822. {
  1823. LoadAssetsTask lat = task.AssetsTask[j];
  1824. StartCoroutine(AsyncMultiAssetFromAB<PlayableAsset>(task.BundleName, bundle.Asset, lat));
  1825. }
  1826. }
  1827. else if(task.DelegateType.Equals(typeof(List<RuntimeAnimatorController>)))
  1828. {
  1829. for (int j = 0; j < task.AssetsTask.Count; j++)
  1830. {
  1831. LoadAssetsTask lat = task.AssetsTask[j];
  1832. StartCoroutine(AsyncMultiAssetFromAB<RuntimeAnimatorController>(task.BundleName, bundle.Asset, lat));
  1833. }
  1834. }
  1835. else if (task.DelegateType.Equals(typeof(Texture2D)))
  1836. {
  1837. for (int j = 0; j < task.AssetsTask.Count; j++)
  1838. {
  1839. LoadAssetsTask lat = task.AssetsTask[j];
  1840. StartCoroutine(AsyncSingleAssetFromAB<Texture2D>(task.BundleName, bundle.Asset, lat));
  1841. }
  1842. }
  1843. else if (task.DelegateType.Equals(typeof(GameObject)))
  1844. {
  1845. for (int j = 0; j < task.AssetsTask.Count; j++)
  1846. {
  1847. LoadAssetsTask lat = task.AssetsTask[j];
  1848. StartCoroutine(AsyncSingleAssetFromAB<GameObject>(task.BundleName, bundle.Asset, lat));
  1849. }
  1850. }
  1851. else if (task.DelegateType.Equals(typeof(AudioClip)))
  1852. {
  1853. for (int j = 0; j < task.AssetsTask.Count; j++)
  1854. {
  1855. LoadAssetsTask lat = task.AssetsTask[j];
  1856. StartCoroutine(AsyncSingleAssetFromAB<AudioClip>(task.BundleName, bundle.Asset, lat));
  1857. }
  1858. }
  1859. else if (task.DelegateType.Equals(typeof(PlayableAsset)))
  1860. {
  1861. for (int j = 0; j < task.AssetsTask.Count; j++)
  1862. {
  1863. LoadAssetsTask lat = task.AssetsTask[j];
  1864. StartCoroutine(AsyncSingleAssetFromAB<PlayableAsset>(task.BundleName, bundle.Asset, lat));
  1865. }
  1866. }
  1867. else if (task.DelegateType.Equals(typeof(Material)))
  1868. {
  1869. for (int j = 0; j < task.AssetsTask.Count; j++)
  1870. {
  1871. LoadAssetsTask lat = task.AssetsTask[j];
  1872. StartCoroutine(AsyncSingleAssetFromAB<Material>(task.BundleName, bundle.Asset, lat));
  1873. }
  1874. }else if(task.DelegateType.Equals(typeof(Sprite)))
  1875. {
  1876. for (int j = 0; j < task.AssetsTask.Count; j++)
  1877. {
  1878. LoadAssetsTask lat = task.AssetsTask[j];
  1879. StartCoroutine(AsyncSingleAssetFromAB<Sprite>(task.BundleName, bundle.Asset, lat));
  1880. }
  1881. }
  1882. else if(task.DelegateType.Equals(typeof(RuntimeAnimatorController)))
  1883. {
  1884. for (int j = 0; j < task.AssetsTask.Count; j++)
  1885. {
  1886. LoadAssetsTask lat = task.AssetsTask[j];
  1887. StartCoroutine(AsyncSingleAssetFromAB<RuntimeAnimatorController>(task.BundleName, bundle.Asset, lat));
  1888. }
  1889. }
  1890. else
  1891. {
  1892. DebugHelper.LogError("[DoCallBack].cant support type {0}", task.DelegateType);
  1893. }
  1894. }
  1895. catch (Exception e)
  1896. {
  1897. DebugHelper.LogException(e);
  1898. }
  1899. finally
  1900. {
  1901. }
  1902. }
  1903. IEnumerator AsyncMultiAssetFromAB<T>(string assetBunleName, AssetBundle bundle, LoadAssetsTask assetsTask) where T : UnityEngine.Object
  1904. {
  1905. if (bundle == null)
  1906. yield break;
  1907. List<T> gos = new List<T>();
  1908. if (assetsTask.AssetName.Length > 0)
  1909. {
  1910. HashSet<string> dic = new HashSet<string>();
  1911. for (int idx = 0; idx < assetsTask.AssetName.Length; idx++)
  1912. {
  1913. string assetName = assetsTask.AssetName[idx].Trim();
  1914. if (string.IsNullOrEmpty(assetName)) continue;
  1915. if (!FileUtils.HasExtension(assetName))
  1916. {
  1917. assetName += GetFileTypeExtention(typeof(T));
  1918. }
  1919. if (dic.Contains(assetName)) continue;
  1920. string assetFilePath = assetName;
  1921. if (!string.IsNullOrEmpty(assetsTask.AssetPath))
  1922. assetFilePath = assetsTask.AssetPath + "/" + assetName;
  1923. AssetBundleRequest req = bundle.LoadAssetAsync<T>(assetFilePath);
  1924. dic.Add(assetName);
  1925. yield return req;
  1926. T g = req.asset as T;
  1927. if (g == null)
  1928. {
  1929. DebugHelper.LogError("AsyncMultiAssetFromAB has null object: " + assetName);
  1930. }
  1931. gos.Add(g);
  1932. }
  1933. }
  1934. else
  1935. {
  1936. AssetBundleRequest req = bundle.LoadAllAssetsAsync<T>();
  1937. yield return req;
  1938. var allAssets = req.allAssets;
  1939. for (int idx = 0, idxMax = allAssets.Length; idx < idxMax; idx++)
  1940. {
  1941. gos.Add(allAssets[idx] as T);
  1942. }
  1943. }
  1944. assetsTask.Invoke(gos);
  1945. }
  1946. IEnumerator AsyncSingleAssetFromAB<T>(string assetBunleName, AssetBundle bundle, LoadAssetsTask assetsTask) where T : UnityEngine.Object
  1947. {
  1948. if (bundle == null)
  1949. yield break;
  1950. if (assetsTask.AssetName.Length > 0 && !string.IsNullOrEmpty(assetsTask.AssetName[0]))
  1951. {
  1952. string name = "";
  1953. if(!string.IsNullOrEmpty(assetsTask.AssetPath))
  1954. name = assetsTask.AssetPath + "/" + assetsTask.AssetName[0] + GetFileTypeExtention(typeof(T));
  1955. else
  1956. name = assetsTask.AssetName[0] + GetFileTypeExtention(typeof(T));
  1957. AssetBundleRequest req = bundle.LoadAssetAsync<T>(name);
  1958. yield return req;
  1959. T g = req.asset as T;
  1960. if (g == null)
  1961. {
  1962. DebugHelper.LogError("AsyncMultiAssetFromAB has null object: " + name);
  1963. }
  1964. assetsTask.Invoke(g);
  1965. }
  1966. }
  1967. #endregion
  1968. }