eye-therapy-2 / Assets / Scripts / UI / RayActivator.cs
RayActivator.cs
Raw
using System.Linq;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

public class RayActivator : MonoBehaviour, IListener
{
    [SerializeField] private EVENT_TYPE[] activatonEvents = null;
    [SerializeField] private EVENT_TYPE[] deactivatonEvents = null;

    private XRRayInteractor rayInteractor;
    private LineRenderer lineRenderer;
    private XRInteractorLineVisual interactorLineVisual;

    void Start()
    {
        rayInteractor = GetComponent<XRRayInteractor>();
        lineRenderer = GetComponent<LineRenderer>();
        interactorLineVisual = GetComponent<XRInteractorLineVisual>();

        rayInteractor.enabled = false;
        interactorLineVisual.enabled = false;
        lineRenderer.enabled = false;

        if (activatonEvents != null)
            foreach(EVENT_TYPE e in activatonEvents)
                EventManager.Instance.AddListener(e, this);
        if (deactivatonEvents != null)
            foreach (EVENT_TYPE e in deactivatonEvents)
                EventManager.Instance.AddListener(e, this);
    }

    public void OnEvent<T>(EVENT_TYPE eventType, Component Sender, T param = default)
    {
        if(activatonEvents.Contains(eventType))
        {
            rayInteractor.enabled = true;
            lineRenderer.enabled = true;
            interactorLineVisual.enabled = true;
        }
        else if(deactivatonEvents.Contains(eventType))
        {
            rayInteractor.enabled = false;
            interactorLineVisual.enabled = false;
            lineRenderer.enabled = false;
        }
    }
}