AssetsMgr.cs 69 KB

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