FguiLoadMgr.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { ConstItem, UserSex } from "../../data/const/TypeConst";
  2. import AssetsBundleMgr from "../../utils/AssetsBundleMgr";
  3. import { I18n } from "../../utils/I18nUtil";
  4. import ResGComponent from "../compment/ResGComponent";
  5. /**
  6. * 动态加载资源
  7. */
  8. export default class FguiLoadMgr {
  9. //动态加载package包里的图片资源
  10. static loadSpriteFormPackage(obj: fgui.GLoader | fairygui.GLabel | fairygui.GButton, bundleName: string, pkgName: string, url: string, callback: Function = (success: boolean) => { }, scale?: number, xFlip: boolean = false, yFlip: boolean = false) {
  11. this.loadPackage(bundleName, pkgName, (error) => {
  12. if (error || !obj?.node || !obj.node.isValid) {
  13. if (callback) callback(false);
  14. return;
  15. }
  16. if (obj instanceof fgui.GLoader) {
  17. obj.url = url;
  18. } else {
  19. obj.icon = url;
  20. }
  21. if (scale) {
  22. obj.setScale(scale, scale);
  23. }
  24. if (xFlip || yFlip) {
  25. obj.setScale(obj.scaleX * (xFlip ? -1 : 1), obj.scaleY * (yFlip ? -1 : 1));
  26. }
  27. if (callback) {
  28. callback(true);
  29. }
  30. });
  31. }
  32. private static loadFguiPkg(bundle, pkgName, callback?: Function) {
  33. let p = fgui.UIPackage.getByName(pkgName);
  34. if (p) {
  35. if (callback) callback(null, p);
  36. } else {
  37. fgui.UIPackage.loadPackage(bundle, pkgName, (error, pkg) => {
  38. if (error) {
  39. if (callback) callback(error, null);
  40. return;
  41. }
  42. if (callback) {
  43. callback(null, pkg);
  44. }
  45. });
  46. }
  47. }
  48. /** 同步加载package */
  49. static async loadPackageAsync(bundleName: string, pkgName: string): Promise<void> {
  50. return new Promise<void>((resolve, reject) => {
  51. AssetsBundleMgr.loadBundle(bundleName, (error, bundle: cc.AssetManager.Bundle) => {
  52. if (error) {
  53. reject(error);
  54. return;
  55. }
  56. FguiLoadMgr.loadFguiPkg(bundle, pkgName, (error, pkg) => {
  57. if (error) {
  58. reject(error);
  59. return;
  60. }
  61. resolve();
  62. });
  63. });
  64. });
  65. }
  66. static loadPackage(bundleName: string, pkgName: string, onComplete: (error: any, pkg: fgui.UIPackage) => void): void {
  67. AssetsBundleMgr.loadBundle(bundleName, (error, bundle: cc.AssetManager.Bundle) => {
  68. if (error) {
  69. onComplete(error, null);
  70. return;
  71. }
  72. FguiLoadMgr.loadFguiPkg(bundle, pkgName, (error, pkg) => {
  73. onComplete(error, pkg);
  74. });
  75. });
  76. }
  77. /** 加载fgui里的图片 */
  78. static loadPackageFrameAsset(node: cc.Node, packageName: string, iconUrl: string) {
  79. this.loadPackage(packageName, packageName, (error, pkg) => {
  80. if (error) {
  81. return;
  82. }
  83. let asset = pkg.getItemAssetByName(iconUrl);
  84. if (asset) {
  85. let frame = asset as cc.SpriteFrame;
  86. node.getComponent(cc.Sprite).spriteFrame = frame;
  87. }
  88. });
  89. }
  90. /** 设置多语言文本 */
  91. static setI18nTextLabel(gLabel: fgui.GTextField | fgui.GRichTextField | fgui.GLabel | fgui.GButton, key: string, ...args: any[]) {
  92. let str = I18n.getI18nText(key, ...args);
  93. this.setLabel(gLabel, str);
  94. }
  95. static setLabel(gLabel: fgui.GTextField | fgui.GRichTextField | fgui.GLabel | fgui.GButton, str: string) {
  96. // gLabel.node.getComponent(cc.Label).cacheMode = cc.Label.CacheMode.CHAR;
  97. if (gLabel instanceof fgui.GTextField || gLabel instanceof fgui.GRichTextField) {
  98. gLabel.text = str;
  99. } else if (gLabel instanceof fgui.GLabel || gLabel instanceof fgui.GButton) {
  100. gLabel.title = str;
  101. }
  102. }
  103. /** 动态加载组件 */
  104. static LoadComp(parent: fairygui.GComponent, bundleName: string, pkgName: string, resName: string, userClass: new () => fgui.GObject, onComplete: (comp: fairygui.GObject) => void) {
  105. let resGComp = parent.node.getComponent(ResGComponent);
  106. if (!resGComp) resGComp = parent.node.addComponent(ResGComponent);
  107. resGComp.LoadComp(bundleName, pkgName, resName, userClass, (comp: fairygui.GObject) => {
  108. onComplete(comp);
  109. });
  110. }
  111. }