From 8ca3044cdff2bf88a12a4ffc3100d05454aa0340 Mon Sep 17 00:00:00 2001 From: Santy-Wang Date: Mon, 20 Jun 2022 23:18:41 +0800 Subject: [PATCH] set persist node's hideFlags to DontSave when switch scene (#11627) * set persist node's hideFlags to DontSave when switch scene * refine log --- EngineErrorMap.md | 2 +- cocos/core/director.ts | 4 +++ cocos/core/game.ts | 2 +- native/cocos/core/builtin/DebugInfos.cpp | 2 +- tests/core/node.test.ts | 34 ++++++++++++++++++++++-- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/EngineErrorMap.md b/EngineErrorMap.md index cc43012bd5..faed1c0cd2 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -1623,7 +1623,7 @@ Failed to load prefab asset for node '%s' ### 3800 -The target can not be made persist because it's not a cc.Node or it doesn't have _id property. +The target can not be made persist because it's not a cc.Node or it doesn't have _id property or it's not in the current scene. ### 3801 diff --git a/cocos/core/director.ts b/cocos/core/director.ts index 56b1590323..768ec3caaa 100644 --- a/cocos/core/director.ts +++ b/cocos/core/director.ts @@ -345,9 +345,13 @@ export class Director extends EventTarget { if (existNode) { // scene also contains the persist node, select the old one const index = existNode.getSiblingIndex(); + // restore to the old saving flag + node.hideFlags &= ~CCObject.Flags.DontSave; + node.hideFlags |= CCObject.Flags.DontSave & existNode.hideFlags; existNode._destroyImmediate(); scene.insertChild(node, index); } else { + node.hideFlags |= CCObject.Flags.DontSave; // @ts-expect-error insert to new scene node.parent = scene; } diff --git a/cocos/core/game.ts b/cocos/core/game.ts index d545f4d220..44e375dd90 100644 --- a/cocos/core/game.ts +++ b/cocos/core/game.ts @@ -688,7 +688,7 @@ export class Game extends EventTarget { * @param node - The node to be made persistent */ public addPersistRootNode (node: Node) { - if (!legacyCC.Node.isNode(node) || !node.uuid) { + if (!legacyCC.Node.isNode(node) || !node.uuid || node.scene !== legacyCC.director._scene) { debug.warnID(3800); return; } diff --git a/native/cocos/core/builtin/DebugInfos.cpp b/native/cocos/core/builtin/DebugInfos.cpp index 51a6f32913..492e0a1f8f 100644 --- a/native/cocos/core/builtin/DebugInfos.cpp +++ b/native/cocos/core/builtin/DebugInfos.cpp @@ -161,7 +161,7 @@ ccstd::unordered_map debugInfos = { { 3660, "You are explicitly specifying `undefined` type to cc property '%s' of cc class '%s'.\nIs this intended? If not, this may indicate a circular reference.\nFor example:\n\n \n// foo.ts\nimport { _decorator } from 'cc';\nimport { Bar } from './bar'; // Given that './bar' also reference 'foo.ts'.\n // When importing './bar', execution of './bar' is hung on to wait execution of 'foo.ts',\n // the `Bar` imported here is `undefined` until './bar' finish its execution.\n // It leads to that\n@_decorator.ccclass // ↓\nexport class Foo { // ↓\n @_decorator.type(Bar) // → is equivalent to `@_decorator.type(undefined)`\n public bar: Bar; // To eliminate this error, either:\n // - Refactor your module structure(recommended), or\n // - specify the type as cc class name: `@_decorator.type('Bar'/* or any name you specified for `Bar` */)`\n}" }, { 3700, "internal error: _prefab is undefined" }, { 3701, "Failed to load prefab asset for node '%s'" }, -{ 3800, "The target can not be made persist because it's not a cc.Node or it doesn't have _id property." }, +{ 3800, "The target can not be made persist because it's not a cc.Node or it doesn't have _id property or it's not in the current scene" }, { 3801, "The node can not be made persist because it's not under root node." }, { 3802, "The node can not be made persist because it's not in current scene." }, { 3803, "The target can not be made persist because it's not a cc.Node or it doesn't have _id property." }, diff --git a/tests/core/node.test.ts b/tests/core/node.test.ts index b0980b0426..27b8f2fef7 100644 --- a/tests/core/node.test.ts +++ b/tests/core/node.test.ts @@ -1,6 +1,6 @@ -import { Node, Scene } from "../../cocos/core/scene-graph" +import { find, Node, Scene } from "../../cocos/core/scene-graph" import { Mat4, Quat, Vec3 } from "../../cocos/core/math" -import { Component, director, game } from "../../cocos/core"; +import { CCObject, Component, director, game } from "../../cocos/core"; import { NodeEventType } from "../../cocos/core/scene-graph/node-event"; import { NodeUIProperties } from "../../cocos/core/scene-graph/node-ui-properties"; import { ccclass } from "../../cocos/core/data/decorators"; @@ -226,4 +226,34 @@ describe(`Node`, () => { const testComp = node.addComponent(TestComponent); expect(node.getComponent(BaseComponent)).toBe(testComp); }); + + test('persist node', () => { + const sceneA = new Scene('A'); + director.runSceneImmediate(sceneA); + const nodeA = new Node('test a'); + sceneA.addChild(nodeA); + game.addPersistRootNode(nodeA); + const sceneB = new Scene('B'); + director.runSceneImmediate(sceneB); + expect(find('test a')).toBe(nodeA); + expect(nodeA._persistNode).toBeTruthy(); + expect(nodeA.hideFlags & CCObject.Flags.DontSave).toBeTruthy(); + + const nodeB = new Node('test b'); + game.addPersistRootNode(nodeB); + expect(nodeB._persistNode).toBeFalsy(); + expect(find('test b')).toBeFalsy(); + + const sceneC = new Scene('C'); + // @ts-expect-error access priority + sceneC._id = sceneA.uuid; + const nodeC = new Node('test c'); + // @ts-expect-error access priority + nodeC._id = nodeA.uuid; + sceneC.addChild(nodeC); + director.runSceneImmediate(sceneC); + expect(find('test c')).toBeFalsy(); + expect(find('test a')).toBe(nodeA); + expect(nodeA.hideFlags & CCObject.Flags.DontSave).toBeFalsy(); + }) });