目录— 个文件
⌕
目录加载中…
…/module/component/DragComponent.ts
1/**
2 * 通用拖拽组件
3 *
4 * 挂在节点上后,该节点可被随意拖拽(单指/鼠标按下拖动)。
5 * 拖拽在父节点坐标系下进行,不影响层级与缩放。
6 *
7 * 使用说明:
8 * - 节点或其子节点需有 UITransform 且 contentSize 大于 0,才能接收触摸;
9 * - 若节点本身无尺寸,可在其下挂一个带 Sprite/Graphics 等有尺寸的子节点。
10 */
11
12import { _decorator, Component, Node, Vec2, Vec3, UITransform, NodeEventType, EventTouch } from 'cc';
13
14const { ccclass, property } = _decorator;
15
16@ccclass('DragComponent')
17export class DragComponent extends Component {
18
19 /** 拖拽开始时触摸点在父节点空间的位置 */
20 private _startTouchInParent: Vec3 = new Vec3();
21 /** 拖拽开始时本节点在父节点空间的位置 */
22 private _startNodePos: Vec3 = new Vec3();
23
24 onLoad() {
25 this.node.on(NodeEventType.TOUCH_START, this._onTouchStart, this);
26 this.node.on(NodeEventType.TOUCH_MOVE, this._onTouchMove, this);
27 this.node.on(NodeEventType.TOUCH_END, this._onTouchEnd, this);
28 this.node.on(NodeEventType.TOUCH_CANCEL, this._onTouchEnd, this);
29 }
30
31 onDestroy() {
32 this.node.off(NodeEventType.TOUCH_START, this._onTouchStart, this);
33 this.node.off(NodeEventType.TOUCH_MOVE, this._onTouchMove, this);
34 this.node.off(NodeEventType.TOUCH_END, this._onTouchEnd, this);
35 this.node.off(NodeEventType.TOUCH_CANCEL, this._onTouchEnd, this);
36 }
37
38 private _getParentUITransform(): UITransform | null {
39 const parent = this.node.parent;
40 if (!parent) return null;
41 return parent.getComponent(UITransform);
42 }
43
44 private _onTouchStart(event: EventTouch) {
45 const parentUt = this._getParentUITransform();
46 if (!parentUt) return;
47
48 const uiPos = event.getUILocation();
49 const world = new Vec3(uiPos.x, uiPos.y, 0);
50 parentUt.convertToNodeSpaceAR(world, this._startTouchInParent);
51 this._startNodePos.set(this.node.position);
52 }
53
54 private _onTouchMove(event: EventTouch) {
55 const parentUt = this._getParentUITransform();
56 if (!parentUt) return;
57
58 const uiPos = event.getUILocation();
59 const world = new Vec3(uiPos.x, uiPos.y, 0);
60 const cur = new Vec3();
61 parentUt.convertToNodeSpaceAR(world, cur);
62
63 const dx = cur.x - this._startTouchInParent.x;
64 const dy = cur.y - this._startTouchInParent.y;
65 this.node.setPosition(
66 this._startNodePos.x + dx,
67 this._startNodePos.y + dy,
68 this._startNodePos.z
69 );
70 }
71
72 private _onTouchEnd(_event: EventTouch) {
73 // 可选:拖拽结束时更新“起始位置”,使下次拖拽从当前位继续;当前实现每次 touch start 都会重记,无需处理
74 }
75}
讲解76 行
节点拖拽人工通读
DragComponent.ts
节点加载时订阅四类触摸事件,把屏幕 UI 坐标转换到父节点局部空间,以开始位置加触点位移更新自身。两个起点向量保存在实例,销毁解绑;结束回调为空,没有单指锁或边界约束,缺少父 UITransform 时该次输入被忽略。
依次读 _onTouchStart 的起点、_onTouchMove 坐标差和生命周期订阅。
特殊处理与易错点
- 特殊处理移动保留开始时的 Z 坐标,仅更新父空间 X/Y。
- 易错点没有触点 ID 或拖拽状态,多指会改写同一组起点。
12注释通用拖拽组件 挂在节点上后,该节点可被随意拖拽(单指/鼠标按下拖动)。 拖拽在父节点坐标系下进行,不影响层级与缩放。 使用说明: - 节点或其子节点需有 UITransform 且 contentSize 大于 0,才能接收触摸; - 若节点本身无尺寸,可在其下挂一个带 Sprite/Graphics 等有尺寸的子节点。
17注释DragComponent:通用拖拽组件 挂在节点上后,该节点可被随意拖拽(单指/鼠标按下拖动)。 拖拽在父节点坐标系下
20属性开始触摸换算后的父节点局部坐标。
22属性开始触摸时自身局部位置,后续移动一直以此为基准。
24方法注册触摸开始移动结束及取消事件以驱动节点拖拽。
31方法移除加载时注册的四类触摸监听以解除组件引用。
38方法查找父节点上的变换组件供触点坐标转换使用。
44方法把触点换算至父空间并保存节点与触点起点。
54方法计算父空间触点相对起点位移并更新节点位置。
72方法结束回调保留为空,下次开始触摸会重新采样起点。
74注释可选:拖拽结束时更新“起始位置”,使下次拖拽从当前位继续;当前实现每次 touch start 都会重记,无需处理