AssetsMgr.cs 69 KB

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