1
0
Fork 0
LaboratoryProtection/Assets/UnityTest/CameraController/Scripts/RayCastLogic.cs

64 lines
1.8 KiB
C#

using Cysharp.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
public class RayCastLogic : MonoBehaviour
{
public Camera mainCamera;
public float length = 10;
private void Reset()
{
this.mainCamera = this.GetComponent<Camera>();
}
// Update is called once per frame
private void Update()
{
var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(mainCamera.transform.position, ray.direction * length, Color.blue);
}
private void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
var hits = Physics.RaycastAll(ray);
Debug.Log(hits.Length);
foreach (var item in hits)
{
ExecuteEvents.Execute(item.collider.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
}
}
}
public async UniTask WaitOnClick(GameObject target, CancellationToken cancellationToken)
{
while (cancellationToken.IsCancellationRequested != true)
{
if (Input.GetMouseButtonDown(0))
{
var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
var hits = Physics.RaycastAll(ray);
foreach (var item in hits)
{
if (item.collider.gameObject == target)
{
ExecuteEvents.Execute(target, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
}
}
}
await UniTask.Yield(cancellationToken);
}
}
}