fgui 点击事件的 pos 怎么转化为 Cocoscreator 世界坐标

发布时间 2023-10-08 13:14:38作者: bakabird1998

对于 fgui 的点击事件 e

  1. e.pos 并不是屏幕坐标
  2. e.pos 是相对于 Canvas 的一个坐标
  3. e.pos 几乎是 Canvas 下的 ui空间世界坐标。

又有:

  • e.pos 使用的坐标系:x轴向右,y轴向下。
  • Canvas 使用的坐标系:x轴向右,y轴向上。

因此:

import { Event } from "fairygui-ccc370";

// ...
private _onTouchBegin(e: Event) {
  // e.pos
  var ePos = e.pos;
  var canvas = GRoot.inst.node.parent.getComponent(Canvas);
  var canvasCam = canvas.cameraComponent;
  // 转化为 ui世界坐标
  var uiWorldPosition = new Vec3(ePos.x, fgui.root.height - ePos.y, 0);
  // 转换为 世界坐标
  var worldPosition = canvasCam.screenToWorld(uiWorldPosition);
}
// ...

如有错误请在评论中指正!