AutoSizeCollider.ts 959 Bytes
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class AutoSizeCollider extends cc.Component {

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    protected onEnable(): void {
        this.resize();
    }

    start () {

    }

    // update (dt) {}

    private resize() {
        let widiget = this.node.getComponent(cc.Widget);
        widiget && widiget.updateAlignment();

        let collider = this.node.getComponent(cc.PhysicsBoxCollider);
        if(collider) {
            collider.size.width = this.node.width;
            collider.size.height = this.node.height;
            collider.apply();
        }
    }
}