AdapterView.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. */
  3. import { v3, _decorator, UITransform } from "cc";
  4. import { EDITOR } from "cc/env";
  5. import { Adapter } from "./Adapter";
  6. // import { Console } from "console";
  7. const { ccclass, property, executeInEditMode, menu } = _decorator;
  8. /**
  9. * 全屏适配
  10. * 用法:
  11. *
  12. * 1. 将本组件挂载在节点上即可(注意该节点不能挂在 Widget 组件)
  13. * 2. 3.x版本引擎旋转时不会触屏旋转事件,所以目前只能设置一个方向才是正常的
  14. *
  15. * 适配原理:
  16. *
  17. * 1. 将节点的宽高设置为安全区域的宽高
  18. */
  19. @ccclass("AdapterView")
  20. @executeInEditMode(true)
  21. @menu("Quick适配组件/AdapterView")
  22. export default class AdapterView extends Adapter {
  23. protected onChangeSize() {
  24. Adapter.safeArea = null as any;
  25. if (this.node) {
  26. if (this.isFullScreenAdaption) {
  27. // 将屏幕尺寸下的大小,转换为设计分辨率下的大小,重新给节点设置大小
  28. this.width = Adapter.safeArea.width / Adapter.safeArea.designPxToScreenPxRatio;
  29. this.height = Adapter.safeArea.height / Adapter.safeArea.designPxToScreenPxRatio;
  30. } else {
  31. // 将屏幕尺寸下的安全区域大小,转换为设计分辨率下的大小,重新给节点设置大小
  32. this.width = Adapter.safeArea.safe.width / Adapter.safeArea.designPxToScreenPxRatio;
  33. this.height = Adapter.safeArea.safe.height / Adapter.safeArea.designPxToScreenPxRatio;
  34. switch (this.direction) {
  35. case Adapter.direction.LandscapeLeft:
  36. this.node.setPosition(v3(Adapter.safeArea.outside.width / Adapter.safeArea.designPxToScreenPxRatio, 0));
  37. break;
  38. case Adapter.direction.LandscapeRight:
  39. this.node.setPosition(v3(-Adapter.safeArea.outside.width / Adapter.safeArea.designPxToScreenPxRatio, 0));
  40. break;
  41. case Adapter.direction.Portrait:
  42. this.node.setPosition(v3(0, -Adapter.safeArea.outside.height / Adapter.safeArea.designPxToScreenPxRatio));
  43. break;
  44. case Adapter.direction.UpsideDown:
  45. this.node.setPosition(v3(0, Adapter.safeArea.outside.height / Adapter.safeArea.designPxToScreenPxRatio));
  46. break;
  47. default:
  48. !EDITOR && console.log('获取不到设备方向,直接居中处理');
  49. this.node.setPosition(v3(0, 0));
  50. break;
  51. }
  52. }
  53. this.doOnAdapterComplete();
  54. }
  55. }
  56. }