// cc.Class({ // extends: cc.Component, // properties: { // }, // // LIFE-CYCLE CALLBACKS: // onLoad: function () { // if (cc.sys.platform == cc.sys.MOBILE_BROWSER) { // var s1 = cc.view.getDesignResolutionSize(); // var s2 = cc.winSize; // var scale = s2.width / s1.width; // // this.node.scaleX = scale; // this.node.scale = scale; // } // }, // }); cc.Class({ extends: cc.Component, properties: { // 最小缩放比例 minScale: { default: 0.8, tooltip: "最小缩放比例" }, // 最大缩放比例 maxScale: { default: 1, tooltip: "最大缩放比例" }, // 是否启用高度适配 enableHeightAdapt: { default: true, tooltip: "是否启用高度适配" } }, onLoad: function () { this.adaptToScreen(); // 监听屏幕尺寸变化 cc.view.on('canvas-resize', this.adaptToScreen, this); }, onDestroy: function () { cc.view.off('canvas-resize', this.adaptToScreen, this); }, adaptToScreen: function () { if (cc.sys.platform == cc.sys.MOBILE_BROWSER || cc.sys.os === cc.sys.OS_IOS || cc.sys.os === cc.sys.OS_ANDROID) { var designSize = cc.view.getDesignResolutionSize(); var winSize = cc.winSize; // 计算缩放比例(考虑宽度和高度) var scaleX = winSize.width / designSize.width; var scaleY = this.enableHeightAdapt ? winSize.height / designSize.height : scaleX; // 使用较小的比例以确保内容完全显示 var scale = Math.min(scaleX, scaleY); // 限制缩放范围 scale = Math.max(this.minScale, Math.min(this.maxScale, scale)); this.node.scale = scale; } } });