потребовалось добавить камеру для переноса на
скрипт для перехода по сценам
This commit is contained in:
parent
9b589ce4fa
commit
b34add94a4
@ -3654,6 +3654,9 @@
|
||||
"__uuid__": "658bce01-ef82-4cee-8fd2-af66cebdba02",
|
||||
"__expectedType__": "cc.SceneAsset"
|
||||
},
|
||||
"camera": {
|
||||
"__id__": 7
|
||||
},
|
||||
"_id": "bfWXLDyG5AY7H6p+7j23oL"
|
||||
},
|
||||
{
|
||||
@ -4126,7 +4129,7 @@
|
||||
"node": {
|
||||
"__id__": 35
|
||||
},
|
||||
"_enabled": true,
|
||||
"_enabled": false,
|
||||
"__prefab": null,
|
||||
"_id": "418lAsM+9Hfq2SqwSdl0ku"
|
||||
},
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
[
|
||||
{
|
||||
"__type__": "cc.SceneAsset",
|
||||
"_name": "12",
|
||||
"_name": "twelve",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"_native": "",
|
||||
@ -1,35 +1,59 @@
|
||||
import { _decorator, Component, Node, EventMouse, director, SceneAsset } from 'cc';
|
||||
import { SceneTransitionManager } from '../stores/SceneTransitionManager';
|
||||
|
||||
import { _decorator, Component, director, SceneAsset, input, Input, EventMouse, CameraComponent, geometry, PhysicsSystem } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('SceneLoaderOnDoubleClick')
|
||||
export class SceneLoaderOnDoubleClick extends Component {
|
||||
@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;
|
||||
|
||||
start() {
|
||||
this.node.on(Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
|
||||
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) {
|
||||
const currentTime = Date.now();
|
||||
if (currentTime - this._lastClickTime < 300) {
|
||||
|
||||
if (!SceneTransitionManager.allowTransition) {
|
||||
console.log('Переходы отключены.');
|
||||
return;
|
||||
}
|
||||
if (this.targetScene) {
|
||||
director.loadScene(this.targetScene.name);
|
||||
} else {
|
||||
console.error('Сцена не указана!');
|
||||
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;
|
||||
}
|
||||
this._lastClickTime = currentTime;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user