feature: support prefab id for onlinex (#12717)

* feature: support prefab id for onlinex

* review fix
This commit is contained in:
王凯鹏
2022-11-23 19:30:43 +08:00
committed by GitHub
parent ea35bf5720
commit 3eda1eb856
2 changed files with 40 additions and 7 deletions
+12 -7
View File
@@ -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<string, any | Node | Component> = {};
/**
@@ -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 = '';
+28
View File
@@ -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}`);
}
}
}