自用的编辑器吸附脚本,分享给大家。
把此脚本拖到想要自动吸附的物体上,在编辑器里拖动即可实现自动吸附~
import { _decorator, Component, Node } from 'cc';
const { ccclass, property, executeInEditMode } = _decorator;
@ccclass
@executeInEditMode
export class SnapInEditor extends Component {
@property
intervalX = 1.0;
@property
intervalY = 1.0;
@property
intervalZ = 1.0;
@property
offsetX = 0;
@property
offsetY = 0;
@property
offsetZ = 0;
update() {
if (CC_EDITOR) {
let newX = Math.round((this.node.position.x - this.offsetX) / this.intervalX) * this.intervalX + this.offsetX;
let newY = Math.round((this.node.position.y - this.offsetY) / this.intervalY) * this.intervalY + this.offsetY;
let newZ = Math.round((this.node.position.z - this.offsetZ) / this.intervalZ) * this.intervalZ + this.offsetZ;
if (isFinite(newX) && isFinite(newY) && isFinite(newZ)) {
this.node.setPosition(newX, newY, newZ);
}
}
}
}
