CustomLoader.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using FairyGUI;
  2. using XGame.Framework.Asyncs;
  3. namespace XGame.Framework.FGUI
  4. {
  5. /// <summary>
  6. /// 自定义加载,加载UIPackage之外的资源
  7. /// 貌似只有贴图有效
  8. /// 该类由UIModuleFGUI创建
  9. /// </summary>
  10. public class CustomLoader : GLoader
  11. {
  12. private IGloaderHandle _handle;
  13. private IAsync _loadingAsync;
  14. public CustomLoader(IGloaderHandle handle)
  15. {
  16. _handle = handle;
  17. }
  18. protected override void OnUrlChanged()
  19. {
  20. _loadingAsync?.RemoveAll();
  21. _loadingAsync = null;
  22. }
  23. protected override void LoadExternal()
  24. {
  25. var addressable = this.url;
  26. _loadingAsync?.RemoveAll();
  27. _loadingAsync = null;
  28. var loadAsync = _handle.LoadNTexture(addressable);
  29. _loadingAsync = loadAsync;
  30. loadAsync.On(_ =>
  31. {
  32. _loadingAsync = null;
  33. var result = _handle.GetNTexture(addressable);
  34. if (result == null)
  35. {
  36. //Log.Error($"CustomLoader failed. addressable:{addressable}");
  37. this.onExternalLoadFailed();
  38. return;
  39. }
  40. Log.Debug($"CustomLoader LoadSuccess Name:{addressable} url:{url} texture:{result.nativeTexture.name}");
  41. this.onExternalLoadSuccess(result);
  42. });
  43. //IconManager.inst.LoadIcon(this.url, OnLoadSuccess, OnLoadFail);
  44. }
  45. protected override void FreeExternal(NTexture texture)
  46. {
  47. texture.ReleaseRef();
  48. //Log.Debug($"CustomLoader FreeExternal Name: {this.name} Texture:{texture.nativeTexture.name} Ref:{texture.refCount} rootRef:{texture.root.refCount}");
  49. }
  50. override public void Dispose()
  51. {
  52. //Log.Info($"CustomLoader Dispose.");
  53. _loadingAsync?.RemoveAll();
  54. _loadingAsync = null;
  55. // 先由GLoader触发FreeExternal
  56. base.Dispose();
  57. _handle = null;
  58. }
  59. }
  60. }