调用
this.creatLayout(5, 5, this.boxNode, this.rootNode)
//限制数量
this.creatLayout(5, 5, this.boxNode, this.rootNode, cc.v3(0, 0), 10, 10, 23)
/**
* 创建格子布局
* @param xCount 列数量
* @param yCount 行数量
* @param prefab 预制体
* @param root 根节点
* @param rootPos 根节点坐标(偏移量)
* @param spacingX 横间隔
* @param spacingY 竖间隔
* @param totalCount 总数量
* @returns
*/
creatLayout(xCount, yCount, prefab, root, rootPos = cc.v3(0, 0), spacingX = 10, spacingY = 10, totalCount = 0) {
if (root.children.length != totalCount) {
root.children.forEach(element => {
element.active = false;
});
}
let scale = 1
let oPos = cc.v3(rootPos.x - (prefab.width * prefab.scale * (xCount - 1) * scale + spacingX * (xCount + 1)) / 2, rootPos.y - (prefab.height * prefab.scale * (yCount - 1) + spacingY * (yCount + 1)) / 2)
let count = 0;
for (let j = yCount - 1; j >= 0; j--) {
for (let i = 0; i < xCount; i++) {
if (totalCount && count >= totalCount) return
let node: cc.Node = root.children[count] || cc.instantiate(prefab);
if (!node) continue;
node.parent = root;
node.width = prefab.width;
node.height = prefab.height;
//表格左上角第一个为1_1
node.name = `${yCount - j}_${i + 1}`;
node.x = oPos.x + prefab.width * prefab.scale * (i) + spacingX * (i + 1);
node.y = oPos.y + prefab.height * prefab.scale * (j) + spacingY * (j + 1);
node.active = true
count++
}
}
}