适配iPhoneX等类型的刘海屏幕¶
源码¶
const { ccclass, property, menu } = cc._decorator;
/**
* 适配iPhoneX等类型的刘海屏幕
* @条件:目标节点有配置cc.Widget组件,且目标组件是按照top属性对其;
*/
@ccclass
@menu('self/AdaptedTopOfIPhoneX')
export default class AdaptedTopOfIPhoneX extends cc.Component {
@property(cc.Float) offset = 50;
onLoad() {
if (this.aspectRatio > 2) {
let comp = this.node.getComponent(cc.Widget);
comp.top += this.offset;
}
}
get aspectRatio() {
const size = cc.view.getFrameSize();
return size.height / size.width;
}
}
使用说明¶
添加该脚本后在顶部菜单栏 "组件" 中将出现myself选项,同时在选中节点时 添加组件 按钮也有专门的分类