ResourceMgr.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using UnityEngine.Playables;
  5. using UnityEngine.Video;
  6. using LuaInterface;
  7. public delegate void ResourceLoadCallback<T>(T o, string assetPath, params string[] assetNames);
  8. public delegate void ResourceLoadCallbackWithSeqId<T>(T o, long seqId,string assetPath,object[] userdatas, params string[] assetNames);
  9. public class ResourceInfo
  10. {
  11. private string path;
  12. private string name;
  13. private Object asset;
  14. private float lastUsedTime;
  15. public string Path
  16. {
  17. get { return path; }
  18. }
  19. public string Name
  20. {
  21. get { return name; }
  22. }
  23. public Object Asset
  24. {
  25. get { return asset; }
  26. }
  27. public float LastUsedTime
  28. {
  29. get { return lastUsedTime; }
  30. set
  31. {
  32. lastUsedTime = value;
  33. }
  34. }
  35. public ResourceInfo(string path_, string name_, Object asset_)
  36. {
  37. path = path_;
  38. name = name_;
  39. asset = asset_;
  40. lastUsedTime = Time.time;
  41. }
  42. public Object Reuse()
  43. {
  44. lastUsedTime = Time.time;
  45. return asset;
  46. }
  47. public void Dispose()
  48. {
  49. asset = null;
  50. }
  51. }
  52. public class LoadingResourceInfo
  53. {
  54. System.Type delegateType;
  55. public System.Type DelegateType
  56. {
  57. get { return delegateType; }
  58. }
  59. System.Delegate callback;
  60. public System.Delegate Callback
  61. {
  62. get { return callback; }
  63. }
  64. string assetPath;
  65. public string AssetPath
  66. {
  67. get { return assetPath; }
  68. }
  69. string assetName;
  70. public string AssetName
  71. {
  72. get { return assetName; }
  73. }
  74. string[] assetNames;
  75. public string[] AssetNames
  76. {
  77. get { return assetNames; }
  78. }
  79. long mSeqId = 0;
  80. public long SeqId
  81. {
  82. get { return mSeqId; }
  83. }
  84. bool mReturnSeqId = false;
  85. public bool ReturnSeqId
  86. {
  87. get { return mReturnSeqId; }
  88. }
  89. int mLoadNum = 0;
  90. public int LoadCBNum
  91. {
  92. get { return mLoadNum; }
  93. set { mLoadNum = value; }
  94. }
  95. public object[] userDatas;
  96. public LoadingResourceInfo(long seqId, string path_, System.Delegate callback_, System.Type delegateType_, bool returnSeq,params string[] assetNames_)
  97. {
  98. mSeqId = seqId;
  99. assetPath = path_;
  100. callback = callback_;
  101. assetNames = assetNames_;
  102. delegateType = delegateType_;
  103. mReturnSeqId = returnSeq;
  104. }
  105. public void Dispose()
  106. {
  107. callback = null;
  108. assetNames = null;
  109. }
  110. }
  111. public class ResourceMgr : Singleton<ResourceMgr>
  112. {
  113. long loadingSeqId = 0;
  114. //对象池
  115. Dictionary<string, Dictionary<string, GameObjectPool<GameObject>> > assetsPool = new Dictionary<string, Dictionary<string, GameObjectPool<GameObject>>>();
  116. //已加载的资源列表
  117. List<ResourceInfo> loadedResources = new List<ResourceInfo>();
  118. //正在加载的资源
  119. List<LoadingResourceInfo> loadingResources = new List<LoadingResourceInfo>();
  120. const float check_internal = 60;
  121. const float cache_Time = 300;
  122. float lastCheckTime = 0;
  123. [NoToLua]
  124. public override void Init()
  125. {
  126. lastCheckTime = Time.time;
  127. }
  128. [NoToLua]
  129. public override void UnInit()
  130. {
  131. ClearAllResource();
  132. }
  133. private GameObject mPoolGo = null;
  134. public Transform PoolNode
  135. {
  136. get
  137. {
  138. if (mPoolGo == null)
  139. {
  140. mPoolGo = new GameObject("PoolRoot");
  141. if (GameMgr.Instance != null) {
  142. mPoolGo.transform.SetParent(GameMgr.Instance.transform);
  143. }
  144. mPoolGo.SetActive(false);
  145. }
  146. return mPoolGo.transform;
  147. }
  148. }
  149. #region outer_methods
  150. //已经在的资源个数
  151. [NoToLua]
  152. public int LoadedResourceCnt
  153. {
  154. get { return loadedResources.Count; }
  155. }
  156. //正在加载的资源个数
  157. [NoToLua]
  158. public int LoadingResourceCnt
  159. {
  160. get { return loadingResources.Count; }
  161. }
  162. [NoToLua]
  163. public GameObject GetGOFromResources(string assetName)
  164. {
  165. GameObject prefab = LoadObjFromResources<GameObject>(assetName);
  166. if (prefab == null) return null;
  167. //DebugHelper.LogError("AssetName " + assetName);
  168. return GameObject.Instantiate(prefab);
  169. }
  170. [NoToLua]
  171. public T LoadObjFromResources<T>(string assetName) where T : UnityEngine.Object
  172. {
  173. return AssetsMgr.Instance.GetAssetFromResources<T>(assetName);
  174. }
  175. [NoToLua]
  176. public void LoadLuaAsset(ResourceLoadCallback<List<TextAsset>> cb, string pathName)
  177. {
  178. loadingSeqId++;
  179. LoadingResourceInfo lri = new LoadingResourceInfo(loadingSeqId, pathName, cb as System.Delegate, typeof(List<TextAsset>), false, null);
  180. loadingResources.Add(lri);
  181. lri.LoadCBNum = AssetsMgr.Instance.GetAsset<List<TextAsset>>(OnLoadDirAssetallback, ELoadType.OTHER, loadingSeqId, pathName);
  182. }
  183. [NoToLua]
  184. public void LoadDirAsset<T>(ResourceLoadCallback<T> cb, string pathName, ELoadType type = ELoadType.OTHER)
  185. {
  186. if (string.IsNullOrEmpty(pathName)) return;
  187. if (ExistAssetPath(pathName))
  188. {
  189. FinishResourceLoad(cb, typeof(T), pathName, null,false,0,null);
  190. return;
  191. }
  192. loadingSeqId++;
  193. LoadingResourceInfo lri = new LoadingResourceInfo(loadingSeqId, pathName, cb as System.Delegate, typeof(T), false,null);
  194. loadingResources.Add(lri);
  195. lri.LoadCBNum = AssetsMgr.Instance.GetAsset<T>(OnLoadDirAssetallback, type, loadingSeqId, pathName);
  196. }
  197. #region methods_for_lua
  198. public long LoadAssetSprite(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<Sprite> cb, params string[] assetNames)
  199. {
  200. return LoadAsset(cb, pathName, type, null,assetNames);
  201. }
  202. public long LoadAssetSprites(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<List<Sprite>> cb, params string[] assetNames)
  203. {
  204. return LoadAsset(cb, pathName, type, null, assetNames);
  205. }
  206. public long LoadAssetSpriteWithUserData(string pathName, ELoadType type,object[] userDatas, ResourceLoadCallbackWithSeqId<Sprite> cb, params string[] assetNames)
  207. {
  208. return LoadAsset(cb, pathName, type, userDatas,assetNames);
  209. }
  210. public long LoadAssetGameObject(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<GameObject> cb, params string[] assetNames)
  211. {
  212. return LoadAsset(cb, pathName, type, null,assetNames);
  213. }
  214. public long LoadAssetGameObjectWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<GameObject> cb, params string[] assetNames)
  215. {
  216. return LoadAsset(cb, pathName, type,userDatas ,assetNames);
  217. }
  218. public long LoadAssetGameObjects(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<List<GameObject>> cb, params string[] assetNames)
  219. {
  220. return LoadAsset(cb, pathName, type,null, assetNames);
  221. }
  222. public long LoadAssetGameObjectsWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<List<GameObject>> cb, params string[] assetNames)
  223. {
  224. return LoadAsset(cb, pathName, type, userDatas, assetNames);
  225. }
  226. public long LoadAssetRuntimeAnimatorControllers(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<List<RuntimeAnimatorController>> cb, params string[] assetNames)
  227. {
  228. return LoadAsset(cb, pathName, type,null, assetNames);
  229. }
  230. public long LoadAssetRuntimeAnimatorControllersWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<List<RuntimeAnimatorController>> cb, params string[] assetNames)
  231. {
  232. return LoadAsset(cb, pathName, type, userDatas,assetNames);
  233. }
  234. public long LoadAssetMaterial(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<Material> cb, params string[] assetNames)
  235. {
  236. return LoadAsset(cb, pathName, type, null,assetNames);
  237. }
  238. public long LoadAssetMaterialWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<Material> cb, params string[] assetNames)
  239. {
  240. return LoadAsset(cb, pathName, type, userDatas, assetNames);
  241. }
  242. public long LoadAssetTexture(string pathName, ELoadType type, ResourceLoadCallbackWithSeqId<Texture2D> cb, params string[] assetNames)
  243. {
  244. return LoadAsset(cb, pathName, type, null, assetNames);
  245. }
  246. public long LoadAssetTextureWithUserData(string pathName, ELoadType type, object[] userDatas, ResourceLoadCallbackWithSeqId<Texture2D> cb, params string[] assetNames)
  247. {
  248. return LoadAsset(cb, pathName, type, userDatas, assetNames);
  249. }
  250. public long LoadAsset<T>(ResourceLoadCallbackWithSeqId<T> cb, string pathName, ELoadType type, object[] userDatas, params string[] assetNames)
  251. {
  252. return Inner_LoadAsset(cb, pathName, assetNames, type, userDatas);
  253. }
  254. public Sprite FindSpriteByPathAndName(string pathName, string assetName)
  255. {
  256. return FindAssetByPathAndName<Sprite>(pathName, assetName);
  257. }
  258. #endregion
  259. public long LoadAsset<T>(ResourceLoadCallback<T> cb, string pathName, ELoadType type, params string[] assetNames)
  260. {
  261. return Inner_LoadAsset(cb, pathName, assetNames, type);
  262. }
  263. public long LoadAsset<T>(ResourceLoadCallback<T> cb, string pathName, params string[] assetNames)
  264. {
  265. return Inner_LoadAsset(cb, pathName, assetNames, ELoadType.OTHER);
  266. }
  267. public Object[] LoadABSync(string pathName, params string[] assetNames)
  268. {
  269. return AssetsMgr.Instance.LoadABSync(pathName, assetNames);
  270. }
  271. public T LoadAssetSync<T>(string assetPath, string assetName) where T : UnityEngine.Object
  272. {
  273. T ret = null;
  274. Object obj = FindAssetByPathAndName(assetPath, assetName);
  275. if (obj == null)
  276. {
  277. ret = AssetsMgr.Instance.LoadAssetSync<T>(assetPath, assetName);
  278. ResourceInfo ri = new ResourceInfo(assetPath, assetName, ret);
  279. loadedResources.Add(ri);
  280. }
  281. else
  282. {
  283. ret = obj as T;
  284. }
  285. return ret;
  286. }
  287. /// <summary>
  288. /// 从对象池中获取对象
  289. /// </summary>
  290. /// <param name="assetsPath"></param>
  291. /// <param name="assetsName"></param>
  292. /// <param name="action"></param>
  293. public long GetGOFromPool(string assetsPath, string assetsName, System.Action<GameObject> action = null)
  294. {
  295. if (action == null || string.IsNullOrEmpty(assetsName)) return 0;
  296. if(!string.IsNullOrEmpty(assetsPath))
  297. {
  298. Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
  299. if (assetsPool.TryGetValue(assetsPath, out mDirGoPool))
  300. {
  301. GameObjectPool<GameObject> curPool = null;
  302. mDirGoPool.TryGetValue(assetsName, out curPool);
  303. if (curPool == null)
  304. {
  305. curPool = new GameObjectPool<GameObject>();
  306. mDirGoPool.Add(assetsName, curPool);
  307. }
  308. GameObject go = curPool.Spawn();
  309. if (go != null)
  310. {
  311. action(go);
  312. return 0;
  313. }
  314. }
  315. }
  316. ResourceLoadCallback<GameObject> callBack = (prefab, path_, assetName_) =>
  317. {
  318. if (null == prefab)
  319. {
  320. if (action != null)
  321. action(null);
  322. return;
  323. }
  324. if (action != null)
  325. {
  326. GameObject goInst = GameObject.Instantiate(prefab);
  327. goInst.transform.localPosition = Vector3.zero;
  328. goInst.transform.localScale = Vector3.one;
  329. goInst.name = assetsName;
  330. action(goInst);
  331. }
  332. };
  333. return LoadAsset<GameObject>(callBack, assetsPath, assetsName);
  334. }
  335. public long GetGoesFromPool(string assetsPath,string[] assetsNames, ResourceLoadCallback<List<GameObject>> cb)
  336. {
  337. if (cb == null || string.IsNullOrEmpty(assetsPath) || assetsNames == null || assetsNames.Length == 0) return 0;
  338. Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
  339. if (assetsPool.TryGetValue(assetsPath, out mDirGoPool))
  340. {
  341. List<GameObject> goList = new List<GameObject>();
  342. int cnt = 0;
  343. GameObjectPool<GameObject> curPool = null;
  344. for(int idx =0; idx < assetsNames.Length;idx++)
  345. {
  346. mDirGoPool.TryGetValue(assetsNames[idx], out curPool);
  347. if (curPool == null)
  348. {
  349. curPool = new GameObjectPool<GameObject>();
  350. mDirGoPool.Add(assetsNames[idx], curPool);
  351. }
  352. GameObject go = curPool.Spawn();
  353. if (go != null)
  354. {
  355. goList.Add(go);
  356. }
  357. else
  358. {
  359. cnt = idx;
  360. break;
  361. }
  362. }
  363. if(goList.Count == assetsNames.Length)
  364. {
  365. cb(goList, assetsPath, assetsNames);
  366. return 0;
  367. }
  368. else
  369. {
  370. for(int idx =0; idx <cnt;idx++)
  371. {
  372. RecycleGO(assetsPath, assetsNames[idx],goList[idx]);
  373. }
  374. goList.Clear();
  375. goList = null;
  376. }
  377. }
  378. ResourceLoadCallback<List<GameObject>> callBack = (prefabs, path_, assetName_) =>
  379. {
  380. if (null == prefabs)
  381. {
  382. if (cb != null)
  383. cb(null, path_, assetName_);
  384. return;
  385. }
  386. if (cb != null)
  387. {
  388. List<GameObject> goInstList = new List<GameObject>();
  389. for(int idx =0;idx < prefabs.Count;idx++)
  390. {
  391. GameObject goInst = GameObject.Instantiate(prefabs[idx]);
  392. goInst.transform.localPosition = Vector3.zero;
  393. goInst.transform.localScale = Vector3.one;
  394. goInst.name = assetName_[idx];
  395. goInstList.Add(goInst);
  396. }
  397. cb(goInstList, path_, assetName_);
  398. }
  399. };
  400. return LoadAsset<List<GameObject>>(callBack, assetsPath, assetsNames);
  401. }
  402. public GameObject GetGoFromPool(string assetPath, string assetName)
  403. {
  404. if (string.IsNullOrEmpty(assetName))
  405. return null;
  406. GameObject go = null;
  407. if (!string.IsNullOrEmpty(assetPath))
  408. {
  409. Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
  410. if (assetsPool.TryGetValue(assetPath, out mDirGoPool))
  411. {
  412. GameObjectPool<GameObject> curPool = null;
  413. mDirGoPool.TryGetValue(assetName, out curPool);
  414. if (curPool == null)
  415. {
  416. curPool = new GameObjectPool<GameObject>();
  417. mDirGoPool.Add(assetName, curPool);
  418. }
  419. go = curPool.Spawn();
  420. if (go != null)
  421. {
  422. return go;
  423. }
  424. }
  425. }
  426. GameObject prefab = (GameObject)FindAssetByPathAndName(assetPath, assetName);
  427. if(prefab == null)
  428. {
  429. prefab = ResourceMgr.Instance.LoadAssetSync<GameObject>(assetPath, assetName);
  430. }
  431. if(prefab != null)
  432. {
  433. go = GameObject.Instantiate(prefab);
  434. }
  435. return go;
  436. }
  437. public GameObject GetUIGoFromPool(string assetPath, string assetName)
  438. {
  439. if (string.IsNullOrEmpty(assetName))
  440. return null;
  441. if (!string.IsNullOrEmpty(assetPath))
  442. {
  443. Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
  444. if (assetsPool.TryGetValue(assetPath, out mDirGoPool))
  445. {
  446. GameObjectPool<GameObject> curPool = null;
  447. mDirGoPool.TryGetValue(assetName, out curPool);
  448. if (curPool == null)
  449. {
  450. curPool = new GameObjectPool<GameObject>();
  451. mDirGoPool.Add(assetName, curPool);
  452. }
  453. GameObject go = curPool.Spawn();
  454. if (go != null)
  455. {
  456. return go;
  457. }
  458. }
  459. }
  460. return null;
  461. }
  462. public void SetGoPoolMaxCacheNum(string assetPath, string assetName,int num)
  463. {
  464. if (string.IsNullOrEmpty(assetPath) || string.IsNullOrEmpty(assetName)) return;
  465. Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
  466. if(assetsPool.TryGetValue(assetPath,out mDirGoPool))
  467. {
  468. GameObjectPool<GameObject> curPool = null;
  469. mDirGoPool.TryGetValue(assetName, out curPool);
  470. if (curPool == null)
  471. {
  472. curPool = new GameObjectPool<GameObject>();
  473. mDirGoPool.Add(assetName, curPool);
  474. }
  475. curPool.SetMaxCacheNum(num);
  476. }
  477. else
  478. {
  479. mDirGoPool = new Dictionary<string, GameObjectPool<GameObject>>();
  480. GameObjectPool<GameObject> curPool = new GameObjectPool<GameObject>();
  481. curPool.SetMaxCacheNum(num);
  482. mDirGoPool.Add(assetName, curPool);
  483. assetsPool.Add(assetPath, mDirGoPool);
  484. }
  485. }
  486. public Object GetAsset(string assetPath,string assetName)
  487. {
  488. if(string.IsNullOrEmpty(assetName))
  489. {
  490. return null;
  491. }
  492. return FindAssetByPathAndName(assetPath, assetName);
  493. }
  494. public void RecycleUIGO(string assetPath, string assetName, GameObject go)
  495. {
  496. if (go == null) return;
  497. if (string.IsNullOrEmpty(assetName) || string.IsNullOrEmpty(assetPath))
  498. {
  499. GameObject.Destroy(go);
  500. go = null;
  501. return;
  502. }
  503. Dictionary<string, GameObjectPool<GameObject>> mDirGoPool = null;
  504. if (assetsPool.TryGetValue(assetPath, out mDirGoPool))
  505. {
  506. GameObjectPool<GameObject> curPool = null;
  507. if (mDirGoPool.TryGetValue(assetName, out curPool))
  508. {
  509. go.transform.SetParent(UIMgr.Instance.UIRootTrans);
  510. //CommonUtil.SetGameObjectLayer(go, "Hide");
  511. go.SetActive(false);
  512. curPool.Recycle(go);
  513. return;
  514. }
  515. else
  516. {
  517. curPool = new GameObjectPool<GameObject>();
  518. mDirGoPool.Add(assetName, curPool);
  519. go.transform.SetParent(UIMgr.Instance.UIRootTrans);
  520. //CommonUtil.SetGameObjectLayer(go, "Hide");
  521. go.SetActive(false);
  522. curPool.Recycle(go);
  523. }
  524. }
  525. else
  526. {
  527. mDirGoPool = new Dictionary<string, GameObjectPool<GameObject>>();
  528. GameObjectPool<GameObject> curPool = new GameObjectPool<GameObject>();
  529. mDirGoPool.Add(assetName, curPool);
  530. assetsPool.Add(assetPath, mDirGoPool);
  531. go.transform.SetParent(UIMgr.Instance.UIRootTrans);
  532. //CommonUtil.SetGameObjectLayer(go, "Hide");
  533. go.SetActive(false);
  534. curPool.Recycle(go);
  535. }
  536. }
  537. public void RecycleGO(string assetPath,string assetName, GameObject go)
  538. {
  539. if (go == null) return;
  540. if (string.IsNullOrEmpty(assetName) || string.IsNullOrEmpty(assetPath))
  541. {
  542. GameObject.Destroy(go);
  543. go = null;
  544. return;
  545. }
  546. Dictionary<string, GameObjectPool<GameObject>> mDirGoPool = null;
  547. if(assetsPool.TryGetValue(assetPath, out mDirGoPool))
  548. {
  549. GameObjectPool<GameObject> curPool = null;
  550. if (mDirGoPool.TryGetValue(assetName, out curPool))
  551. {
  552. go.transform.SetParent(PoolNode);
  553. curPool.Recycle(go);
  554. return;
  555. }
  556. else
  557. {
  558. curPool = new GameObjectPool<GameObject>();
  559. mDirGoPool.Add(assetName, curPool);
  560. go.transform.SetParent(PoolNode);
  561. curPool.Recycle(go);
  562. }
  563. }
  564. else
  565. {
  566. mDirGoPool = new Dictionary<string, GameObjectPool<GameObject>>();
  567. GameObjectPool<GameObject> curPool = new GameObjectPool<GameObject>();
  568. mDirGoPool.Add(assetName, curPool);
  569. assetsPool.Add(assetPath, mDirGoPool);
  570. go.transform.SetParent(PoolNode);
  571. curPool.Recycle(go);
  572. }
  573. }
  574. public void UnloadAssetBySeqId(long seqId)
  575. {
  576. RemoveLoadingCb(seqId);
  577. }
  578. [NoToLua]
  579. public void CleanGOPool()
  580. {
  581. foreach (var p in assetsPool)
  582. {
  583. Dictionary<string, GameObjectPool<GameObject>> poolDic = p.Value;
  584. foreach(var list in poolDic)
  585. {
  586. GameObjectPool<GameObject> goPool = list.Value;
  587. goPool.Dispose();
  588. }
  589. }
  590. assetsPool.Clear();
  591. }
  592. public Shader FindShader(string shaderName,string shaderPath)
  593. {
  594. return AssetsMgr.Instance.FindShader(shaderName, shaderPath);
  595. }
  596. #endregion
  597. [NoToLua]
  598. public void Update()
  599. {
  600. //if (LoadedResourceCnt == 0) return;
  601. //float curTime = Time.time;
  602. //if (curTime - lastCheckTime >= check_internal)
  603. //{
  604. // ClearLongTimeUnusedResource(curTime);
  605. // lastCheckTime = curTime;
  606. //}
  607. }
  608. #region inner_methods
  609. void RemoveLoadingCb(long seqId)
  610. {
  611. LoadingResourceInfo lri = FindLoadingResourceInfoBySeqId(seqId);
  612. if (lri != null)
  613. {
  614. AssetsMgr.Instance.CancelLoadAsset(seqId);
  615. lri.Dispose();
  616. loadingResources.Remove(lri);
  617. }
  618. }
  619. LoadingResourceInfo FindLoadingResourceInfoBySeqId(long seqId)
  620. {
  621. for (int idx = 0; idx < loadingResources.Count; idx++)
  622. {
  623. LoadingResourceInfo lri = loadingResources[idx];
  624. if (lri.SeqId == seqId) return lri;
  625. }
  626. return null;
  627. }
  628. public bool HasAsset(string path,string assetName)
  629. {
  630. if (string.IsNullOrEmpty(assetName)) return false;
  631. Object obj = FindAssetByPathAndName(path, assetName);
  632. return obj != null;
  633. }
  634. bool ExistAssets(string path, string[] assetNames)
  635. {
  636. if (assetNames == null || assetNames.Length == 0) return false;
  637. bool find = false;
  638. for (int idx = 0; idx < assetNames.Length; idx++)
  639. {
  640. Object obj = FindAssetByPathAndName(path, assetNames[idx]);
  641. if (obj == null) return false;
  642. find = true;
  643. }
  644. return find;
  645. }
  646. bool ExistAssetPath(string path)
  647. {
  648. for (int idx = 0; idx < loadedResources.Count; idx++)
  649. {
  650. ResourceInfo ri = loadedResources[idx];
  651. if (ri.Path == path)
  652. {
  653. return true;
  654. }
  655. }
  656. return false;
  657. }
  658. Object FindAssetByPathAndName(string pathName, string assetName)
  659. {
  660. for (int idx = 0; idx < loadedResources.Count; idx++)
  661. {
  662. ResourceInfo ri = loadedResources[idx];
  663. if (ri.Path == pathName && ri.Name == assetName)
  664. {
  665. return ri.Asset;
  666. }
  667. }
  668. return null;
  669. }
  670. T FindAssetByPathAndName<T>(string pathName, string assetName) where T : Object
  671. {
  672. for (int idx = 0; idx < loadedResources.Count; idx++)
  673. {
  674. ResourceInfo ri = loadedResources[idx];
  675. if (ri.Path == pathName && ri.Name == assetName)
  676. {
  677. T ret = ri.Reuse() as T;
  678. return ret;
  679. }
  680. }
  681. return null;
  682. }
  683. long Inner_LoadAsset<T>(ResourceLoadCallbackWithSeqId<T> cb, string pathName, string[] assetNames, ELoadType type, object[] userDatas)
  684. {
  685. if (string.IsNullOrEmpty(pathName) && (assetNames == null || assetNames.Length == 0)) return 0;
  686. for (int idx = 0; idx < assetNames.Length; idx++)
  687. {
  688. if (assetNames[idx] == "Null" || string.IsNullOrEmpty(assetNames[idx]))
  689. {
  690. DebugHelper.LogError("加载的资源存在Null,请检查配置");
  691. return 0;
  692. }
  693. }
  694. if (assetNames == null || assetNames.Length == 0)
  695. {
  696. if (cb != null)
  697. cb.DynamicInvoke(null, 0, pathName, userDatas, assetNames);
  698. return 0;
  699. }
  700. if (ExistAssets(pathName, assetNames))
  701. {
  702. FinishResourceLoad(cb, typeof(T), pathName, assetNames,true,0, userDatas);
  703. return 0;
  704. }
  705. loadingSeqId++;
  706. LoadingResourceInfo lri = new LoadingResourceInfo(loadingSeqId, pathName, cb as System.Delegate, typeof(T), true,assetNames);
  707. lri.userDatas = userDatas;
  708. loadingResources.Add(lri);
  709. lri.LoadCBNum = AssetsMgr.Instance.GetAsset<T>(OnLoadAssetallback, type, loadingSeqId, pathName, assetNames);
  710. if (lri.LoadCBNum == 0)
  711. {
  712. if (cb != null)
  713. cb.DynamicInvoke(null, loadingSeqId,pathName, userDatas, assetNames);
  714. RemoveLoadingCb(loadingSeqId);
  715. }
  716. return loadingSeqId;
  717. }
  718. private long Inner_LoadAsset<T>(ResourceLoadCallback<T> cb, string pathName, string[] assetNames, ELoadType type)
  719. {
  720. if (string.IsNullOrEmpty(pathName) && (assetNames == null || assetNames.Length == 0)) return 0;
  721. for (int idx = 0; idx < assetNames.Length; idx++)
  722. {
  723. if (assetNames[idx] == "Null" || string.IsNullOrEmpty(assetNames[idx]))
  724. {
  725. DebugHelper.LogError("加载的资源存在Null,请检查配置");
  726. return 0;
  727. }
  728. }
  729. if (assetNames == null || assetNames.Length == 0)
  730. {
  731. if (cb != null)
  732. cb.DynamicInvoke(null, pathName, assetNames);
  733. return 0;
  734. }
  735. if (ExistAssets(pathName, assetNames))
  736. {
  737. FinishResourceLoad(cb, typeof(T), pathName, assetNames,false,0,null);
  738. return 0;
  739. }
  740. loadingSeqId++;
  741. LoadingResourceInfo lri = new LoadingResourceInfo(loadingSeqId, pathName, cb as System.Delegate, typeof(T), false,assetNames);
  742. loadingResources.Add(lri);
  743. lri.LoadCBNum = AssetsMgr.Instance.GetAsset<T>(OnLoadAssetallback, type, loadingSeqId, pathName, assetNames);
  744. if (lri.LoadCBNum == 0)
  745. {
  746. if (cb != null)
  747. cb.DynamicInvoke(null, pathName, assetNames);
  748. RemoveLoadingCb(loadingSeqId);
  749. }
  750. return loadingSeqId;
  751. }
  752. void OnLoadDirAssetallback<T>(T result, long seqId, string assetPath, params string[] assetNames)
  753. {
  754. LoadingResourceInfo lri = FindLoadingResourceInfoBySeqId(seqId);
  755. if (lri != null)
  756. {
  757. System.Delegate cb = lri.Callback;
  758. if(cb!=null)
  759. cb.DynamicInvoke(result, assetPath, assetNames);
  760. RemoveLoadingCb(seqId);
  761. }
  762. else
  763. {
  764. DebugHelper.Log("Load " + seqId + " Failed LoadingCnt:" + LoadingResourceCnt);
  765. }
  766. }
  767. void OnLoadAssetallback<T>(T result, long seqId, string assetPath, params string[] assetNames)
  768. {
  769. var t = typeof(T);
  770. if (t.Equals(typeof(List<GameObject>)))
  771. {
  772. List<GameObject> objList = result as List<GameObject>;
  773. if (assetNames.Length > 0)
  774. {
  775. for (int idx = 0; idx < assetNames.Length; idx++)
  776. {
  777. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
  778. loadedResources.Add(ri);
  779. }
  780. }
  781. else
  782. {
  783. for (int idx = 0; idx < objList.Count; idx++)
  784. {
  785. string objName = objList[idx] != null ? objList[idx].name : null;
  786. ResourceInfo ri = new ResourceInfo(assetPath, objName, objList[idx]);
  787. loadedResources.Add(ri);
  788. }
  789. }
  790. }
  791. else if (t.Equals(typeof(List<Texture2D>)))
  792. {
  793. List<Texture2D> objList = result as List<Texture2D>;
  794. if (assetNames.Length > 0)
  795. {
  796. for (int idx = 0; idx < assetNames.Length; idx++)
  797. {
  798. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
  799. loadedResources.Add(ri);
  800. }
  801. }
  802. else
  803. {
  804. for (int idx = 0; idx < objList.Count; idx++)
  805. {
  806. ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
  807. loadedResources.Add(ri);
  808. }
  809. }
  810. }
  811. else if (t.Equals(typeof(List<Sprite>)))
  812. {
  813. List<Sprite> objList = result as List<Sprite>;
  814. if (assetNames.Length > 0)
  815. {
  816. for (int idx = 0; idx < assetNames.Length; idx++)
  817. {
  818. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
  819. loadedResources.Add(ri);
  820. }
  821. }
  822. else
  823. {
  824. for (int idx = 0; idx < objList.Count; idx++)
  825. {
  826. ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
  827. loadedResources.Add(ri);
  828. }
  829. }
  830. }
  831. else if (t.Equals(typeof(List<TextAsset>)))
  832. {
  833. List<TextAsset> objList = result as List<TextAsset>;
  834. if (assetNames.Length > 0)
  835. {
  836. for (int idx = 0; idx < assetNames.Length; idx++)
  837. {
  838. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
  839. loadedResources.Add(ri);
  840. }
  841. }
  842. else
  843. {
  844. for (int idx = 0; idx < objList.Count; idx++)
  845. {
  846. ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
  847. loadedResources.Add(ri);
  848. }
  849. }
  850. }
  851. else if (t.Equals(typeof(List<PlayableAsset>)))
  852. {
  853. List<PlayableAsset> objList = result as List<PlayableAsset>;
  854. if (assetNames.Length > 0)
  855. {
  856. for (int idx = 0; idx < assetNames.Length; idx++)
  857. {
  858. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
  859. loadedResources.Add(ri);
  860. }
  861. }
  862. else
  863. {
  864. for (int idx = 0; idx < objList.Count; idx++)
  865. {
  866. ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
  867. loadedResources.Add(ri);
  868. }
  869. }
  870. }
  871. else if (t.Equals(typeof(List<AudioClip>)))
  872. {
  873. List<AudioClip> objList = result as List<AudioClip>;
  874. if (assetNames.Length > 0)
  875. {
  876. for (int idx = 0; idx < assetNames.Length; idx++)
  877. {
  878. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
  879. loadedResources.Add(ri);
  880. }
  881. }
  882. else
  883. {
  884. for (int idx = 0; idx < objList.Count; idx++)
  885. {
  886. ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
  887. loadedResources.Add(ri);
  888. }
  889. }
  890. }
  891. else if (t.Equals(typeof(List<VideoClip>)))
  892. {
  893. List<VideoClip> objList = result as List<VideoClip>;
  894. if (assetNames.Length > 0)
  895. {
  896. for (int idx = 0; idx < assetNames.Length; idx++)
  897. {
  898. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
  899. loadedResources.Add(ri);
  900. }
  901. }
  902. else
  903. {
  904. for (int idx = 0; idx < objList.Count; idx++)
  905. {
  906. ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
  907. loadedResources.Add(ri);
  908. }
  909. }
  910. }
  911. else if (t.Equals(typeof(List<AnimationClip>)))
  912. {
  913. List<AnimationClip> objList = result as List<AnimationClip>;
  914. if (assetNames.Length > 0)
  915. {
  916. for (int idx = 0; idx < assetNames.Length; idx++)
  917. {
  918. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
  919. loadedResources.Add(ri);
  920. }
  921. }
  922. else
  923. {
  924. for (int idx = 0; idx < objList.Count; idx++)
  925. {
  926. ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
  927. loadedResources.Add(ri);
  928. }
  929. }
  930. }
  931. else if (t.Equals(typeof(List<RuntimeAnimatorController>)))
  932. {
  933. List<RuntimeAnimatorController> objList = result as List<RuntimeAnimatorController>;
  934. if (assetNames.Length > 0)
  935. {
  936. for (int idx = 0; idx < assetNames.Length; idx++)
  937. {
  938. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[idx], objList[idx]);
  939. loadedResources.Add(ri);
  940. }
  941. }
  942. else
  943. {
  944. for (int idx = 0; idx < objList.Count; idx++)
  945. {
  946. ResourceInfo ri = new ResourceInfo(assetPath, null, objList[idx]);
  947. loadedResources.Add(ri);
  948. }
  949. }
  950. }
  951. else if (t.Equals(typeof(ScriptableObject)) ||
  952. t.Equals(typeof(GameObject)) ||
  953. t.Equals(typeof(AudioClip)) ||
  954. t.Equals(typeof(VideoClip)) ||
  955. t.Equals(typeof(Texture2D)) ||
  956. t.Equals(typeof(Sprite)) ||
  957. t.Equals(typeof(Material)) ||
  958. t.Equals(typeof(AnimationClip)) ||
  959. t.Equals(typeof(RuntimeAnimatorController)))
  960. {
  961. Object obj = result as Object;
  962. ResourceInfo ri = new ResourceInfo(assetPath, assetNames[0], obj);
  963. loadedResources.Add(ri);
  964. }
  965. LoadingResourceInfo lri = FindLoadingResourceInfoBySeqId(seqId);
  966. if (lri != null)
  967. {
  968. lri.LoadCBNum = lri.LoadCBNum - 1;
  969. if(lri.LoadCBNum <= 0)
  970. {
  971. System.Delegate cb = lri.Callback;
  972. FinishResourceLoad(cb, lri.DelegateType, lri.AssetPath, lri.AssetNames, lri.ReturnSeqId, lri.SeqId,lri.userDatas);
  973. RemoveLoadingCb(seqId);
  974. }
  975. }
  976. else
  977. {
  978. DebugHelper.Log("Load " + seqId + " Failed LoadingCnt:" + LoadingResourceCnt);
  979. }
  980. }
  981. void FinishResourceLoad(System.Delegate cb, System.Type delegateType, string pathName, string[] assetNames,bool returnSeq,long seqId,object[] userdatas)
  982. {
  983. if (cb == null) return;
  984. if (delegateType.Equals(typeof(List<GameObject>)))
  985. {
  986. FinishLoad<GameObject>(cb, pathName, assetNames, false, returnSeq,seqId, userdatas);
  987. }
  988. else if (delegateType.Equals(typeof(List<Texture2D>)))
  989. {
  990. FinishLoad<Texture2D>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
  991. }
  992. else if (delegateType.Equals(typeof(List<Sprite>)))
  993. {
  994. FinishLoad<Sprite>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
  995. }
  996. else if (delegateType.Equals(typeof(List<TextAsset>)))
  997. {
  998. FinishLoad<TextAsset>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
  999. }
  1000. else if (delegateType.Equals(typeof(List<PlayableAsset>)))
  1001. {
  1002. FinishLoad<PlayableAsset>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
  1003. }
  1004. else if (delegateType.Equals(typeof(List<AudioClip>)))
  1005. {
  1006. FinishLoad<AudioClip>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
  1007. }
  1008. else if (delegateType.Equals(typeof(List<VideoClip>)))
  1009. {
  1010. FinishLoad<VideoClip>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
  1011. }
  1012. else if (delegateType.Equals(typeof(List<AnimationClip>)))
  1013. {
  1014. FinishLoad<AnimationClip>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
  1015. }
  1016. else if (delegateType.Equals(typeof(List<RuntimeAnimatorController>)))
  1017. {
  1018. FinishLoad<RuntimeAnimatorController>(cb, pathName, assetNames, false, returnSeq, seqId, userdatas);
  1019. }
  1020. else if (delegateType.Equals(typeof(GameObject)))
  1021. {
  1022. FinishLoad<GameObject>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1023. }
  1024. else if (delegateType.Equals(typeof(Texture2D)))
  1025. {
  1026. FinishLoad<Texture2D>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1027. }
  1028. else if (delegateType.Equals(typeof(Sprite)))
  1029. {
  1030. FinishLoad<Sprite>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1031. }
  1032. else if (delegateType.Equals(typeof(TextAsset)))
  1033. {
  1034. FinishLoad<TextAsset>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1035. }
  1036. else if (delegateType.Equals(typeof(PlayableAsset)))
  1037. {
  1038. FinishLoad<PlayableAsset>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1039. }
  1040. else if (delegateType.Equals(typeof(AudioClip)))
  1041. {
  1042. FinishLoad<AudioClip>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1043. }
  1044. else if (delegateType.Equals(typeof(VideoClip)))
  1045. {
  1046. FinishLoad<VideoClip>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1047. }
  1048. else if (delegateType.Equals(typeof(AnimationClip)))
  1049. {
  1050. FinishLoad<AnimationClip>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1051. }
  1052. else if (delegateType.Equals(typeof(Sprite)))
  1053. {
  1054. FinishLoad<Sprite>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1055. }
  1056. else if (delegateType.Equals(typeof(Material)))
  1057. {
  1058. FinishLoad<Material>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1059. }
  1060. else if (delegateType.Equals(typeof(RuntimeAnimatorController)))
  1061. {
  1062. FinishLoad<RuntimeAnimatorController>(cb, pathName, assetNames, true, returnSeq, seqId, userdatas);
  1063. }
  1064. }
  1065. void FinishLoad<T>(System.Delegate cb, string pathName, string[] assetNames, bool single, bool returnSeq, long seqId, object[] userdatas) where T : Object
  1066. {
  1067. if (single)
  1068. {
  1069. T go = FindAssetByPathAndName<T>(pathName, assetNames[0]);
  1070. try
  1071. {
  1072. if (returnSeq)
  1073. {
  1074. cb.DynamicInvoke(go, seqId, pathName, userdatas,assetNames);
  1075. }
  1076. else
  1077. {
  1078. cb.DynamicInvoke(go, pathName, assetNames);
  1079. }
  1080. }
  1081. catch(System.Exception e)
  1082. {
  1083. DebugHelper.LogError(e.StackTrace);
  1084. }
  1085. }
  1086. else
  1087. {
  1088. List<T> assetList = new List<T>();
  1089. if (assetNames != null && assetNames.Length > 0)
  1090. {
  1091. for (int idx = 0; idx < assetNames.Length; idx++)
  1092. {
  1093. assetList.Add(FindAssetByPathAndName<T>(pathName, assetNames[idx]));
  1094. }
  1095. }
  1096. else
  1097. {
  1098. for (int idx = 0; idx < loadedResources.Count; idx++)
  1099. {
  1100. ResourceInfo ri = loadedResources[idx];
  1101. if (ri.Path == pathName)
  1102. {
  1103. T asset = ri.Reuse() as T;
  1104. assetList.Add(asset);
  1105. }
  1106. }
  1107. }
  1108. if(returnSeq)
  1109. {
  1110. cb.DynamicInvoke(assetList,seqId, pathName, userdatas, assetNames);
  1111. }
  1112. else
  1113. {
  1114. cb.DynamicInvoke(assetList, pathName, assetNames);
  1115. }
  1116. }
  1117. ClearNullResource();
  1118. }
  1119. void ClearNullResource()
  1120. {
  1121. for (int idx = loadedResources.Count - 1; idx >= 0; idx--)
  1122. {
  1123. ResourceInfo ri = loadedResources[idx];
  1124. if (ri.Asset == null)
  1125. {
  1126. loadedResources.RemoveAt(idx);
  1127. }
  1128. }
  1129. }
  1130. public void RemoveLoadedResource(string path,string assetName)
  1131. {
  1132. for (int idx = loadedResources.Count-1; idx >= 0; idx--)
  1133. {
  1134. var ri = loadedResources[idx];
  1135. if(ri.Path == path && ri.Name == assetName)
  1136. {
  1137. ri.Dispose();
  1138. loadedResources.RemoveAt(idx);
  1139. }
  1140. }
  1141. }
  1142. List<string> mResidentDirList = new List<string>();
  1143. List<string> mResidentResList = new List<string>();
  1144. public void ReadResidentResCfg()
  1145. {
  1146. mResidentDirList.Clear();
  1147. mResidentResList.Clear();
  1148. Dictionary<string, Dictionary<string, string>> tab = ConfigMgr.Instance.getTable(Config.ResidentResCfg);
  1149. if (tab == null) return;
  1150. foreach(var p in tab)
  1151. {
  1152. var key = p.Key;
  1153. var dic = p.Value;
  1154. int kVal = 0;
  1155. if (int.TryParse(key, out kVal))
  1156. {
  1157. string resName = dic["name"];
  1158. int type = 0;
  1159. if (dic.ContainsKey("type"))
  1160. {
  1161. int.TryParse(dic["type"], out type);
  1162. }
  1163. if (!string.IsNullOrEmpty(resName) && type > 0)
  1164. {
  1165. if (type == 1)
  1166. {
  1167. if (!mResidentDirList.Contains(resName))
  1168. mResidentDirList.Add(resName);
  1169. }
  1170. else if (type == 2)
  1171. {
  1172. if (!mResidentResList.Contains(resName))
  1173. {
  1174. mResidentResList.Add(resName);
  1175. }
  1176. }
  1177. }
  1178. }
  1179. }
  1180. }
  1181. public bool IsResidentRes(string assetPath,string assetName)
  1182. {
  1183. string path = assetPath + "/" + assetName;
  1184. return IsResidentRes(path);
  1185. }
  1186. public bool IsResidentRes(string assetFullPath)
  1187. {
  1188. for (int idx = 0; idx < mResidentResList.Count; idx++)
  1189. {
  1190. if (mResidentResList[idx] == assetFullPath) return true;
  1191. }
  1192. string pathDir;
  1193. FileUtils.ExtractParent(assetFullPath, out pathDir);
  1194. if (!string.IsNullOrEmpty(pathDir))
  1195. {
  1196. for (int idx = 0; idx < mResidentDirList.Count; idx++)
  1197. {
  1198. if (mResidentDirList[idx] == pathDir) return true;
  1199. }
  1200. }
  1201. return false;
  1202. }
  1203. public void ClearAllResource()
  1204. {
  1205. for(int idx = loadedResources.Count -1; idx>=0; idx--)
  1206. {
  1207. ResourceInfo ri = loadedResources[idx];
  1208. if (IsResidentRes(ri.Path, ri.Name))
  1209. {
  1210. continue;
  1211. }
  1212. ri.Dispose();
  1213. loadedResources.RemoveAt(idx);
  1214. }
  1215. CleanGOPool();
  1216. }
  1217. void ClearLongTimeUnusedResource(float curTime)
  1218. {
  1219. for (int idx = loadedResources.Count - 1; idx >= 0; idx--)
  1220. {
  1221. ResourceInfo ri = loadedResources[idx];
  1222. float passedTime = curTime - ri.LastUsedTime;
  1223. if (passedTime >= cache_Time && !IsResidentRes(ri.Path,ri.Name) && HasGoInPool(ri.Path,ri.Name))
  1224. {
  1225. ri.Dispose();
  1226. loadedResources.RemoveAt(idx);
  1227. }
  1228. }
  1229. }
  1230. bool HasGoInPool(string assetPath,string assetName)
  1231. {
  1232. if (string.IsNullOrEmpty(assetPath)) return false;
  1233. if (assetsPool == null) return false;
  1234. Dictionary<string, GameObjectPool<GameObject>> mDirGoPool;
  1235. if (assetsPool.TryGetValue(assetPath, out mDirGoPool))
  1236. {
  1237. GameObjectPool<GameObject> curPool = null;
  1238. mDirGoPool.TryGetValue(assetName, out curPool);
  1239. if (curPool != null && curPool.Count > 0)
  1240. {
  1241. return true;
  1242. }
  1243. }
  1244. return false;
  1245. }
  1246. #endregion
  1247. }