import { director, Node, Component, view, v3, _decorator, UITransform, Vec3, CCFloat, CCInteger } from "cc"; const { ccclass, property ,menu} = _decorator; @ccclass('ScaleNode') @menu("节点适配/宽度适配") export default class ScaleNode extends Component { @property({ type: CCInteger, displayName: "适配模式", tooltip: "0: 宽度适配, 1: 高度适配, 2: 自动适配" }) fitMode: number = 2; protected onLoad(): void { this.updateScale(); } private updateScale(): void { // 获取设计分辨率和可见尺寸 const designSize = view.getDesignResolutionSize(); const visibleSize = view.getVisibleSize(); const designRatio = designSize.width / designSize.height; const screenRatio = visibleSize.width / visibleSize.height; let scale = 1; switch (this.fitMode) { case 0: // 宽度适配 scale = visibleSize.width / designSize.width; break; case 1: // 高度适配 scale = visibleSize.height / designSize.height; break; case 2: // 自动适配(保持宽高比) if (screenRatio > designRatio) { // 屏幕更宽,基于高度缩放 scale = visibleSize.height / designSize.height; } else { // 屏幕更高,基于宽度缩放 scale = visibleSize.width / designSize.width; } break; } this.node.setScale(new Vec3(scale, scale, scale)); } } // import { director, Node, Component, view, v3, _decorator, UITransform } from "cc"; // const { ccclass, property ,menu} = _decorator; // @ccclass('ScaleNode') // @menu("节点适配/宽度适配") // export default class ScaleNode extends Component { // protected onLoad(): void { // let wScale = this.node.getComponent(UITransform).width / view.getVisibleSize().width; // let sc = wScale; // this.node.setScale(v3(sc, sc, sc)) // } // }