如何在OVR ray中拖放Canvas的2D UI元素进行交互?

问题描述 投票:0回答:1
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;
                }
            }
        }
    }
}

我为桌面版本编写了脚本代码,并检查它在桌面上运行良好。然后我想在 Oculus VR 中制作它。

所以我在 Meta All-in-One 中使用了 rayinteractable。所以我从控制器制作了光线投射,并在画布中制作了 oculus 光标。但我无法拖放 UI 元素。如何修复 oculus VR 版本的代码?

unity-game-engine drag-and-drop 2d virtual-reality
1个回答
0
投票

我使用 Oculus Integration,仍然没有更新到 Meta-all-in-one。但我做了一个丑陋的修复,也许你会发现它有用。我仍在为此寻找更优雅的解决方案。

https://forum.unity.com/threads/how-to-drag-ui-object-around-canvas-with-ray-interactor.1445110/#post-9882387

© www.soinside.com 2019 - 2024. All rights reserved.