60 lines
2.6 KiB
TypeScript
60 lines
2.6 KiB
TypeScript
import { _decorator, Component, director, SceneAsset, input, Input, EventMouse, CameraComponent, geometry, PhysicsSystem } from 'cc';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('SceneLoaderOnDoubleClick3D')
|
|
export class SceneLoaderOnDoubleClick3D extends Component {
|
|
@property({ type: SceneAsset, tooltip: 'Сцена для загрузки при двойном клике' })
|
|
public targetScene: SceneAsset | null = null;
|
|
|
|
@property({ type: CameraComponent, tooltip: 'Основная камера' })
|
|
public camera: CameraComponent | null = null;
|
|
|
|
// Интервал для определения двойного клика (в мс)
|
|
private _doubleClickThreshold: number = 300;
|
|
private _lastClickTime: number = 0;
|
|
|
|
onLoad() {
|
|
// Подписываемся на глобальное событие
|
|
input.on(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
|
|
}
|
|
|
|
onDestroy() {
|
|
input.off(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
|
|
}
|
|
|
|
onMouseDown(event: EventMouse) {
|
|
if (event.getButton() !== EventMouse.BUTTON_LEFT) return;
|
|
|
|
const currentTime = Date.now();
|
|
if (currentTime - this._lastClickTime < this._doubleClickThreshold) {
|
|
// Если камера не назначена, выводим ошибку
|
|
if (!this.camera) {
|
|
console.error('Основная камера не назначена!');
|
|
return;
|
|
}
|
|
|
|
// Выполняем преобразование экрана в луч
|
|
const clickPos = event.getLocation(); // координаты клика
|
|
let ray = new geometry.Ray();
|
|
this.camera.screenPointToRay(clickPos.x, clickPos.y, ray);
|
|
|
|
// Выполняем Raycast
|
|
if (PhysicsSystem.instance.raycast(ray)) {
|
|
const results = PhysicsSystem.instance.raycastResults;
|
|
// Проверяем, что среди результатов есть наш объект
|
|
let hit = results.find(result => result.collider.node === this.node);
|
|
if (hit) {
|
|
console.log(`Двойной клик по ${this.node.name}`);
|
|
if (this.targetScene) {
|
|
console.log(`Загрузка сцены: ${this.targetScene.name}`);
|
|
director.loadScene(this.targetScene.name);
|
|
} else {
|
|
console.error('Сцена не указана!');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
this._lastClickTime = currentTime;
|
|
}
|
|
}
|