示例场景 : Assets/Scripts/Example/AF/1.ResFrame/ASyncLoadExample.unity
image.png

一.从Resources路径下加载资源

  1. public void loadSprite()
  2. {
  3. //请注意不要带后缀,加载Resource路径下的Sprite
  4. resLoader.LoadAsync<Sprite>(ResFromType.ResourcesRes, "LocalImageExample/ChangeSetGo", LoadSpriteCallBack, true);
  5. }
  6. public void LoadSpriteCallBack(bool result,ResInfo resInfo)
  7. {
  8. if(result)
  9. {
  10. spriteRender.sprite = resInfo.ResObject as Sprite;
  11. }
  12. else
  13. {
  14. AFLogger.e("加载" + resInfo.ResPath + "失败!");
  15. }
  16. }
  17. //释放resLoader加载的单个资源
  18. public void ReleaseSprite()
  19. {
  20. spriteRender.sprite = null;
  21. //如果传false内存不会释放
  22. resLoader.ReleaseRes("LocalImageExample/ChangeSetGo", true);
  23. }
  24. public void LoadPrefab()
  25. {
  26. //加载Resource路径下的Prefabs
  27. resLoader.LoadAsync<GameObject>(ResFromType.ResourcesRes, "ExampleRes/Models/OxygenTank", LoadResPrefab, DestroyCache: true);
  28. }
  29. public void LoadResPrefab(bool result, ResInfo resInfo)
  30. {
  31. if (result)
  32. {
  33. Res.Add(Instantiate(resInfo.ResObject as GameObject));
  34. AFLogger.d("从Resources加载并实例化了" + Res.Count + "个");
  35. }
  36. else
  37. {
  38. AFLogger.e("加载" + resInfo.ResPath + "失败!");
  39. }
  40. }
  41. public void ReleaseResPrefab()
  42. {
  43. if (Res.Count > 0)
  44. {
  45. AFLogger.d("释放从Resources加载的资源");
  46. DestroyImmediate(Res[Res.Count - 1]);
  47. Res.RemoveAt(Res.Count - 1);
  48. //满足释放的条件 : 1.释放资源时destoryObj为true,默认为false 2.此资源的引用计数为0
  49. //也就是说你加载此资源实例化了多少次,就需要释放多少次,并且最后一次释放调用destoryObj = true
  50. resLoader.ReleaseRes("ExampleRes/Models/OxygenTank", true);
  51. }
  52. else
  53. {
  54. AFLogger.d("请先加载资源再释放");
  55. }
  56. }

二.从AB同步加载prefab

  1. public void LoadSpriteFromLocalAB()
  2. {
  3. //从AB包加载Sprite,这里只测试了本地加载,加载来源配置要设置为streamasset
  4. resLoader.LoadAsync<Sprite>(ResFromType.ABRes, "Assets/ResForAB/ABMain/Img/Help.png", LoadABSpriteCallBack, true);
  5. }
  6. public void LoadABSpriteCallBack(bool result,ResInfo resInfo)
  7. {
  8. spriteRender1.sprite = resInfo.ResObject as Sprite;
  9. }
  10. //释放resLoader加载的单个资源
  11. public void ReleaseABSprite()
  12. {
  13. spriteRender1.sprite = null;
  14. //如果传false内存不会释放
  15. resLoader.ReleaseRes("Assets/ResForAB/ABMain/Img/Help.png", true);
  16. }
  17. public void LoadPrefabFromLocalAB()
  18. {
  19. resLoader.InstantiateObjectASync(ResFromType.ABRes, "Assets/ResForAB/ABMain/Prefabs/ABLoadTestPre.prefab",
  20. LoadObjCallBack, UIPar, bClear:true);
  21. }
  22. public void LoadObjCallBack(bool result,ResObject resObject)
  23. {
  24. if(result)
  25. {
  26. Obj.Add(resObject.mCloneObj);
  27. }
  28. }
  29. public void ReleaseObjPrefab()
  30. {
  31. if (Obj.Count > 0)
  32. {
  33. AFLogger.d("释放从AB包加载的资源");
  34. //满足释放的条件 : 1.maxCacheCount为0 2.destoryObj为true 3.引用计数为0
  35. resLoader.ReleaseObj(Obj[Obj.Count - 1], 0, true);
  36. Obj.RemoveAt(Obj.Count - 1);
  37. }
  38. else
  39. {
  40. AFLogger.d("请先加载资源再释放");
  41. }
  42. }

三.编辑器中加载资源

同一中一样使用resloader释放

  1. //专用于编辑器测试,路径为相对于Assets,加载Sprite
  2. public void LoadSpriteFromEditor()
  3. {
  4. resLoader.LoadAsync<Sprite>(ResFromType.EditorRes, "Assets/AFramewprk/Example/Resources/LocalImageExample/ChangeSetGo.png", LoadSprite, true, true);
  5. }
  6. //异步加载Sprite回调
  7. public void LoadSprite(bool result, ResInfo resInfo)
  8. {
  9. if (result)
  10. {
  11. spriteRender.sprite = resInfo.ResObject as Sprite;
  12. }
  13. }

四.API

LoadAsync :

resType 加载来源,与assetPath配合,确定资源从哪里进行加载
assetPath 加载资源路径, 其他方式都是相对于Asset路径,从Reources加载是相对Resources并且没有后缀名
loadFinish 加载回调函数
isSprite 是否加载Sprite,因为Sprite有自己单独的加载方法
DestroyCache 回收时是否销毁内存
loadResPriority 加载优先级,暂未实现

同名参数不再赘述

InstantiateObjectSync :

parTrans 实例化GameObject的父物体
bClear 同DestroyCache