using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace XRTool.WorldUI
{
///
/// Simple event system using physics raycasts.
///
[AddComponentMenu("Event/XR Physics Raycaster")]
[RequireComponent(typeof(Camera))]
public class XRPhysicsRaycaster : PhysicsRaycaster
{
private RaycastResult minResult;
private PhysicsRayConf layerConf;
public PhysicsRayConf LayerConf { get => layerConf; set => layerConf = value; }
protected override void Start()
{
base.Start();
LayerConf = Resources.Load("PhysicsRayConf");
eventCamera.cullingMask = LayerConf.eventMask;
eventMask = LayerConf.eventMask;
}
public override void Raycast(PointerEventData eventData, List resultAppendList)
{
if (resultAppendList != null && resultAppendList.Count > 0)
{
float minDis = float.MaxValue;
int minIndex = 0;
for (int i = 0; i < resultAppendList.Count; i++)
{
RaycastResult result = resultAppendList[i];
if (result.distance < minDis)
{
minDis = result.distance;
minIndex = i;
}
}
minResult = resultAppendList[minIndex];
resultAppendList.Clear();
resultAppendList.Add(minResult);
base.Raycast(eventData, resultAppendList);
}
else
{
base.Raycast(eventData, resultAppendList);
}
}
}
}