diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 5234171380..bec07978b3 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1275,10 +1275,6 @@ var Node = cc.Class({ _onBatchCreated () { this._upgrade_1x_to_2x(); - if (!this._id) { - this._id = CC_EDITOR ? Editor.Utils.UuidUtils.uuid() : BaseNode.idGenerater.getNewId(); - } - this._updateOrderOfArrival(); let prefabInfo = this._prefab; @@ -1312,10 +1308,6 @@ var Node = cc.Class({ _onBatchRestored () { this._upgrade_1x_to_2x(); - if (!this._id) { - this._id = CC_EDITOR ? Editor.Utils.UuidUtils.uuid() : BaseNode.idGenerater.getNewId(); - } - if (!this._activeInHierarchy) { // deactivate ActionManager and EventManager by default if (ActionManagerExist) { diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index c95728c27e..bbe0c3b528 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -54,9 +54,6 @@ var Component = cc.Class({ _Scene.AssetsWatcher.initComponent(this); } this._id = Editor.Utils.UuidUtils.uuid(); - if (cc.engine) { - cc.engine.attachedObjsForEditor[this._id] = this; - } /** * Register all related EventTargets, @@ -67,9 +64,7 @@ var Component = cc.Class({ this.__eventTargets = []; } : function () { this._id = idGenerater.getNewId(); - if (CC_TEST && cc.engine) { - cc.engine.attachedObjsForEditor[this._id] = this; - } + this.__eventTargets = []; }, @@ -508,12 +503,6 @@ var Component = cc.Class({ // do remove component this.node._removeComponent(this); - - if (CC_EDITOR || CC_TEST) { - if (cc.engine.attachedObjsForEditor[this._id] === this) { // temporarily fix - delete cc.engine.attachedObjsForEditor[this._id]; - } - } }, _instantiate (cloned) { diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index 0f38367356..00f8f4624d 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -570,26 +570,15 @@ var _Deserializer = (function () { sources.push('}'); } if (cc.js.isChildClassOf(klass, cc._BaseNode) || cc.js.isChildClassOf(klass, cc.Component)) { - let overwriteId = false; if (CC_PREVIEW || (CC_EDITOR && self._ignoreEditorOnly)) { var mayUsedInPersistRoot = js.isChildClassOf(klass, cc.Node); - overwriteId = mayUsedInPersistRoot; - } - else { - overwriteId = true; - } - if (overwriteId) { - if (CC_EDITOR && cc.engine) { - sources.push('if (d._id) {' + - 'delete cc.engine.attachedObjsForEditor[o._id];' + - 'o._id=d._id;' + - 'cc.engine.attachedObjsForEditor[d._id] = o;' + - '}'); - } - else { + if (mayUsedInPersistRoot) { sources.push('d._id&&(o._id=d._id);'); } } + else { + sources.push('d._id&&(o._id=d._id);'); + } } if (props[props.length - 1] === '_$erialized') { // deep copy original serialized data diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 82c660bb56..ff1df85ed3 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -949,7 +949,9 @@ var BaseNode = cc.Class({ var component = new constructor(); component.node = this; this._components.push(component); - + if ((CC_EDITOR || CC_TEST) && cc.engine && (this._id in cc.engine.attachedObjsForEditor)) { + cc.engine.attachedObjsForEditor[component._id] = component; + } if (this._activeInHierarchy) { cc.director._nodeActivator.activateComp(component); } @@ -997,7 +999,9 @@ var BaseNode = cc.Class({ comp.node = this; this._components.splice(index, 0, comp); - + if ((CC_EDITOR || CC_TEST) && cc.engine && (this._id in cc.engine.attachedObjsForEditor)) { + cc.engine.attachedObjsForEditor[comp._id] = comp; + } if (this._activeInHierarchy) { cc.director._nodeActivator.activateComp(comp); } @@ -1061,6 +1065,9 @@ var BaseNode = cc.Class({ var i = this._components.indexOf(component); if (i !== -1) { this._components.splice(i, 1); + if ((CC_EDITOR || CC_TEST) && cc.engine) { + delete cc.engine.attachedObjsForEditor[component._id]; + } } else if (component.node !== this) { cc.errorID(3815); @@ -1196,17 +1203,25 @@ var BaseNode = cc.Class({ }, _registerIfAttached: (CC_EDITOR || CC_TEST) && function (register) { + var attachedObjsForEditor = cc.engine.attachedObjsForEditor; if (register) { - cc.engine.attachedObjsForEditor[this.uuid] = this; + attachedObjsForEditor[this._id] = this; + for (let i = 0; i < this._components.length; i++) { + let comp = this._components[i]; + attachedObjsForEditor[comp._id] = comp; + } cc.engine.emit('node-attach-to-scene', {target: this}); - //this._objFlags |= RegisteredInEditor; } else { cc.engine.emit('node-detach-from-scene', {target: this}); - delete cc.engine.attachedObjsForEditor[this._id]; + delete attachedObjsForEditor[this._id]; + for (let i = 0; i < this._components.length; i++) { + let comp = this._components[i]; + delete attachedObjsForEditor[comp._id]; + } } var children = this._children; - for (var i = 0, len = children.length; i < len; ++i) { + for (let i = 0, len = children.length; i < len; ++i) { var child = children[i]; child._registerIfAttached(register); }