From ad276d99e8aa8aec8e5df563a417fe81d803dc93 Mon Sep 17 00:00:00 2001 From: zheng han Date: Mon, 12 Apr 2021 15:46:20 +0800 Subject: [PATCH] V3.1 prefab nested apply (#8412) * add or remove component in prefabInstance * fix type * app mounted children * remove editorOnly import * remove _mountedRoot here and let editor polyfill it~ --- cocos/core/components/component.ts | 8 ++- cocos/core/data/instantiate.ts | 2 +- cocos/core/scene-graph/node.ts | 5 +- cocos/core/utils/prefab/prefab-info.ts | 24 +++++++- cocos/core/utils/prefab/utils.ts | 77 ++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 5 deletions(-) diff --git a/cocos/core/components/component.ts b/cocos/core/components/component.ts index 5be69d55ce..87031ff0d0 100644 --- a/cocos/core/components/component.ts +++ b/cocos/core/components/component.ts @@ -385,11 +385,15 @@ class Component extends CCObject { this.node._removeComponent(this); } - public _instantiate (cloned: Component) { + public _instantiate (cloned?: Component) { if (!cloned) { cloned = legacyCC.instantiate._clone(this, this); } - cloned.node = NullNode; + + if (cloned) { + cloned.node = NullNode; + } + return cloned; } diff --git a/cocos/core/data/instantiate.ts b/cocos/core/data/instantiate.ts index f210047fd6..9268a42288 100644 --- a/cocos/core/data/instantiate.ts +++ b/cocos/core/data/instantiate.ts @@ -281,7 +281,7 @@ function instantiateObj (obj, parent) { return obj; } } else if (obj instanceof legacyCC.Component) { - if (!obj.node.isChildOf(parent)) { + if (obj.node && !obj.node.isChildOf(parent)) { // should not clone other component if not descendant return obj; } diff --git a/cocos/core/scene-graph/node.ts b/cocos/core/scene-graph/node.ts index 671a0bbf40..9e3864329b 100644 --- a/cocos/core/scene-graph/node.ts +++ b/cocos/core/scene-graph/node.ts @@ -44,7 +44,8 @@ import { NULL_HANDLE, NodeHandle, NodePool, NodeView, } from '../renderer/core/memory-pools'; import { NodeSpace, TransformBit } from './node-enum'; -import { applyMountedChildren, applyPropertyOverrides, applyTargetOverrides, createNodeWithPrefab, generateTargetMap } from '../utils/prefab/utils'; +import { applyMountedChildren, applyMountedComponents, applyRemovedComponents, + applyPropertyOverrides, applyTargetOverrides, createNodeWithPrefab, generateTargetMap } from '../utils/prefab/utils'; import { Component } from '../components'; const v3_a = new Vec3(); @@ -419,6 +420,8 @@ export class Node extends BaseNode { generateTargetMap(this, targetMap, true); applyMountedChildren(this, prefabInstance.mountedChildren, targetMap); + applyRemovedComponents(this, prefabInstance.removedComponents, targetMap); + applyMountedComponents(this, prefabInstance.mountedComponents, targetMap); applyPropertyOverrides(this, prefabInstance.propertyOverrides, targetMap); } diff --git a/cocos/core/utils/prefab/prefab-info.ts b/cocos/core/utils/prefab/prefab-info.ts index 36d8396044..12ab710ed8 100644 --- a/cocos/core/utils/prefab/prefab-info.ts +++ b/cocos/core/utils/prefab/prefab-info.ts @@ -3,7 +3,7 @@ import { EDITOR } from 'internal:constants'; import { legacyCC } from '../../global-exports'; import Prefab from '../../assets/prefab'; import { CCObject } from '../../data/object'; -import { Component } from '../../components'; +import { Component } from '../../components/component'; import { Node } from '../../scene-graph/node'; function compareStringArray (array1: string[]|undefined, array2: string[]|undefined) { @@ -88,6 +88,23 @@ export class MountedChildrenInfo { } } +@ccclass('cc.MountedComponentsInfo') +export class MountedComponentsInfo { + @serializable + @type(TargetInfo) + public targetInfo: TargetInfo|null = null; + @serializable + @type([Component]) + public components: Component[] = []; + + // eslint-disable-next-line consistent-return + public isTarget (localID: string[]) { + if (EDITOR) { + return compareStringArray(this.targetInfo?.localID, localID); + } + } +} + /** * Prefab实例类 */ @@ -107,6 +124,11 @@ export class PrefabInstance { @type([MountedChildrenInfo]) public mountedChildren: MountedChildrenInfo[] = []; + // 实例化的Prefab中额外增加的Component数据 + @serializable + @type([MountedComponentsInfo]) + public mountedComponents: MountedComponentsInfo[] = []; + // 属性的覆盖数据 @serializable @type([PropertyOverrideInfo]) diff --git a/cocos/core/utils/prefab/utils.ts b/cocos/core/utils/prefab/utils.ts index af13a731e2..3fead5b58b 100644 --- a/cocos/core/utils/prefab/utils.ts +++ b/cocos/core/utils/prefab/utils.ts @@ -36,6 +36,7 @@ import { errorID, warn } from '../../platform/debug'; import { Component } from '../../components'; import type { BaseNode } from '../../scene-graph/base-node'; import { MountedChildrenInfo, PropertyOverrideInfo } from './prefab-info'; +import { MountedComponentsInfo, TargetInfo } from '.'; export function createNodeWithPrefab (node: Node) { // @ts-expect-error: private member access @@ -104,6 +105,9 @@ export function createNodeWithPrefab (node: Node) { // TODO: more efficient id->Node/Component map export function generateTargetMap (node: Node, targetMap: any, isRoot: boolean) { + if (!targetMap) { + return; + } let curTargetMap = targetMap; // @ts-expect-error: private member access @@ -165,13 +169,31 @@ export function applyMountedChildren (node: Node, mountedChildren: MountedChildr continue; } + let curTargetMap = targetMap; + const localID = childInfo.targetInfo.localID; + if (localID.length > 0) { + for (let i = 0; i < localID.length - 1; i++) { + curTargetMap = curTargetMap[localID[i]]; + } + } if (childInfo.nodes) { for (let i = 0; i < childInfo.nodes.length; i++) { const childNode = childInfo.nodes[i]; + + if (!childNode) { + continue; + } + // @ts-expect-error private member access target._children.push(childNode); // @ts-expect-error private member access childNode._parent = target; + if (EDITOR) { + // @ts-expect-error editor polyfill + childNode._mountedRoot = node; + } + // mounted node need to add to the target map + generateTargetMap(childNode, curTargetMap, false); // siblingIndex update is in _onBatchCreated function, and it needs a parent. childNode._onBatchCreated(false); } @@ -180,6 +202,61 @@ export function applyMountedChildren (node: Node, mountedChildren: MountedChildr } } +export function applyMountedComponents (node: Node, mountedComponents: MountedComponentsInfo[], targetMap: Record) { + if (!mountedComponents) { + return; + } + + for (let i = 0; i < mountedComponents.length; i++) { + const componentsInfo = mountedComponents[i]; + if (componentsInfo && componentsInfo.targetInfo) { + const target = getTarget(componentsInfo.targetInfo.localID, targetMap) as Node; + if (!target) { + continue; + } + + if (componentsInfo.components) { + for (let i = 0; i < componentsInfo.components.length; i++) { + const comp = componentsInfo.components[i]; + if (!comp) { + continue; + } + + comp.node = target; + if (EDITOR) { + // @ts-expect-error editor polyfill + comp._mountedRoot = node; + } + // @ts-expect-error private member access + target._components.push(comp); + } + } + } + } +} + +export function applyRemovedComponents (node: Node, removedComponents: TargetInfo[], targetMap: Record) { + if (!removedComponents) { + return; + } + + for (let i = 0; i < removedComponents.length; i++) { + const targetInfo = removedComponents[i]; + if (targetInfo) { + const target = getTarget(targetInfo.localID, targetMap) as Component; + if (!target || !target.node) { + continue; + } + + const index = target.node.components.indexOf(target); + if (index >= 0) { + // @ts-expect-error private member access + target.node._components.splice(index, 1); + } + } + } +} + export function applyPropertyOverrides (node: Node, propertyOverrides: PropertyOverrideInfo[], targetMap: Record) { if (propertyOverrides.length <= 0) { return;