最近有一个项目效果,要使用曲面UI。就选择了受欢迎度比较高的CurvedUI这个插件。配合上SteamVR和VRTK一起使用。版本使用的是Curved UI - VR Ready Solution To Bend Warp Your Canvas3.0+steamVR1.2.2+VRTK3.2.0
插件链接
导入三个插件后,打开CurvedUI的demo场景,发现并不能如愿以偿。研究未果之后,选择了另一种方案。如果你也没找到合适的方案可以使用以下;
直接复制并打开CurvedUI的demo场景,然后隐藏掉SteamVRPrefabs和场景中的射线模型CurvedUILaserPointer,并将VRTK的那一套拿过来添加上。
这里设置完成后,在canvas上添加可进行UI交互的VRTK_UICanvas;运行程序发现并不能进行点击。这里我使用的是讲button的事件转发,在射线进行交互点击的时候,模拟了按钮Button的点击;脚本挂在Button身上。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using VRTK;
public class CurvedUITest : MonoBehaviour
{
public VRTK_Pointer _Pointer;
private GameObject EnterPanel;
// Start is called before the first frame update
void Start()
{
EnterPanel = gameObject.transform.parent.gameObject;
_Pointer.DestinationMarkerEnter += PointerEnter;
_Pointer.DestinationMarkerExit += PointerExit;
_Pointer.DestinationMarkerHover += PointerHover;
_Pointer.DestinationMarkerSet += PointerSet;
gameObject.GetComponent<Button>().onClick.AddListener(EnterButtonClick);
}
private void PointerSet(object sender, DestinationMarkerEventArgs e)
{
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
//关键代码(按钮本身,当前按钮事件,按钮绑定的事件)
}
private void PointerHover(object sender, DestinationMarkerEventArgs e)
{
//print("射线停留");
}
private void PointerExit(object sender, DestinationMarkerEventArgs e)
{
//print("射线退出");
}
private void PointerEnter(object sender, DestinationMarkerEventArgs e)
{
// print("射线进入");
}
public void EnterButtonClick()
{
EnterPanel.SetActive(false);
}
private void OnDisable()
{
_Pointer.DestinationMarkerEnter -= PointerEnter;
_Pointer.DestinationMarkerExit -= PointerExit;
_Pointer.DestinationMarkerHover -= PointerHover;
_Pointer.DestinationMarkerSet -= PointerSet;
}
}
修改下手柄的交互按键。就Ok了!