GridDraw.ts 858 B

1234567891011121314151617181920212223242526
  1. import { _decorator, Component, Size, Rect, Vec2, Vec3 } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. // 画网格,个工具类
  4. export class GridDraw {
  5. // 默认右下角,是0,0,返回的坐标,为网格中点
  6. drawGrid(rect:Rect,size:Size,offset:Vec2 =new Vec2(0.5,0.5)):Vec3[][]{
  7. let w = rect.width / size.width;
  8. let h = rect.height / size.height;
  9. let offset_x = w*offset.x+rect.x
  10. let offset_y = h*offset.y+rect.y
  11. let res :Vec3[][] = [[]]
  12. for (let i = 0; i < size.height; i++) {
  13. for (let j = 0; j < size.width; j++) {
  14. if(!res[i]){
  15. res[i] = [];
  16. }
  17. res[i][j] = new Vec3(j *w +offset_x, i * h + offset_y , 0);
  18. }
  19. }
  20. return res;
  21. }
  22. }
  23. export const gridDraw = new GridDraw()