copy预制体变量名 python3写的
用法 bash variableName.sh Bag.prefab<a class=“attachment” copyProperty.zip (1.2 KB)
打印 复制到 cc.Component脚本内即可
private Bag : cc.Node;
private MainCamera : cc.Node;
private button : cc.Node;
private label : cc.Node;
protected nodePropertys:string[] = [“Bag”,“MainCamera”,“button”,“label”]
cc.Component组件内获取预制变量 this.node.getChildByName(“button”)比较繁琐,
修改后直接 this.button即可
如果node节点存在 label 或 sprite组件 动态创建节点属性Label和Sprite直接指向组件 this.button[‘Label’].string=“123”;
核心代码
private Bag : cc.Node;
private MainCamera : cc.Node;
private button : cc.Node;
private label : cc.Node;
protected nodePropertys:string[] = ["Bag","MainCamera","button","label"]
/**赋值变量属性 */
private assignPropertys(): void {
if (!this.node) {
return;
}
let nodeList: cc.Node[] = [];
let instance
this.nodePropertys && this.nodePropertys.forEach(pro => {
instance = this.node.getChildByName(pro)
if (instance) {
this.checkProperty(pro,instance,nodeList);
} else if (nodeList.length) {
let len = nodeList.length - 1;
let node: cc.Node;
for (var i: number = len; i >= 0; i–) {
node = nodeList[i];
instance = node.getChildByName(pro);
if (instance) {
this.checkProperty(pro,instance,nodeList);
break;
}
}
}
}, this)
}
/**检测 节点上是否存在 图片和文本 组件 */
private checkProperty(pro,instance,nodeList): void {
!!instance.children.length && nodeList.push(instance);
this[pro] = instance
let component
component = instance.getComponent(cc.Sprite);
if(component){
this[pro]['Sprite'] = component;
}else{
component = instance.getComponent(cc.Label);
component && (this[pro]['Label'] = component);
}
}