2020-12-22 03:13:43 +00:00
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-03-29 04:19:24 +01:00
|
|
|
|
using System.Collections.Generic;
|
2021-05-12 00:15:11 +01:00
|
|
|
|
using UniRx;
|
2020-03-29 04:19:24 +01:00
|
|
|
|
using UnityEngine;
|
2020-05-26 01:12:06 +01:00
|
|
|
|
using UnityEngine.EventSystems;
|
2020-03-29 04:19:24 +01:00
|
|
|
|
|
|
|
|
|
|
public class ClickRaycaster : MonoBehaviour
|
|
|
|
|
|
{
|
2021-05-12 00:15:11 +01:00
|
|
|
|
public LayerMask Layers;
|
2020-05-26 01:12:06 +01:00
|
|
|
|
public event Action<PointerEventData> OnClickMiss;
|
2020-03-29 04:19:24 +01:00
|
|
|
|
public Camera RayCamera;
|
|
|
|
|
|
private ClickCatcher _clickCatcher;
|
|
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
_clickCatcher = GetComponent<ClickCatcher>();
|
2022-08-08 22:43:55 +01:00
|
|
|
|
_clickCatcher.OnDown.Subscribe(pointer =>
|
2020-03-29 04:19:24 +01:00
|
|
|
|
{
|
2022-08-08 22:43:55 +01:00
|
|
|
|
var ray = RayCamera.ScreenPointToRay(pointer.position);
|
2026-06-21 09:37:54 +01:00
|
|
|
|
var clickables = new List<ClickableCollider>();
|
|
|
|
|
|
foreach (var clickable in ClickableCollider.Active)
|
2020-03-29 04:19:24 +01:00
|
|
|
|
{
|
2026-06-21 09:37:54 +01:00
|
|
|
|
if (clickable == null || ((1 << clickable.gameObject.layer) & Layers.value) == 0)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
clickables.Add(clickable);
|
2020-03-29 04:19:24 +01:00
|
|
|
|
}
|
2026-06-21 09:37:54 +01:00
|
|
|
|
|
|
|
|
|
|
if (AetheriaYmirPhysicsBridge.Instance.TryCastClickables(clickables, ray, 1000, out var hit))
|
|
|
|
|
|
hit.Clickable.Click(pointer, ray, hit);
|
|
|
|
|
|
else
|
|
|
|
|
|
OnClickMiss?.Invoke(pointer);
|
2021-05-12 00:15:11 +01:00
|
|
|
|
});
|
2020-03-29 04:19:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|