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-04-23 03:31:17 +01:00
|
|
|
|
using System.Collections;
|
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
|
|
|
|
{
|
|
|
|
|
|
RaycastHit hit;
|
2022-08-08 22:43:55 +01:00
|
|
|
|
var ray = RayCamera.ScreenPointToRay(pointer.position);
|
|
|
|
|
|
Physics.Raycast(ray, out hit, 1000, Layers.value);
|
2020-03-29 04:19:24 +01:00
|
|
|
|
if (hit.collider != null)
|
|
|
|
|
|
{
|
2022-08-08 22:43:55 +01:00
|
|
|
|
//Debug.Log($"Clicked on gameobject {hit.collider.gameObject}");
|
2020-03-29 04:19:24 +01:00
|
|
|
|
var clickable = hit.collider.GetComponent<ClickableCollider>();
|
|
|
|
|
|
if (clickable != null)
|
2022-08-08 22:43:55 +01:00
|
|
|
|
clickable.Click(pointer, ray, hit);
|
2020-03-29 04:19:24 +01:00
|
|
|
|
}
|
2020-05-26 01:12:06 +01:00
|
|
|
|
else OnClickMiss?.Invoke(pointer);
|
2021-05-12 00:15:11 +01:00
|
|
|
|
});
|
2020-03-29 04:19:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|