BangsFit.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const {ccclass, property} = cc._decorator;
  2. const LeftPadding = 65;
  3. const RightPadding = 65;
  4. /**
  5. * 刘海屏适配
  6. * @author 薛鸿潇
  7. */
  8. @ccclass
  9. export default class BangsFit extends cc.Component {
  10. onLoad() {
  11. this._AdaptLeftAndRight();
  12. }
  13. /*============================================================================================================*/
  14. /**
  15. */
  16. /*============================================================================================================*/
  17. _AdaptLeftAndRight() {
  18. let widget = this.node.getComponent(cc.Widget);
  19. if (!widget) return;
  20. let is_big = this.IsBigScreenRatio();
  21. if (is_big) {
  22. widget.top = LeftPadding;
  23. widget.bottom = RightPadding;
  24. }
  25. }
  26. /**
  27. * @returns 是宽屏
  28. */
  29. IsBigScreenRatio() {
  30. return this.GetWinHeightWidthRatio() >= 1.86;
  31. }
  32. /**
  33. * @returns 屏幕比率
  34. */
  35. GetWinHeightWidthRatio() {
  36. let ratio = cc.winSize.height >= cc.winSize.width ? (cc.winSize.height / cc.winSize.width) : (cc.winSize.width / cc.winSize.height);
  37. return ratio;
  38. }
  39. }