BangsFit.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. @property({displayName: '是否适配特殊节点'})
  11. private isFitSpecialNode = false;
  12. @property({displayName: '是否适配特殊节点', visible(){return this.isFitSpecialNode}})
  13. private offset = 80;
  14. onLoad() {
  15. this._AdaptLeftAndRight();
  16. }
  17. start()
  18. {
  19. }
  20. /*============================================================================================================*/
  21. /**
  22. */
  23. /*============================================================================================================*/
  24. _AdaptLeftAndRight() {
  25. let widget = this.node.getComponent(cc.Widget);
  26. if (!widget) return;
  27. let is_big = this.IsBigScreenRatio();
  28. if (is_big) {
  29. if(this.isFitSpecialNode)
  30. {
  31. if(widget.isAlignTop)
  32. {
  33. let add = Math.round((this.GetWinHeightWidthRatio() * this.offset)/1.86);
  34. widget.top = widget.top + add;
  35. }
  36. if(widget.isAlignBottom)
  37. {
  38. let add = Math.round((this.GetWinHeightWidthRatio() * this.offset)/1.86);
  39. widget.bottom = widget.bottom + add;
  40. }
  41. }else{
  42. widget.top = LeftPadding;
  43. }
  44. // widget.bottom = RightPadding;
  45. }
  46. }
  47. /**
  48. * @returns 是宽屏
  49. */
  50. IsBigScreenRatio() {
  51. return this.GetWinHeightWidthRatio() >= 1.86;
  52. }
  53. /**
  54. * @returns 屏幕比率
  55. */
  56. GetWinHeightWidthRatio() {
  57. let ratio = cc.winSize.height >= cc.winSize.width ? (cc.winSize.height / cc.winSize.width) : (cc.winSize.width / cc.winSize.height);
  58. return ratio;
  59. }
  60. }