1 Star 2 Fork 1

xyTuoren/cocos2.4Tool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ScreenAdaptMgr.ts 5.76 KB
一键复制 编辑 原始数据 按行查看 历史
xyTuoren 提交于 2023-04-25 18:32 . 修改屏幕背景适配代码
export default class ScreenAdaptMgr {
private static isMobile: boolean = false;
private static isTablet: boolean = false;
private static isPc: boolean = false;
public static initAdapt() {
this.adapt()
cc.director.on(cc.Director.EVENT_AFTER_SCENE_LAUNCH, () => {
this.adapt()
})
}
/**适配屏幕 */
public static adapt() {
let bgNode = this.getBgNode(cc.Canvas.instance.node);
//当前屏幕实际大小 cc.winSize(W为Fit适配缩放后的大小)
let frameSize = cc.view.getFrameSize();
let screenRatio = frameSize.width / frameSize.height;
//设计比例
let designRatio = cc.Canvas.instance.designResolution.width / cc.Canvas.instance.designResolution.height;
//判断屏幕宽高比
if (screenRatio <= 1) {
//此屏幕高度大于宽度
if (screenRatio <= designRatio) {
this.setFitWidth();
bgNode.scaleY = cc.winSize.height / bgNode.height
} else {
//此时屏幕比例大于设计比例
//为了保证纵向的游戏内容不受影响,应该使用fitHeight模式
this.setFitHeight();
bgNode.scaleX = cc.winSize.width / bgNode.width;
}
} else {
this.setFitWidth();
bgNode.scaleY = cc.winSize.height / bgNode.height
}
}
private static setFitHeight() {
cc.Canvas.instance.fitHeight = true;
cc.Canvas.instance.fitWidth = false;
}
private static setFitWidth() {
cc.Canvas.instance.fitHeight = false;
cc.Canvas.instance.fitWidth = true;
}
/**返回上层的bg节点 */
private static getBgNode(preNode: cc.Node): cc.Node {
let name = preNode.name.toLocaleLowerCase()
if (name === 'bg' || name === 'background') {
return preNode
}
let len = preNode.childrenCount
for (let index = 0; index < len; index++) {
const result = this.getBgNode(preNode.children[index]);
if (result) {
return result
}
}
}
//#region ljc 长短屏手机判断(比较特殊,正常判断是否大于0.5,这里网页端会裁切下半部分所以取1)
public static IsShortScreen(): boolean {
let deviceSize = cc.view.getFrameSize();
return (deviceSize.width / deviceSize.height >= 1)
}
//#endregion
//#region ljc 系统判断
public static IsAndroid() {
if (0 == this.getOSType()) {
return true
} else {
return false
}
}
public static IsIOS() {
if (1 == this.getOSType()) {
return true
} else {
return false
}
}
/**
* 获取操作系统类型:
* 0-Android
* 1-iOS
* @returns
*/
private static getOSType() {
if (/(Android)/i.test(navigator.userAgent)) {
// console.log("ljc - js判断系统: 安卓");
return 0;
} else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
// console.log("ljc - js判断系统: 苹果");
return 1;
} else {
return 2;
}
}
//#endregion
//#region ljc 平台判断
public static IsMobile() {
if (this.isMobile == true) {
return this.isMobile;
} else {
this.PlatformTest()
return this.isMobile;
}
}
/**横屏状态 */
public static IsLandscape() {
//横屏状态!
return (window.orientation == 90 || window.orientation == -90)
}
/**竖屏状态 */
public static IsPortrait() {
//竖屏状态!
return (window.orientation == 180 || window.orientation == 0)
}
public static IsTablet() {
if (this.isTablet == true) {
return this.isTablet;
} else {
this.PlatformTest()
return this.isTablet;
}
}
public static IsPc() {
if (this.isPc == true) {
return this.isPc;
} else {
this.PlatformTest()
return this.isPc;
}
}
private static PlatformTest() {
var os = function () {
var ua = navigator.userAgent,
isWindowsPhone = /(?:Windows Phone)/.test(ua),
isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
isAndroid = /(?:Android)/.test(ua),
isFireFox = /(?:Firefox)/.test(ua),
isChrome = /(?:Chrome|CriOS)/.test(ua),
isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
isPhone = /(?:iPhone)/.test(ua) && !isTablet,
isPc = !isPhone && !isAndroid && !isSymbian;
return {
isTablet: isTablet,
isPhone: isPhone,
isAndroid: isAndroid,
isPc: isPc
};
}();
if (os.isAndroid || os.isPhone) {
// 手机
// document.write('<meta name="viewport" content="width=device-width,initial-scale=0.3">')
this.isMobile = true;
// console.log("ljc - js判断平台: 手机");
} else if (os.isTablet) {
// 平板
// document.write('<meta name="viewport" content="width=device-width,initial-scale=0.6">')
this.isTablet = true;
// console.log("ljc - js判断平台: 平板");
} else if (os.isPc) {
// pc
// document.write('<meta name="viewport" content="width=device-width,initial-scale=1">')
this.isPc = true;
// console.log("ljc - js判断平台: PC");
}
}
//#endregion
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xytuoren/cocos2.4-tool.git
git@gitee.com:xytuoren/cocos2.4-tool.git
xytuoren
cocos2.4-tool
cocos2.4Tool
master

搜索帮助