cocos2d-cli 1.5.1 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,7 @@ const commands = {
11
11
  tree: '../src/commands/tree',
12
12
  get: '../src/commands/get',
13
13
  set: '../src/commands/set',
14
+ 'set-component': '../src/commands/set-component',
14
15
  add: '../src/commands/add',
15
16
  'add-component': '../src/commands/add-component',
16
17
  'remove-component': '../src/commands/remove-component',
@@ -29,42 +30,58 @@ Cocos Creator CLI - 场景/预制体操作工具集
29
30
  cocos2d-cli <command> [options]
30
31
 
31
32
  命令:
32
- tree <场景.fire | 预制体.prefab> 查看节点树
33
- get <场景.fire | 预制体.prefab> <节点路径> 获取节点属性
34
- set <场景.fire | 预制体.prefab> <节点路径> [选项] 修改节点属性
35
- add <场景.fire | 预制体.prefab> <父节点路径> <名称> 添加节点
36
- add-component <文件> <节点路径> <类型> 给节点添加组件
37
- remove-component <文件> <节点路径> <类型> 删除节点组件
38
- remove <文件> <节点路径> 删除节点
39
- build <项目目录> 构建组件映射
40
- create-prefab [JSON文件] <输出.prefab> 创建预制体(不传JSON则创建默认)
41
- create-scene [JSON文件] <输出.fire> 创建场景(不传JSON则创建默认)
33
+ tree <场景.fire | 预制体.prefab> 查看节点树
34
+ get <场景.fire | 预制体.prefab> <节点路径> [属性名|组件类型] 获取节点或组件属性
35
+ set <场景.fire | 预制体.prefab> <节点路径> <属性名> <值> 修改节点属性
36
+ set-component <文件> <节点路径> <组件类型> <属性名> <值> 修改组件属性
37
+ add <场景.fire | 预制体.prefab> <父节点路径> <名称> 添加节点
38
+ add-component <文件> <节点路径> <类型> 给节点添加组件
39
+ remove-component <文件> <节点路径> <类型> 删除节点组件
40
+ remove <文件> <节点路径> 删除节点
41
+ build <项目目录> 构建组件映射
42
+ create-prefab [JSON文件路径] <输出.prefab> 创建预制体(不传JSON则创建默认)
43
+ create-scene [JSON文件路径] <输出.fire> 创建场景(不传JSON则创建默认)
42
44
 
43
45
  节点路径格式:
44
46
  Canvas - 根节点下的 Canvas
45
47
  Canvas/MidNode - Canvas 下的 MidNode
46
48
  Canvas/GameScene/NodeA - 多层嵌套路径
47
49
 
48
- 选项:
49
- --name=<名称> 修改节点名称
50
- --active=true/false 修改激活状态
51
- --x=<数值> 修改 X 坐标
52
- --y=<数值> 修改 Y 坐标
53
- --width=<数值> 修改宽度
54
- --height=<数值> 修改高度
55
- --anchorX=<0-1> 修改锚点 X
56
- --anchorY=<0-1> 修改锚点 Y
57
- --opacity=<0-255> 修改透明度
58
- --color=<#RRGGBB> 修改颜色
59
- --rotation=<角度> 修改旋转角度
60
- --scaleX=<数值> 修改 X 缩放
61
- --scaleY=<数值> 修改 Y 缩放
62
- --string=<文字> 修改 Label 文字内容
63
- --fontSize=<数值> 修改 Label 字体大小
64
- --lineHeight=<数值> 修改 Label 行高
50
+ set 支持的节点属性:
51
+ name 节点名称
52
+ active 激活状态 (true/false)
53
+ x X 坐标
54
+ y Y 坐标
55
+ width 宽度
56
+ height 高度
57
+ anchorX 锚点 X (0-1)
58
+ anchorY 锚点 Y (0-1)
59
+ opacity 透明度 (0-255)
60
+ scaleX X 缩放
61
+ scaleY Y 缩放
62
+ rotation 旋转角度
63
+
64
+ set-component 示例:
65
+ cocos2d-cli set-component xxx.fire Canvas/Label Label string "Hello"
66
+ cocos2d-cli set-component xxx.fire Canvas/Label Label fontSize 32
67
+ cocos2d-cli set-component xxx.fire Canvas/Sprite Sprite sizeMode 0
68
+
69
+ add 支持的选项:
70
+ --x=<数值> 设置 X 坐标
71
+ --y=<数值> 设置 Y 坐标
72
+ --width=<数值> 设置宽度
73
+ --height=<数值> 设置高度
74
+ --scaleX=<数值> 设置 X 缩放
75
+ --scaleY=<数值> 设置 Y 缩放
76
+ --rotation=<角度> 设置旋转角度
77
+ --active=true/false 设置激活状态
65
78
  --type=sprite/label/button 添加节点时指定组件类型
79
+ --string=<文字> Label 文字内容 (配合 --type=label)
80
+ --fontSize=<数值> Label 字体大小 (配合 --type=label)
66
81
 
67
82
  JSON 格式 (create-prefab / create-scene):
83
+ 注意: 参数必须是 JSON 文件路径,不支持直接传递 JSON 字符串
84
+
68
85
  {
69
86
  "name": "节点名称",
70
87
  "width": 400,
@@ -98,14 +115,14 @@ JSON 格式 (create-prefab / create-scene):
98
115
  camera - 相机
99
116
  particle - 粒子效果
100
117
 
101
- 注意:
102
- - color 写在节点或 label 组件均可
103
- - button 需要配合 sprite 才能看见按钮外观
104
-
105
118
  示例:
106
119
  cocos2d-cli tree assets/main.fire
107
120
  cocos2d-cli get assets/main.fire Canvas/MidNode
108
- cocos2d-cli set assets/main.fire Canvas/MidNode --x=100 --y=200 --color=#ff0000
121
+ cocos2d-cli get assets/main.fire Canvas/MidNode x
122
+ cocos2d-cli get assets/main.fire Canvas/MidNode Label
123
+ cocos2d-cli set assets/main.fire Canvas/MidNode x 100
124
+ cocos2d-cli set assets/main.fire Canvas/MidNode width 200
125
+ cocos2d-cli set-component assets/main.fire Canvas/Label Label string "Hello World"
109
126
  cocos2d-cli add assets/main.fire Canvas NewSprite --type=sprite --x=100
110
127
  cocos2d-cli add-component assets/main.fire Canvas/MidNode label
111
128
  cocos2d-cli remove-component assets/main.fire Canvas/MidNode label
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cocos2d-cli",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Command-line tools for AI to read and manipulate Cocos Creator 2.4.x project scenes",
5
5
  "main": "bin/cocos2d-cli.js",
6
6
  "bin": {
@@ -9,10 +9,10 @@ const { CCSceneAsset, CCPrefab } = require('../lib/cc');
9
9
  /**
10
10
  * 查找节点
11
11
  */
12
- function findNode(root, path) {
13
- if (!path) return root;
12
+ function findNode(root, nodePath) {
13
+ if (!nodePath) return root;
14
14
 
15
- const parts = path.split('/').filter(p => p);
15
+ const parts = nodePath.split('/').filter(p => p);
16
16
  if (parts.length === 0) return root;
17
17
 
18
18
  let current = root;
@@ -43,37 +43,15 @@ function findComponent(node, compType) {
43
43
  return node._components.find(c => c.__type__ === typeName);
44
44
  }
45
45
 
46
- /**
47
- * 获取节点属性值
48
- */
49
- function getNodeProp(node, prop) {
50
- switch (prop.toLowerCase()) {
51
- case 'name': return node._name;
52
- case 'active': return node._active;
53
- case 'x': return node._trs.array[0];
54
- case 'y': return node._trs.array[1];
55
- case 'width': return node._contentSize.width;
56
- case 'height': return node._contentSize.height;
57
- case 'scalex': return node._trs.array[7];
58
- case 'scaley': return node._trs.array[8];
59
- case 'rotation': return node._eulerAngles.z;
60
- case 'anchorx': return node._anchorPoint.x;
61
- case 'anchory': return node._anchorPoint.y;
62
- case 'opacity': return node._opacity;
63
- case 'color': return node._color;
64
- default: return undefined;
65
- }
66
- }
67
-
68
46
  function run(args) {
69
- if (args.length < 3) {
70
- console.log(JSON.stringify({ error: '用法: cocos2d-cli get <场景.fire|预制体.prefab> <节点路径> <属性名|组件.属性>' }));
47
+ if (args.length < 2) {
48
+ console.log(JSON.stringify({ error: '用法: cocos2d-cli get <场景.fire|预制体.prefab> <节点路径> [属性名|组件类型]' }));
71
49
  return;
72
50
  }
73
51
 
74
52
  const filePath = args[0];
75
53
  const nodePath = args[1];
76
- const propPath = args[2];
54
+ const propOrComp = args[2];
77
55
 
78
56
  const ext = path.extname(filePath).toLowerCase();
79
57
 
@@ -99,33 +77,27 @@ function run(args) {
99
77
  return;
100
78
  }
101
79
 
102
- // 检查是否是组件属性(格式: 组件.属性)
103
- const parts = propPath.split('.');
80
+ // 没有指定属性或组件,返回节点所有属性
81
+ if (!propOrComp) {
82
+ const props = node.getProp ? node.getProp() : {};
83
+ console.log(JSON.stringify(props, null, 2));
84
+ return;
85
+ }
86
+
87
+ // 检查是否是组件类型
88
+ const comp = findComponent(node, propOrComp);
89
+ if (comp) {
90
+ const props = comp.getProp ? comp.getProp() : {};
91
+ console.log(JSON.stringify(props, null, 2));
92
+ return;
93
+ }
104
94
 
105
- if (parts.length === 2) {
106
- // 组件属性
107
- const comp = findComponent(node, parts[0]);
108
- if (!comp) {
109
- console.log(JSON.stringify({ error: `组件不存在: ${parts[0]}` }));
110
- return;
111
- }
112
-
113
- const value = comp[parts[1]];
114
- if (value === undefined) {
115
- console.log(JSON.stringify({ error: `属性不存在: ${parts[1]}` }));
116
- return;
117
- }
118
-
119
- console.log(JSON.stringify({ value }));
95
+ // 返回节点的单个属性
96
+ const nodeProps = node.getProp ? node.getProp() : {};
97
+ if (nodeProps[propOrComp] !== undefined) {
98
+ console.log(JSON.stringify({ [propOrComp]: nodeProps[propOrComp] }, null, 2));
120
99
  } else {
121
- // 节点属性
122
- const value = getNodeProp(node, propPath);
123
- if (value === undefined) {
124
- console.log(JSON.stringify({ error: `属性不存在: ${propPath}` }));
125
- return;
126
- }
127
-
128
- console.log(JSON.stringify({ value }));
100
+ console.log(JSON.stringify({ error: `属性不存在: ${propOrComp}` }));
129
101
  }
130
102
 
131
103
  } catch (err) {
@@ -0,0 +1,119 @@
1
+ /**
2
+ * set-component 命令 - 设置组件属性
3
+ */
4
+
5
+ const path = require('path');
6
+ const fs = require('fs');
7
+ const { CCSceneAsset, CCPrefab } = require('../lib/cc');
8
+ const { buildTree } = require('../lib/node-utils');
9
+ const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
10
+
11
+ /**
12
+ * 查找节点
13
+ */
14
+ function findNode(root, nodePath) {
15
+ if (!nodePath) return root;
16
+
17
+ const parts = nodePath.split('/').filter(p => p);
18
+ if (parts.length === 0) return root;
19
+
20
+ let current = root;
21
+
22
+ if (parts[0] === root._name) {
23
+ parts.shift();
24
+ }
25
+
26
+ for (const part of parts) {
27
+ if (!current._children || current._children.length === 0) return null;
28
+ const found = current._children.find(c => c._name === part);
29
+ if (!found) return null;
30
+ current = found;
31
+ }
32
+
33
+ return current;
34
+ }
35
+
36
+ /**
37
+ * 查找组件
38
+ */
39
+ function findComponent(node, compType) {
40
+ if (!node._components) return null;
41
+
42
+ const type = compType.toLowerCase();
43
+ const typeName = 'cc.' + type.charAt(0).toUpperCase() + type.slice(1);
44
+
45
+ return node._components.find(c => c.__type__ === typeName);
46
+ }
47
+
48
+ function run(args) {
49
+ if (args.length < 4) {
50
+ console.log(JSON.stringify({
51
+ error: '用法: cocos2d-cli set-component <场景.fire|预制体.prefab> <节点路径> <组件类型> <属性名> <值>'
52
+ }));
53
+ return;
54
+ }
55
+
56
+ const filePath = args[0];
57
+ const nodePath = args[1];
58
+ const compType = args[2];
59
+ const prop = args[3];
60
+ const value = args[4];
61
+
62
+ const ext = path.extname(filePath).toLowerCase();
63
+
64
+ try {
65
+ let root;
66
+ let asset;
67
+ const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
68
+
69
+ if (ext === '.fire') {
70
+ asset = CCSceneAsset.fromJSON(json);
71
+ root = asset._scene;
72
+ } else if (ext === '.prefab') {
73
+ asset = CCPrefab.fromJSON(json);
74
+ root = asset._root;
75
+ } else {
76
+ console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
77
+ return;
78
+ }
79
+
80
+ const node = findNode(root, nodePath);
81
+
82
+ if (!node) {
83
+ console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
84
+ return;
85
+ }
86
+
87
+ const comp = findComponent(node, compType);
88
+
89
+ if (!comp) {
90
+ console.log(JSON.stringify({ error: `组件不存在: ${compType}` }));
91
+ return;
92
+ }
93
+
94
+ // 调用组件的 setProp 方法
95
+ if (typeof comp.setProp !== 'function') {
96
+ console.log(JSON.stringify({ error: '组件不支持 setProp 方法' }));
97
+ return;
98
+ }
99
+
100
+ // 构造属性对象并调用 setProp
101
+ const props = { [prop]: value };
102
+ comp.setProp(props);
103
+
104
+ // 保存
105
+ const data = asset.toJSON();
106
+ fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
107
+
108
+ // 输出节点树
109
+ const scriptMap = loadScriptMap(filePath);
110
+ const prefab = isPrefab(data);
111
+ const startIndex = prefab ? 0 : 1;
112
+ console.log(buildTree(data, scriptMap, startIndex).trim());
113
+
114
+ } catch (err) {
115
+ console.log(JSON.stringify({ error: err.message }));
116
+ }
117
+ }
118
+
119
+ module.exports = { run };
@@ -1,5 +1,5 @@
1
1
  /**
2
- * set 命令 - 设置节点或组件属性
2
+ * set 命令 - 设置节点属性
3
3
  */
4
4
 
5
5
  const path = require('path');
@@ -11,10 +11,10 @@ const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
11
11
  /**
12
12
  * 查找节点
13
13
  */
14
- function findNode(root, path) {
15
- if (!path) return root;
14
+ function findNode(root, nodePath) {
15
+ if (!nodePath) return root;
16
16
 
17
- const parts = path.split('/').filter(p => p);
17
+ const parts = nodePath.split('/').filter(p => p);
18
18
  if (parts.length === 0) return root;
19
19
 
20
20
  let current = root;
@@ -33,51 +33,15 @@ function findNode(root, path) {
33
33
  return current;
34
34
  }
35
35
 
36
- /**
37
- * 查找组件
38
- */
39
- function findComponent(node, compType) {
40
- if (!node._components) return null;
41
-
42
- const type = compType.toLowerCase();
43
- const typeName = 'cc.' + type.charAt(0).toUpperCase() + type.slice(1);
44
-
45
- return node._components.find(c => c.__type__ === typeName);
46
- }
47
-
48
- /**
49
- * 设置节点属性值
50
- */
51
- function setNodeProp(node, prop, value) {
52
- switch (prop.toLowerCase()) {
53
- case 'name': node._name = value; return true;
54
- case 'active': node._active = value === 'true' || value === true; return true;
55
- case 'x': node._trs.array[0] = parseFloat(value); return true;
56
- case 'y': node._trs.array[1] = parseFloat(value); return true;
57
- case 'width': node._contentSize.width = parseFloat(value); return true;
58
- case 'height': node._contentSize.height = parseFloat(value); return true;
59
- case 'scalex': node._trs.array[7] = parseFloat(value); return true;
60
- case 'scaley': node._trs.array[8] = parseFloat(value); return true;
61
- case 'rotation':
62
- node._trs.array[5] = parseFloat(value) * Math.PI / 180;
63
- node._eulerAngles.z = parseFloat(value);
64
- return true;
65
- case 'anchorx': node._anchorPoint.x = parseFloat(value); return true;
66
- case 'anchory': node._anchorPoint.y = parseFloat(value); return true;
67
- case 'opacity': node._opacity = parseInt(value); return true;
68
- default: return false;
69
- }
70
- }
71
-
72
36
  function run(args) {
73
37
  if (args.length < 4) {
74
- console.log(JSON.stringify({ error: '用法: cocos2d-cli set <场景.fire|预制体.prefab> <节点路径> <属性名|组件.属性> <值>' }));
38
+ console.log(JSON.stringify({ error: '用法: cocos2d-cli set <场景.fire|预制体.prefab> <节点路径> <属性名> <值>' }));
75
39
  return;
76
40
  }
77
41
 
78
42
  const filePath = args[0];
79
43
  const nodePath = args[1];
80
- const propPath = args[2];
44
+ const prop = args[2];
81
45
  const value = args[3];
82
46
 
83
47
  const ext = path.extname(filePath).toLowerCase();
@@ -105,31 +69,26 @@ function run(args) {
105
69
  return;
106
70
  }
107
71
 
108
- // 检查是否是组件属性(格式: 组件.属性)
109
- const parts = propPath.split('.');
72
+ // 调用节点的 setProp 方法
73
+ if (typeof node.setProp !== 'function') {
74
+ console.log(JSON.stringify({ error: '节点不支持 setProp 方法' }));
75
+ return;
76
+ }
110
77
 
111
- if (parts.length === 2) {
112
- // 组件属性
113
- const comp = findComponent(node, parts[0]);
114
- if (!comp) {
115
- console.log(JSON.stringify({ error: `组件不存在: ${parts[0]}` }));
116
- return;
117
- }
118
-
119
- if (comp[parts[1]] === undefined) {
120
- console.log(JSON.stringify({ error: `属性不存在: ${parts[1]}` }));
121
- return;
122
- }
123
-
124
- comp[parts[1]] = value;
125
- } else {
126
- // 节点属性
127
- if (!setNodeProp(node, propPath, value)) {
128
- console.log(JSON.stringify({ error: `属性不存在: ${propPath}` }));
129
- return;
130
- }
78
+ // 转换数值类型
79
+ let parsedValue = value;
80
+ if (!isNaN(value) && value !== '') {
81
+ parsedValue = parseFloat(value);
82
+ } else if (value === 'true') {
83
+ parsedValue = true;
84
+ } else if (value === 'false') {
85
+ parsedValue = false;
131
86
  }
132
87
 
88
+ // 构造属性对象并调用 setProp
89
+ const props = { [prop]: parsedValue };
90
+ node.setProp(props);
91
+
133
92
  // 保存
134
93
  const data = asset.toJSON();
135
94
  fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
@@ -36,15 +36,22 @@ class CCComponent extends CCObject {
36
36
  }
37
37
 
38
38
  /**
39
- * 转换为属性面板显示格式(子类重写)
39
+ * 获取属性(子类重写)
40
40
  */
41
- toPanelJSON() {
41
+ getProp() {
42
42
  return {
43
- type: this.__type__,
44
43
  enabled: this._enabled
45
44
  };
46
45
  }
47
46
 
47
+ /**
48
+ * 设置属性(子类重写)
49
+ */
50
+ setProp(props) {
51
+ if (props.enabled !== undefined) this._enabled = props.enabled;
52
+ return this;
53
+ }
54
+
48
55
  toJSON() {
49
56
  return {
50
57
  __type__: this.__type__,
@@ -56,14 +56,13 @@ class CCLabel extends CCComponent {
56
56
  }
57
57
 
58
58
  /**
59
- * 转换为属性面板显示格式
59
+ * 获取属性
60
60
  */
61
- toPanelJSON() {
61
+ getProp() {
62
62
  const H_ALIGN = ['LEFT', 'CENTER', 'RIGHT'];
63
63
  const V_ALIGN = ['TOP', 'CENTER', 'BOTTOM'];
64
64
  const OVERFLOW = ['NONE', 'CLAMP', 'SHRINK', 'RESIZE_HEIGHT'];
65
65
  return {
66
- ...super.toPanelJSON(),
67
66
  string: this._string,
68
67
  fontSize: this._fontSize,
69
68
  lineHeight: this._lineHeight,
@@ -75,6 +74,19 @@ class CCLabel extends CCComponent {
75
74
  };
76
75
  }
77
76
 
77
+ /**
78
+ * 设置属性
79
+ */
80
+ setProp(props) {
81
+ if (props.string !== undefined) this.setString(props.string);
82
+ if (props.fontSize !== undefined) this.setFontSize(props.fontSize);
83
+ if (props.lineHeight !== undefined) this._lineHeight = props.lineHeight;
84
+ if (props.fontFamily !== undefined) this.setFontFamily(props.fontFamily);
85
+ if (props.horizontalAlign !== undefined) this._N$horizontalAlign = props.horizontalAlign;
86
+ if (props.verticalAlign !== undefined) this._N$verticalAlign = props.verticalAlign;
87
+ return this;
88
+ }
89
+
78
90
  toJSON() {
79
91
  return {
80
92
  __type__: this.__type__,
@@ -175,6 +175,51 @@ class CCNode extends CCObject {
175
175
  return this;
176
176
  }
177
177
 
178
+ /**
179
+ * 获取属性
180
+ */
181
+ getProp() {
182
+ const trs = this._trs?.array || [0, 0, 0, 0, 0, 0, 1, 1, 1, 1];
183
+ const result = {
184
+ name: this._name,
185
+ active: this._active,
186
+ x: trs[0],
187
+ y: trs[1],
188
+ width: this._contentSize?.width ?? 0,
189
+ height: this._contentSize?.height ?? 0,
190
+ anchorX: this._anchorPoint?.x ?? 0.5,
191
+ anchorY: this._anchorPoint?.y ?? 0.5,
192
+ scaleX: trs[7],
193
+ scaleY: trs[8],
194
+ rotation: this._eulerAngles?.z ?? 0,
195
+ opacity: this._opacity ?? 255,
196
+ color: this._color ? `#${this._color.r.toString(16).padStart(2,'0')}${this._color.g.toString(16).padStart(2,'0')}${this._color.b.toString(16).padStart(2,'0')}` : '#ffffff'
197
+ };
198
+ return result;
199
+ }
200
+
201
+ /**
202
+ * 设置属性
203
+ */
204
+ setProp(props) {
205
+ if (props.name !== undefined) this._name = props.name;
206
+ if (props.active !== undefined) this._active = props.active;
207
+ if (props.x !== undefined) this._trs.array[0] = props.x;
208
+ if (props.y !== undefined) this._trs.array[1] = props.y;
209
+ if (props.width !== undefined) this._contentSize.width = props.width;
210
+ if (props.height !== undefined) this._contentSize.height = props.height;
211
+ if (props.anchorX !== undefined) this._anchorPoint.x = props.anchorX;
212
+ if (props.anchorY !== undefined) this._anchorPoint.y = props.anchorY;
213
+ if (props.scaleX !== undefined) this._trs.array[7] = props.scaleX;
214
+ if (props.scaleY !== undefined) this._trs.array[8] = props.scaleY;
215
+ if (props.rotation !== undefined) {
216
+ this._trs.array[5] = props.rotation * Math.PI / 180;
217
+ this._eulerAngles.z = props.rotation;
218
+ }
219
+ if (props.opacity !== undefined) this._opacity = props.opacity;
220
+ return this;
221
+ }
222
+
178
223
  /**
179
224
  * 转换为属性面板显示格式(人类可读)
180
225
  */
@@ -199,7 +244,7 @@ class CCNode extends CCObject {
199
244
  result.components = {};
200
245
  this._components.forEach(c => {
201
246
  const typeName = c.__type__.replace('cc.', '');
202
- result.components[typeName] = c.toPanelJSON ? c.toPanelJSON() : {};
247
+ result.components[typeName] = c.getProp ? c.getProp() : {};
203
248
  });
204
249
  }
205
250
 
@@ -1,6 +1,12 @@
1
1
  const CCObject = require('./CCObject');
2
2
  const CCScene = require('./CCScene');
3
3
  const CCNode = require('./CCNode');
4
+ const CCCanvas = require('./CCCanvas');
5
+ const CCWidget = require('./CCWidget');
6
+ const CCSprite = require('./CCSprite');
7
+ const CCLabel = require('./CCLabel');
8
+ const CCButton = require('./CCButton');
9
+ const CCCamera = require('./CCCamera');
4
10
  const { generateCompressedUUID } = require('../utils');
5
11
 
6
12
  /**
@@ -230,13 +236,39 @@ function createObject(item) {
230
236
  return node;
231
237
  }
232
238
 
233
- // 组件
234
- const comp = { __type__: type };
239
+ // 组件 - 创建类实例
240
+ const comp = createComponentInstance(type);
241
+ if (comp) {
242
+ copyComponentProps(comp, item);
243
+ return comp;
244
+ }
245
+
246
+ // 未知类型,返回普通对象
247
+ const obj = { __type__: type };
248
+ for (const key of Object.keys(item)) {
249
+ obj[key] = item[key];
250
+ }
251
+ return obj;
252
+ }
253
+
254
+ function createComponentInstance(type) {
255
+ switch (type) {
256
+ case 'cc.Canvas': return new CCCanvas();
257
+ case 'cc.Widget': return new CCWidget();
258
+ case 'cc.Sprite': return new CCSprite();
259
+ case 'cc.Label': return new CCLabel();
260
+ case 'cc.Button': return new CCButton();
261
+ case 'cc.Camera': return new CCCamera();
262
+ default: return null;
263
+ }
264
+ }
265
+
266
+ function copyComponentProps(comp, item) {
235
267
  for (const key of Object.keys(item)) {
236
268
  if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
237
269
  comp[key] = item[key];
238
270
  }
239
- return comp;
271
+ if (item._id) comp._id = item._id;
240
272
  }
241
273
 
242
274
  function copyNodeProps(node, item) {
@@ -42,19 +42,28 @@ class CCSprite extends CCComponent {
42
42
  }
43
43
 
44
44
  /**
45
- * 转换为属性面板显示格式
45
+ * 获取属性
46
46
  */
47
- toPanelJSON() {
47
+ getProp() {
48
48
  const SIZE_MODE = ['CUSTOM', 'RAW', 'TRIMMED'];
49
49
  const SPRITE_TYPE = ['SIMPLE', 'SLICED', 'TILED', 'FILLED', 'MESH'];
50
50
  return {
51
- ...super.toPanelJSON(),
52
51
  sizeMode: SIZE_MODE[this._sizeMode] || this._sizeMode,
53
52
  type: SPRITE_TYPE[this._type] || this._type,
54
53
  trim: this._isTrimmedMode
55
54
  };
56
55
  }
57
56
 
57
+ /**
58
+ * 设置属性
59
+ */
60
+ setProp(props) {
61
+ if (props.sizeMode !== undefined) this.setSizeMode(props.sizeMode);
62
+ if (props.spriteFrame !== undefined) this.setSpriteFrame(props.spriteFrame);
63
+ if (props.type !== undefined) this._type = props.type;
64
+ return this;
65
+ }
66
+
58
67
  toJSON() {
59
68
  return {
60
69
  __type__: this.__type__,