From d313f2828f7903362e6b77670d0b7210f08e4e73 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Fri, 18 Aug 2017 18:35:58 +0800 Subject: [PATCH] should load position of node when synced with prefab --- cocos2d/core/platform/instantiate-jit.js | 2 +- cocos2d/core/platform/instantiate.js | 2 +- cocos2d/core/utils/prefab-helper.js | 6 ++- test/qunit/unit-es5/test-instantiate.js | 1 + test/qunit/unit-es5/test-prefab-sync.js | 61 +++++++++++++----------- 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/cocos2d/core/platform/instantiate-jit.js b/cocos2d/core/platform/instantiate-jit.js index f5a19bc8b9..1d51385adb 100644 --- a/cocos2d/core/platform/instantiate-jit.js +++ b/cocos2d/core/platform/instantiate-jit.js @@ -311,7 +311,7 @@ proto.enumerateCCClass = function (codeArray, obj, klass) { var attrs = Attr.getClassAttrs(klass); for (var p = 0; p < props.length; p++) { var key = props[p]; - if (CC_EDITOR && key === '_id') { + if ((CC_EDITOR || CC_TEST) && key === '_id') { if (obj instanceof cc._BaseNode || obj instanceof cc.Component) { continue; } diff --git a/cocos2d/core/platform/instantiate.js b/cocos2d/core/platform/instantiate.js index 95a6341bf6..5d58e7eb4a 100644 --- a/cocos2d/core/platform/instantiate.js +++ b/cocos2d/core/platform/instantiate.js @@ -171,7 +171,7 @@ function enumerateCCClass (klass, obj, clone, parent) { } } } - if (CC_EDITOR && (obj instanceof cc._BaseNode || obj instanceof cc.Component)) { + if ((CC_EDITOR || CC_TEST) && (obj instanceof cc._BaseNode || obj instanceof cc.Component)) { clone._id = ''; } } diff --git a/cocos2d/core/utils/prefab-helper.js b/cocos2d/core/utils/prefab-helper.js index 74c21f8d21..13b53239fe 100644 --- a/cocos2d/core/utils/prefab-helper.js +++ b/cocos2d/core/utils/prefab-helper.js @@ -72,7 +72,8 @@ module.exports = { var _id = node._id; var _name = node._name; var _active = node._active; - var _position = node._position; + var x = node._position.x; + var y = node._position.y; var _rotationX = node._rotationX; var _rotationY = node._rotationY; var _localZOrder = node._localZOrder; @@ -90,7 +91,8 @@ module.exports = { node._prefab = _prefab; node._name = _name; node._active = _active; - node._position = _position; + node._position.x = x; + node._position.y = y; node._rotationX = _rotationX; node._rotationY = _rotationY; node._localZOrder = _localZOrder; diff --git a/test/qunit/unit-es5/test-instantiate.js b/test/qunit/unit-es5/test-instantiate.js index 018e622c29..85a1d888f6 100644 --- a/test/qunit/unit-es5/test-instantiate.js +++ b/test/qunit/unit-es5/test-instantiate.js @@ -163,6 +163,7 @@ function testInstantiate (module, match, instantiate) { var clone = instantiate(node); ok(clone.parent === null, 'root of cloned node should not have parent'); + ok(clone.uuid !== node.uuid, 'should not clone uuid in tests'); ok(clone.children[0].parent === clone, 'cloned child node should have parent'); }); diff --git a/test/qunit/unit-es5/test-prefab-sync.js b/test/qunit/unit-es5/test-prefab-sync.js index ae2d8722fe..9f076a7960 100644 --- a/test/qunit/unit-es5/test-prefab-sync.js +++ b/test/qunit/unit-es5/test-prefab-sync.js @@ -15,37 +15,42 @@ var UUID = '19941122'; var DEBUG_SERIALIZED_PREFAB = 0 && !isPhantomJS; - var parent = new cc.Node('parent'); - parent.addComponent(MyComponent); - parent.scale = cc.v2(123, 432); - parent.x = 1020; - parent.active = false; + var nodeToCreatePrefab = new cc.Node('parent'); + nodeToCreatePrefab.addComponent(MyComponent); + nodeToCreatePrefab.scale = cc.v2(123, 432); + nodeToCreatePrefab.x = 1020; + nodeToCreatePrefab.active = false; var child = new cc.Node('child'); child.addComponent(MyComponent); child.scale = cc.v2(3, 2); - child.parent = parent; + child.parent = nodeToCreatePrefab; child.y = 1029; child.active = false; var prefabAsset; var prefabJson; var parentJson; + var otherSyncedNode; var reloadedParent; (function beforeAll () { // CREATE PREFAB ASSET - _Scene.PrefabUtils.createPrefabFrom(parent); + _Scene.PrefabUtils.createPrefabFrom(nodeToCreatePrefab); //var asset = new cc.Prefab(); //asset.data = parent; - parent._prefab.asset._uuid = UUID; + nodeToCreatePrefab._prefab.asset._uuid = UUID; //_Scene.PrefabUtils.setPrefabAsset(parent, Editor.serialize.asAsset()); - _Scene.PrefabUtils.setPrefabSync(parent, true); + _Scene.PrefabUtils.setPrefabSync(nodeToCreatePrefab, true); + + otherSyncedNode = cc.instantiate(nodeToCreatePrefab); + otherSyncedNode.x = 1234; + otherSyncedNode.name = 'otherSyncedNode'; // apply sync property - prefabAsset = _Scene.PrefabUtils.createAppliedPrefab(parent); + prefabAsset = _Scene.PrefabUtils.createAppliedPrefab(nodeToCreatePrefab); // 重新生成已经加载好的 prefab,去除类型,去除 runtime node prefabJson = Editor.serialize(prefabAsset, { stringify: false }); @@ -53,8 +58,7 @@ prefabAsset._uuid = UUID; // SAVE SCENE ASSET - - parentJson = Editor.serialize(parent, { stringify: false }); + parentJson = Editor.serialize(otherSyncedNode, { stringify: false }); if (DEBUG_SERIALIZED_PREFAB) { console.log(JSON.stringify(parentJson, null, 4)); } @@ -64,14 +68,14 @@ })(); test('prefab info', function () { - strictEqual(parent._prefab.sync, true, "sync property should be saved to root prefab info"); + strictEqual(nodeToCreatePrefab._prefab.sync, true, "sync property should be saved to root prefab info"); }); test('prefab asset should not discard content if sync enabled', function () { var nodeToSave = prefabAsset.data; ok(nodeToSave.children.length > 0, 'children should be saved'); var childToSave = nodeToSave.children[0]; - strictEqual(nodeToSave.scaleX, parent.scaleX, 'root scale should be saved'); + strictEqual(nodeToSave.scaleX, nodeToCreatePrefab.scaleX, 'root scale should be saved'); strictEqual(childToSave.scaleY, child.scaleY, 'child scale should be saved'); ok(nodeToSave.getComponent(cc.Component), 'root component should be saved'); ok(childToSave.getComponent(cc.Component), 'child component should be saved'); @@ -80,7 +84,7 @@ strictEqual(childToSave.active, false, 'child active should be saved'); }); - test('sync config saved in prefab asset', function () { + test('sync config in prefab asset', function () { var nodeToSave = prefabAsset.data; strictEqual(nodeToSave._prefab.sync, false, 'sync config should not saved'); }); @@ -88,23 +92,24 @@ test('saved scene node if sync enabled', function () { strictEqual(reloadedParent.scaleX, 1, 'all scale should not saved'); strictEqual(reloadedParent.getComponent(cc.Component), null, 'all component should not saved'); - strictEqual(reloadedParent.x, parent.x, 'root position should be saved'); - strictEqual(reloadedParent.name, parent.name, 'root name should be saved'); - strictEqual(reloadedParent.active, false, 'root active should be saved'); ok(reloadedParent.children.length === 0, 'children should not saved'); + strictEqual(reloadedParent.x, otherSyncedNode.x, 'root position should be saved'); + strictEqual(reloadedParent.name, otherSyncedNode.name, 'root name should be saved'); + strictEqual(reloadedParent.active, otherSyncedNode.active, 'root active should be saved'); + var prefabInfo = reloadedParent._prefab; ok(prefabInfo, 'prefab info should be saved'); ok(prefabInfo.asset, 'asset should be saved'); strictEqual(prefabInfo.sync, true, 'sync should be saved'); - strictEqual(prefabInfo.fileId, parent.uuid, 'fileId should be saved'); + strictEqual(prefabInfo.fileId, nodeToCreatePrefab.uuid, 'fileId should be saved'); }); test('prefab info in exported scene if syncable', function () { - var reloadedParent = cc.deserialize(Editor.serialize(parent, { stringify: false, exporting: true })); - ok(reloadedParent._prefab, 'prefab info should be saved'); - strictEqual(reloadedParent._prefab.sync, true, 'sync should be saved'); - strictEqual(reloadedParent._prefab.fileId, parent.uuid, 'fileId should be saved'); + var exportedParent = cc.deserialize(Editor.serialize(nodeToCreatePrefab, { stringify: false, exporting: true })); + ok(exportedParent._prefab, 'prefab info should be saved'); + strictEqual(exportedParent._prefab.sync, true, 'sync should be saved'); + strictEqual(exportedParent._prefab.fileId, nodeToCreatePrefab.uuid, 'fileId should be saved'); }); function testInstantiatedNode (newNode) { @@ -119,7 +124,7 @@ testInstantiatedNode(newNode); }); test('instantiating synced scene node', function () { - var newNode = cc.instantiate(parent); + var newNode = cc.instantiate(nodeToCreatePrefab); testInstantiatedNode(newNode); ok(newNode._prefab.sync === true, "sync should be instantiated"); ok(newNode._prefab._synced === true, "instantiated node should be _synced"); @@ -140,15 +145,15 @@ ok(prefabInfo, "new node should preserve the prefab info"); ok(prefabInfo.sync === true, "sync should be instantiated"); ok(prefabInfo._synced === true, "instantiated node should be _synced"); - strictEqual(prefabInfo.fileId, parent.uuid, 'fileId should be kept'); + strictEqual(prefabInfo.fileId, nodeToCreatePrefab.uuid, 'fileId should be kept'); // test parent - strictEqual(reloadedParent.scaleX, parent.scaleX, 'all scale should be synced'); + strictEqual(reloadedParent.scaleX, nodeToCreatePrefab.scaleX, 'all scale should be synced'); var comp = reloadedParent.getComponent(cc.Component); ok(comp, 'all component should be synced'); strictEqual(comp.node, reloadedParent, 'component reference should redirect to scene node'); - strictEqual(reloadedParent.x, parent.x, 'root position should be kept'); - strictEqual(reloadedParent.name, parent.name, 'root name should be kept'); + strictEqual(reloadedParent.x, otherSyncedNode.x, 'root position should be kept'); + strictEqual(reloadedParent.name, otherSyncedNode.name, 'root name should be kept'); strictEqual(reloadedParent.active, false, 'root active should be kept'); strictEqual(reloadedParent.children.length, 1, 'children should be synced');