diff --git a/src/ui/IPackageConfig.ts b/src/ui/IPackageConfig.ts index 7c4121d..a0a292b 100644 --- a/src/ui/IPackageConfig.ts +++ b/src/ui/IPackageConfig.ts @@ -7,6 +7,10 @@ interface IPackageConfig { /** UI所在resources中的路径 */ uiPath: string; + + /** 如果UI不在 resources 中,则需要配置 所在bundle下的路径名*/ + bundlePaths?: { [bundleName: string]: string }; + /** * 手动管理资源的包 * 1. 用于基础UI包, 提供一些最基础的组件,所有其他包都可能引用其中的内容 diff --git a/src/ui/UIDecorator.ts b/src/ui/UIDecorator.ts index 9536a22..f0f734a 100644 --- a/src/ui/UIDecorator.ts +++ b/src/ui/UIDecorator.ts @@ -40,6 +40,8 @@ export namespace _uidecorator { pkg: string; /** 窗口名 */ name: string; + /** 窗口bundle名 */ + bundle: string; }; } /** 用来存储窗口注册信息 @internal */ @@ -56,7 +58,7 @@ export namespace _uidecorator { * @param {string} pkgName fgui包名 * @param {string} name 窗口名 (与fgui中的组件名一一对应) */ - export function uiclass(groupName: string, pkgName: string, name: string): Function { + export function uiclass(groupName: string, pkgName: string, name: string, bundle?: string): Function { /** target 类的构造函数 */ return function (ctor: any): void { // debug(`uiclass >${JSON.stringify(res)}<`); @@ -71,6 +73,7 @@ export namespace _uidecorator { group: groupName, pkg: pkgName, name: name, + bundle: bundle || "", }, }); }; @@ -127,6 +130,8 @@ export namespace _uidecorator { pkg: string; /** 组件名 */ name: string; + /** headerbundle名 */ + bundle: string; }; } /** 用来存储组件注册信息 @internal */ @@ -142,7 +147,7 @@ export namespace _uidecorator { * @param {string} pkg 包名 * @param {string} name 组件名 */ - export function uiheader(pkg: string, name: string): Function { + export function uiheader(pkg: string, name: string, bundle?: string): Function { return function (ctor: any): void { // log(`pkg:【${pkg}】 uiheader prop >${JSON.stringify(ctor[UIPropMeta] || {})}<`); uiheaderMap.set(ctor, { @@ -154,6 +159,7 @@ export namespace _uidecorator { res: { pkg: pkg, name: name, + bundle: bundle || "", } }); }; diff --git a/src/ui/WindowManager.ts b/src/ui/WindowManager.ts index 8159975..bca6c9b 100644 --- a/src/ui/WindowManager.ts +++ b/src/ui/WindowManager.ts @@ -213,13 +213,13 @@ export class WindowManager { // 窗口注册 for (const { ctor, res } of _uidecorator.getWindowMaps().values()) { debug(`窗口注册 窗口名:${res.name} 包名:${res.pkg} 组名:${res.group}`); - this._resPool.add(ctor, res.group, res.pkg, res.name); + this._resPool.add(ctor, res.group, res.pkg, res.name, res.bundle); } // 窗口header注册 for (const { ctor, res } of _uidecorator.getHeaderMaps().values()) { debug(`header注册 header名:${res.name} 包名:${res.pkg}`); - this._resPool.addHeader(ctor, res.pkg, res.name); + this._resPool.addHeader(ctor, res.pkg, res.name, res.bundle); } // 组件注册 ComponentExtendHelper.register(); diff --git a/src/ui/WindowResPool.ts b/src/ui/WindowResPool.ts index 707bf0a..f7ae411 100644 --- a/src/ui/WindowResPool.ts +++ b/src/ui/WindowResPool.ts @@ -4,6 +4,7 @@ * @Description: */ +import { assetManager, resources } from "cc"; import { UIObjectFactory, UIPackage } from "fairygui-cc"; import { warn } from "../kunpocc"; import { IPackageConfigRes } from "./IPackageConfig"; @@ -17,11 +18,17 @@ export interface WindowInfo { pkg: string; /** 窗口名 */ name: string; + /** bundle名 */ + bundle: string; } export interface HeaderInfo { + /** 类的构造函数 */ ctor: any; + /** fgui包名 */ pkg: string; + /** bundle名 */ + bundle: string; } /** @internal */ @@ -30,6 +37,8 @@ export class WindowResPool { protected _windowInfos: Map = new Map(); /** 窗口header信息池 @internal */ protected _headerInfos: Map = new Map(); + /** UI包所在的bundle名 */ + private _pkgBundles: Map = new Map(); /** 是否设置过配置内容 @internal */ private _isInit: boolean = false; @@ -37,8 +46,12 @@ export class WindowResPool { private _windowPkgs: Map = new Map(); /** 包的引用计数 @internal */ private _pkgRefs: { [pkg: string]: number } = {}; + /** UI包路径 @internal */ - private _uipath: string = ""; + // private _uipath: string = ""; + /** UI包在bundle中的路径 @internal */ + private _uiPaths: { [bundle: string]: string } = {}; + /** 手动管理的包 @internal */ private _manualPackages: Set = new Set(); /** 立即释放的包 @internal */ @@ -58,7 +71,7 @@ export class WindowResPool { * 注册窗口信息 * @param info */ - public add(ctor: any, group: string, pkg: string, name: string): void { + public add(ctor: any, group: string, pkg: string, name: string, bundle: string): void { if (this.has(name)) { return; } @@ -66,8 +79,10 @@ export class WindowResPool { ctor: ctor, group: group, pkg: pkg, - name: name + name: name, + bundle: bundle }); + this._pkgBundles.set(pkg, bundle || "resources"); this.addWindowPkg(name, pkg); // 窗口组件扩展 UIObjectFactory.setExtension(`ui://${pkg}/${name}`, ctor); @@ -88,14 +103,16 @@ export class WindowResPool { * 注册窗口header信息 * @param info */ - public addHeader(ctor: any, pkg: string, name: string): void { + public addHeader(ctor: any, pkg: string, name: string, bundle: string): void { if (this.hasHeader(name)) { return; } this._headerInfos.set(name, { ctor: ctor, - pkg: pkg + pkg: pkg, + bundle: bundle }); + this._pkgBundles.set(pkg, bundle || "resources"); // 窗口header扩展 UIObjectFactory.setExtension(`ui://${pkg}/${name}`, ctor); } @@ -124,10 +141,13 @@ export class WindowResPool { this._hideWaitWindow = res?.hideWaitWindow; this._fail = res?.fail; - this._uipath = res.config?.uiPath || ""; - // 如果uipath不以/结尾 则添加/ - if (this._uipath != "" && !this._uipath.endsWith("/")) { - this._uipath += "/"; + this._uiPaths = res.config?.bundlePaths || {}; + this._uiPaths["resources"] = res.config?.uiPath || ""; + + for (const bundle in this._uiPaths) { + if (this._uiPaths[bundle] != "" && !this._uiPaths[bundle].endsWith("/")) { + this._uiPaths[bundle] += "/"; + } } this._manualPackages = new Set(res.config.manualPackages || []); @@ -249,7 +269,12 @@ export class WindowResPool { return; } for (const pkg of needLoadPkgs) { - UIPackage.loadPackage(this._uipath + pkg, (err: any) => { + let bundleName = this.getPkgBundle(pkg); + let bundle = bundleName === "resources" ? resources : assetManager.getBundle(bundleName); + if (!bundle) { + throw new Error(`UI包【${pkg}】所在的bundle【${bundleName}】未加载`); + } + UIPackage.loadPackage(bundle, this.getPkgPath(pkg), (err: any) => { total--; err ? failPkgs.push(pkg) : successPkgs.push(pkg); if (total > 0) { @@ -264,6 +289,17 @@ export class WindowResPool { } } + /** 获取UI包所在的bundle名 */ + private getPkgBundle(pkg: string): string { + return this._pkgBundles.get(pkg) || "resources"; + } + + /** 获取UI包在bundle中的路径 */ + private getPkgPath(pkg: string): string { + let bundle = this._pkgBundles.get(pkg); + return this._uiPaths[bundle] + pkg; + } + /** 获取窗口对应的包名列表 */ private getWindowPkgs(windowName: string): string[] { if (this._windowPkgs.has(windowName)) {