BangsFit.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const {ccclass, property} = cc._decorator;
  2. const LeftPadding = 65;
  3. const RightPadding = 65;
  4. const topOffset = 80;
  5. /**
  6. * 刘海屏适配
  7. * @author 薛鸿潇
  8. */
  9. @ccclass
  10. export default class BangsFit extends cc.Component {
  11. @property({displayName: '是否适配特殊节点'})
  12. private isFitSpecialNode = false;
  13. onLoad() {
  14. this._AdaptLeftAndRight();
  15. }
  16. start()
  17. {
  18. }
  19. /*============================================================================================================*/
  20. /**
  21. */
  22. /*============================================================================================================*/
  23. _AdaptLeftAndRight() {
  24. let widget = this.node.getComponent(cc.Widget);
  25. if (!widget) return;
  26. let is_big = this.IsBigScreenRatio();
  27. if (is_big) {
  28. if(this.isFitSpecialNode)
  29. {
  30. if(widget.isAlignTop)
  31. {
  32. let add = Math.round((this.GetWinHeightWidthRatio() * topOffset)/1.86);
  33. widget.top = widget.top + add;
  34. }
  35. if(widget.isAlignBottom)
  36. {
  37. let add = Math.round((this.GetWinHeightWidthRatio() * topOffset)/1.86);
  38. widget.bottom = widget.bottom + add;
  39. }
  40. }else{
  41. widget.top = LeftPadding;
  42. }
  43. // widget.bottom = RightPadding;
  44. }
  45. }
  46. /**
  47. * @returns 是宽屏
  48. */
  49. IsBigScreenRatio() {
  50. return this.GetWinHeightWidthRatio() >= 1.86;
  51. }
  52. /**
  53. * @returns 屏幕比率
  54. */
  55. GetWinHeightWidthRatio() {
  56. let ratio = cc.winSize.height >= cc.winSize.width ? (cc.winSize.height / cc.winSize.width) : (cc.winSize.width / cc.winSize.height);
  57. return ratio;
  58. }
  59. }