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 版本的代码?
我使用 Oculus Integration,仍然没有更新到 Meta-all-in-one。但我做了一个丑陋的修复,也许你会发现它有用。我仍在为此寻找更优雅的解决方案。