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~
This commit is contained in:
zheng han
2021-04-12 15:46:20 +08:00
committed by GitHub
parent 60dd3024c0
commit ad276d99e8
5 changed files with 111 additions and 5 deletions
+6 -2
View File
@@ -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;
}
+1 -1
View File
@@ -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;
}
+4 -1
View File
@@ -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);
}
+23 -1
View File
@@ -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])
+77
View File
@@ -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<string, any | Node | Component>) {
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<string, any | Node | Component>) {
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<string, any | Node | Component>) {
if (propertyOverrides.length <= 0) {
return;