FguiAssetModule.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using FairyGUI;
  2. using System;
  3. using UnityEngine;
  4. using XGame.Framework.Asset;
  5. using XGame.Framework.UI;
  6. namespace XGame.Framework.FGUI
  7. {
  8. internal class FguiAssetModule : IUIAssetModule, IDisposable
  9. {
  10. private IAssetModule _assetModule;
  11. private IAssetModule Asset => _assetModule ?? (_assetModule = new AssetModule());
  12. private UIContext _context;
  13. private IPackageHandle _pkgHandle;
  14. public FguiAssetModule(UIContext context, IPackageHandle pkgHandle)
  15. {
  16. _context = context;
  17. _pkgHandle = pkgHandle;
  18. }
  19. IAssetLoadAsync<TResult> IUIAssetModule.LoadAsync<TResult>(string addressableName)
  20. {
  21. return Asset.LoadAsync<TResult>(addressableName);
  22. }
  23. void IUIAssetModule.Recycle(UnityEngine.Object obj, bool isDestroy)
  24. {
  25. if (obj is GameObject go)
  26. {
  27. Asset.Recycle(go, isDestroy);
  28. }
  29. else
  30. {
  31. Asset.Recycle(obj);
  32. }
  33. }
  34. IAssetLoadAsync<INestedView> IUIAssetModule.LoadNested(UIKey nestedKey)
  35. {
  36. return LoadNested(nestedKey, null);
  37. }
  38. /// <summary>
  39. /// fgui全部销毁
  40. /// </summary>
  41. /// <param name="nestedView"></param>
  42. /// <param name="isDestroy"></param>
  43. void IUIAssetModule.Recycle(INestedView nestedView, bool isDestroy)
  44. {
  45. if (nestedView == null) return;
  46. nestedView.Disable();
  47. var nested = (nestedView as INestedInternal)?.Nested;
  48. (nestedView as IDisposable)?.Dispose();
  49. if (nested is FguiNested uiNested)
  50. {
  51. var com = uiNested.Component;
  52. com.parent?.RemoveChild(com);
  53. com.Dispose();
  54. }
  55. }
  56. void IUIAssetModule.StopAllAsyncs()
  57. {
  58. _assetModule?.StopAllAsyncs();
  59. }
  60. void IDisposable.Dispose()
  61. {
  62. (_assetModule as IDisposable)?.Dispose();
  63. _assetModule = null;
  64. _context = null;
  65. _pkgHandle = null;
  66. }
  67. public IAssetLoadAsync<INestedView> LoadNested(UIKey nestedKey, GComponent parent)
  68. {
  69. var objLoadAsync = _pkgHandle.LoadGobjectAsync(nestedKey);
  70. var nestedLoadAsync = new FguiNestedLoadAsync(objLoadAsync, nestedKey, parent, _context);
  71. return nestedLoadAsync;
  72. }
  73. }
  74. }