mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 15:42:39 +08:00
set persist node's hideFlags to DontSave when switch scene (#11627)
* set persist node's hideFlags to DontSave when switch scene * refine log
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+1
-1
@@ -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;
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ ccstd::unordered_map<int, ccstd::string> 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." },
|
||||
|
||||
+32
-2
@@ -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();
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user