| 18 | * Prefab loader implementation |
| 19 | */ |
| 20 | export class PrefabLoader implements IAssetLoader<IPrefabAsset> { |
| 21 | readonly supportedType = AssetType.Prefab; |
| 22 | readonly supportedExtensions = ['.prefab']; |
| 23 | readonly contentType: AssetContentType = 'text'; |
| 24 | |
| 25 | /** |
| 26 | * 从文本内容解析预制体 |
| 27 | * Parse prefab from text content |
| 28 | */ |
| 29 | async parse(content: IAssetContent, context: IAssetParseContext): Promise<IPrefabAsset> { |
| 30 | if (!content.text) { |
| 31 | throw new Error('Prefab content is empty'); |
| 32 | } |
| 33 | |
| 34 | let prefabData: IPrefabData; |
| 35 | try { |
| 36 | prefabData = JSON.parse(content.text) as IPrefabData; |
| 37 | } catch (error) { |
| 38 | throw new Error(`Failed to parse prefab JSON: ${(error as Error).message}`); |
| 39 | } |
| 40 | |
| 41 | // 验证预制体格式 | Validate prefab format |
| 42 | this.validatePrefabData(prefabData); |
| 43 | |
| 44 | // 版本兼容性检查 | Version compatibility check |
| 45 | if (prefabData.version > PREFAB_FORMAT_VERSION) { |
| 46 | console.warn( |
| 47 | `Prefab version ${prefabData.version} is newer than supported version ${PREFAB_FORMAT_VERSION}. ` + |
| 48 | `Some features may not work correctly.` |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | // 构建资产对象 | Build asset object |
| 53 | const prefabAsset: IPrefabAsset = { |
| 54 | data: prefabData, |
| 55 | guid: context.metadata.guid, |
| 56 | path: context.metadata.path, |
| 57 | |
| 58 | // 快捷访问属性 | Quick access properties |
| 59 | get root(): SerializedPrefabEntity { |
| 60 | return prefabData.root; |
| 61 | }, |
| 62 | get componentTypes(): string[] { |
| 63 | return prefabData.metadata.componentTypes; |
| 64 | }, |
| 65 | get referencedAssets(): string[] { |
| 66 | return prefabData.metadata.referencedAssets; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | return prefabAsset; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * 释放已加载的资产 |
| 75 | * Dispose loaded asset |
| 76 | */ |
| 77 | dispose(asset: IPrefabAsset): void { |
nothing calls this directly
no outgoing calls
no test coverage detected