diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index fbc77b8cfc..35006eadb0 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1061,11 +1061,12 @@ var Node = cc.Class({ */ _onBatchCreated () { var prefabInfo = this._prefab; - if (prefabInfo && prefabInfo.sync && !prefabInfo._synced) { - // checks to ensure no recursion, recursion will caused only on old data. - if (prefabInfo.root === this) { - PrefabHelper.syncWithPrefab(this); + if (prefabInfo && prefabInfo.sync && prefabInfo.root === this) { + if (CC_DEV) { + // TODO - remove all usage of _synced + cc.assert(!prefabInfo._synced, 'prefab should not synced'); } + PrefabHelper.syncWithPrefab(this); } this._updateDummySgNode(); @@ -1088,6 +1089,28 @@ var Node = cc.Class({ } }, + // the same as _onBatchCreated but untouch prefab + _onBatchRestored () { + this._updateDummySgNode(); + + if (this._parent) { + this._parent._sgNode.addChild(this._sgNode); + } + + if (!this._activeInHierarchy) { + // deactivate ActionManager and EventManager by default + if (ActionManagerExist) { + cc.director.getActionManager().pauseTarget(this); + } + cc.eventManager.pauseTarget(this); + } + + var children = this._children; + for (var i = 0, len = children.length; i < len; i++) { + children[i]._onBatchRestored(); + } + }, + // EVENTS /** diff --git a/cocos2d/core/CCScene.js b/cocos2d/core/CCScene.js index 6c044f80f3..a3a76e2d57 100644 --- a/cocos2d/core/CCScene.js +++ b/cocos2d/core/CCScene.js @@ -62,6 +62,10 @@ cc.Scene = cc.Class({ this._activeInHierarchy = false; this._inited = !cc.game._isCloning; + if (CC_EDITOR) { + this._prefabSyncedInLiveReload = false; + } + // cache all depend assets for auto release this.dependAssets = null; }, @@ -79,7 +83,12 @@ cc.Scene = cc.Class({ if (CC_TEST) { cc.assert(!this._activeInHierarchy, 'Should deactivate ActionManager and EventManager by default'); } - this._onBatchCreated(); + if (CC_EDITOR && this._prefabSyncedInLiveReload) { + this._onBatchRestored(); + } + else { + this._onBatchCreated(); + } this._inited = true; } }, diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index d60fffff61..e4acdfb5f2 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -23,6 +23,7 @@ THE SOFTWARE. ****************************************************************************/ +var PrefabHelper = require('./prefab-helper'); var Flags = require('../platform/CCObject').Flags; var Misc = require('./misc'); var IdGenerater = require('../platform/id-generater'); @@ -1055,6 +1056,12 @@ var BaseNode = cc.Class({ _onSetParent (value) {}, _onPostActivated () {}, + _onBatchRestored () { + var children = this._children; + for (var i = 0, len = children.length; i < len; i++) { + children[i]._onBatchRestored(); + } + }, _onHierarchyChanged (oldParent) { var newParent = this._parent; @@ -1107,11 +1114,11 @@ var BaseNode = cc.Class({ _onBatchCreated () { var prefabInfo = this._prefab; - if (prefabInfo && prefabInfo.sync && !prefabInfo._synced) { - // checks to ensure no recursion, recursion will caused only on old data. - if (prefabInfo.root === this) { - PrefabHelper.syncWithPrefab(this); + if (prefabInfo && prefabInfo.sync && prefabInfo.root === this) { + if (CC_DEV && prefabInfo._synced) { + cc.error('Internal error: prefab already synced'); } + PrefabHelper.syncWithPrefab(this); } var children = this._children; @@ -1133,8 +1140,6 @@ var BaseNode = cc.Class({ } var syncing = thisPrefabInfo && this === thisPrefabInfo.root && thisPrefabInfo.sync; if (syncing) { - // copy non-serialized property - cloned._prefab._synced = thisPrefabInfo._synced; //if (thisPrefabInfo._synced) { // return clone; //} @@ -1145,7 +1150,7 @@ var BaseNode = cc.Class({ // reset and init cloned._parent = null; - cloned._onBatchCreated(); + cloned._onBatchRestored(); return cloned; }, diff --git a/cocos2d/core/utils/prefab-helper.js b/cocos2d/core/utils/prefab-helper.js index 9ee8248e5e..a2086f361d 100644 --- a/cocos2d/core/utils/prefab-helper.js +++ b/cocos2d/core/utils/prefab-helper.js @@ -25,11 +25,13 @@ cc._PrefabInfo = cc.Class({ name: 'cc.PrefabInfo', + // extends: require('../platform/CCObject'), properties: { // the most top node of this prefab in the scene root: null, // 所属的 prefab 资源对象 (cc.Prefab) + // In Editor, only asset._uuid is usable because asset will be changed. asset: null, // 用来标识别该节点在 prefab 资源中的位置,因此这个 ID 只需要保证在 Assets 里不重复就行 @@ -43,7 +45,18 @@ cc._PrefabInfo = cc.Class({ default: false, serializable: false }, - } + }, + // _instantiate (cloned) { + // if (!cloned) { + // cloned = new cc._PrefabInfo(); + // } + // cloned.root = this.root; + // cloned.asset = this.asset; + // cloned.fileId = this.fileId; + // cloned.sync = this.sync; + // cloned._synced = this._synced; + // return cloned; + // } }); // prefab helper function diff --git a/test/qunit/unit-es5/test-prefab-sync.js b/test/qunit/unit-es5/test-prefab-sync.js index 9f076a7960..801476d987 100644 --- a/test/qunit/unit-es5/test-prefab-sync.js +++ b/test/qunit/unit-es5/test-prefab-sync.js @@ -127,7 +127,7 @@ 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"); + // ok(newNode._prefab._synced === true, "instantiated node should be _synced"); }); test('load and sync scene', function () {