Aetheria/Assets/Scripts/UI/ClickRaycaster.cs

39 lines
1.3 KiB
C#
Raw Normal View History

/* 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;
using UniRx;
2020-03-29 04:19:24 +01:00
using UnityEngine;
using UnityEngine.EventSystems;
2020-03-29 04:19:24 +01:00
public class ClickRaycaster : MonoBehaviour
{
public LayerMask Layers;
public event Action<PointerEventData> OnClickMiss;
2020-03-29 04:19:24 +01:00
public Camera RayCamera;
private ClickCatcher _clickCatcher;
void Start()
{
_clickCatcher = GetComponent<ClickCatcher>();
_clickCatcher.OnDown.Subscribe(pointer =>
2020-03-29 04:19:24 +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);
});
2020-03-29 04:19:24 +01:00
}
}