From 3eda1eb85649799527c92dc7e9d4a0aaf19130b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=87=AF=E9=B9=8F?= <609075410@qq.com> Date: Wed, 23 Nov 2022 19:30:43 +0800 Subject: [PATCH] feature: support prefab id for onlinex (#12717) * feature: support prefab id for onlinex * review fix --- cocos/scene-graph/prefab/prefab-info.ts | 19 ++++++++++------- cocos/scene-graph/prefab/utils.ts | 28 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/cocos/scene-graph/prefab/prefab-info.ts b/cocos/scene-graph/prefab/prefab-info.ts index a5f2aa4e6a..cedce9e335 100644 --- a/cocos/scene-graph/prefab/prefab-info.ts +++ b/cocos/scene-graph/prefab/prefab-info.ts @@ -20,7 +20,7 @@ function compareStringArray (array1: string[] | undefined, array2: string[] | un @ccclass('cc.TargetInfo') export class TargetInfo { - // 用于标识目标在prefab 资源中的ID,区别于UUID + // as the target's fileId in prefab asset,used to find the target when prefab expanded. @serializable public localID: string[] = []; } @@ -115,30 +115,35 @@ export class PrefabInstance { @serializable public fileId = ''; - // 记录PrefabInstance所属的Prefab的Root节点信息 + // record the node with the Prefab that this prefabInstance belongs to. @serializable @type(Node) public prefabRootNode?: Node; - // 实例化的Prefab中额外增加的子节点数据 + // record children nodes that exist in this prefabInstance but not in prefab asset. @serializable @type([MountedChildrenInfo]) public mountedChildren: MountedChildrenInfo[] = []; - // 实例化的Prefab中额外增加的Component数据 + // record components that exist in this prefabInstance but not in prefab asset. @serializable @type([MountedComponentsInfo]) public mountedComponents: MountedComponentsInfo[] = []; - // 属性的覆盖数据 + // override properties info in this prefabInstance. @serializable @type([PropertyOverrideInfo]) public propertyOverrides: PropertyOverrideInfo[] = []; + // record components that exist in ths prefab asset but not in prefabInstance. @serializable @type([TargetInfo]) public removedComponents: TargetInfo[] = []; + // record children's id in prefab asset. + @serializable + public ids: string[] = []; + public targetMap: Record = {}; /** @@ -180,12 +185,12 @@ export class PrefabInfo { @type(Node) public root?: Node; - // 所属的 prefab 资源对象 (cc.Prefab) + // reference to the prefab asset file. // In Editor, only asset._uuid is usable because asset will be changed. @serializable public asset?: Prefab; - // 用来标识别该节点在 prefab 资源中的位置,因此这个 ID 只需要保证在 Assets 里不重复就行 + // prefabInfo's id,unique in the asset. @serializable @editable public fileId = ''; diff --git a/cocos/scene-graph/prefab/utils.ts b/cocos/scene-graph/prefab/utils.ts index 0a78695c68..76b6963c4f 100644 --- a/cocos/scene-graph/prefab/utils.ts +++ b/cocos/scene-graph/prefab/utils.ts @@ -434,6 +434,7 @@ export function expandNestedPrefabInstanceNode (node: Node) { if (prefabInfo && prefabInfo.nestedPrefabInstanceRoots) { prefabInfo.nestedPrefabInstanceRoots.forEach((instanceNode: Node) => { expandPrefabInstanceNode(instanceNode); + applyPrefabInstanceIds(instanceNode); }); } } @@ -451,3 +452,30 @@ export function applyNodeAndComponentId (node: Node, rootId: string) { applyNodeAndComponentId(child, rootId); } } + +export function applyPrefabInstanceIds (node: Node) { + // @ts-expect-error private member access + const prefab = node._prefab; + if (prefab?.instance?.ids) { + const ids: string[] = prefab.instance.ids; + const idsLength: number = ids.length; + if (idsLength === 0) return; + let index = 0; + let childrenCount = 0; + let matched = true; + node.walk((child) => { + childrenCount++; + matched = index < idsLength; + // @ts-expect-error private member access + child._id = matched ? ids[index++] : child._id; + child.components.forEach((component) => { + childrenCount++; + matched = index < idsLength; + component._id = matched ? ids[index++] : component._id; + }); + }); + if (!matched) { + warn(`node:${node.name} applyPrefabInstanceIds failed:${childrenCount} id expected,only have ${idsLength}`); + } + } +}