How drag and drop 2D UI elements of Canvas in OVR ray interactable?

15 Views Asked by At
using UnityEngine;
using UnityEngine.EventSystems;

public class TestAsDesktop : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    private bool isDragging = false;
    private RectTransform rectTransform;
    private Canvas canvas;
    public Vector2 movePos;
    void Awake()
    {
        rectTransform = GetComponent<RectTransform>();
        canvas = GetComponentInParent<Canvas>();
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        bool isHighlighting = gameObject.GetComponent<Node>().isHighlighting;
        if (isHighlighting)
        {
            if (eventData.button == PointerEventData.InputButton.Left && gameObject.tag == "NodeUI")
            {
                isDragging = true;
                gameObject.GetComponent<Node>().isMoving = true;
            }
        }
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left && gameObject.tag == "NodeUI")
        {
            isDragging = false;
            gameObject.GetComponent<Node>().isMoving = false;

            int childCount = rectTransform.childCount;
            int startIndex = Mathf.Max(0, childCount - 2);

            for (int i = startIndex; i < childCount; i++)
            {
                PointEventHandler point = rectTransform.GetChild(i).GetComponent<PointEventHandler>();
                if (point.isLinked)
                {
                    point.isMoved = false;
                }
            }
        }
    }

    void Update()
    {
        if (isDragging)
        {
            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                transform.parent as RectTransform,
                Input.mousePosition,
                canvas.worldCamera,
                out movePos);

            rectTransform.anchoredPosition = movePos;

            int childCount = rectTransform.childCount;
            int startIndex = Mathf.Max(0, childCount - 2);

            for (int i = startIndex; i < childCount; i++)
            {
                PointEventHandler point = rectTransform.GetChild(i).GetComponent<PointEventHandler>();
                if (point.isLinked)
                {
                    point.isMoved = true;
                }
            }
        }
    }
}

I scripted code for the desktop version and checked it worked well on the desktop. Then I want to make it in Oculus VR.

So I used rayinteractable in Meta All-in-One. So I made raycast from controller and make oculus cursor in Canvas. But I was not able to drag and drop UI elements. How can I fix the code for oculus VR version?

0

There are 0 best solutions below