register component to scene after ctor (#2661)

This commit is contained in:
Jare Guo
2018-05-10 17:00:59 +08:00
committed by pandamicro
parent b7ce3032ba
commit ebf7d7d730
4 changed files with 26 additions and 41 deletions
-8
View File
@@ -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) {
+1 -12
View File
@@ -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) {
+4 -15
View File
@@ -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
+21 -6
View File
@@ -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);
}