由于Unity软件对VR插件的兼容性问题使得实际项目中对于VR插件的使用限制很大,官方对于SteamVR插件的兼容性是最稳定的,SteamVR插件的示例场景里面对于UI的交互是使用的手柄触摸的方式,在Button上还添加Box Collider,这太麻烦了。
下面我们使用手柄射线交互的UI的方式
本文主要记录实现的方法,具体原理这里不做说明,插件从商店导入到unity,插件导入之后没有报错即是没有问题,找到VR预制体Player加入到场景里面,第一步新建脚本VRInputModel,该脚本不需要添加在任何物体上,代码如下:
using UnityEngine;
using UnityEngine.EventSystems;
public class VRInputModel : BaseInputModule
{
/// <summary>
/// 事件摄像机
/// </summary>
private Camera eventCamera;
/// <summary>
/// 是否检测射线
/// </summary>
public bool isActive = true;
/// <summary>
/// 是否执行UI操作标志位
/// </summary>
public bool isExecute = false;
/// <summary>
/// 指针事件数据
/// </summary>
public PointerEventData Data { get; private set; } = null;
protected override void Awake()
{
eventCamera = GetComponent<Camera>();
}
protected override void Start()
{
Data = new PointerEventData(eventSystem);
//设定射线的起始点为事件相机的视窗中心
Data.position = new Vector2(eventCamera.pixelWidth / 2, eventCamera.pixelHeight / 2);
}
public override void Process()
{
if (isActive)
{
//发射射线检测UI
eventSystem.RaycastAll(Data, m_RaycastResultCache);
//从由近到远的射线碰撞结果m_RaycastResultCache中获取第一个(最近)的碰撞结果对应的射线结果
Data.pointerCurrentRaycast = FindFirstRaycast(m_RaycastResultCache);
//先处理射线点进入或移出UI游戏物体(这个事件让继承IPointerEnterHandler和IPointerExitHandler中的事件触发)
HandlePointerExitAndEnter(Data, Data.pointerCurrentRaycast.gameObject);
Debug.Log(Data.pointerCurrentRaycast.gameObject);
//按下点击按钮的标志位
if (isExecute)
{
ProcessPress();
}
else
{
ProcessRelease();
}
}
}
private void ProcessPress()
{
print("process");
//把当前的射线信息赋值给光标按下射线
Data.pointerPressRaycast = Data.pointerCurrentRaycast;
//把光标按下射线对应的游戏物体赋值给指针数据中的pointPress
Data.pointerPress = ExecuteEvents.GetEventHandler<IPointerClickHandler>(Data.pointerPressRaycast.gameObject);
//执行光标按下事件,该事件会让继承了IPointerClickHandler的派生类中的事件触发
ExecuteEvents.Execute(Data.pointerPress, Data, ExecuteEvents.pointerDownHandler);
//把光标按下射线对应的游戏物体赋值给指针数据中的pointDrag
Data.pointerDrag = ExecuteEvents.GetEventHandler<IDragHandler>(Data.pointerPressRaycast.gameObject);
//执行光标开始拖动事件,该事件会让继承了IIDragHandler的派生类中的事件触发
// ExecuteEvents.Execute(Data.pointerDrag, Data, ExecuteEvents.beginDragHandler);
ExecuteEvents.Execute(Data.pointerDrag, Data, ExecuteEvents.dragHandler);
}
private void ProcessRelease()
{
GameObject pointRelease = ExecuteEvents.GetEventHandler<IPointerClickHandler>(Data.pointerCurrentRaycast.gameObject);
if (Data.pointerPress == pointRelease)
ExecuteEvents.Execute(Data.pointerPress, Data, ExecuteEvents.pointerClickHandler);
ExecuteEvents.Execute(Data.pointerPress, Data, ExecuteEvents.pointerUpHandler);
ExecuteEvents.Execute(Data.pointerDrag, Data, ExecuteEvents.endDragHandler);
Data.pointerPress = null;
Data.pointerDrag = null;
Data.pointerCurrentRaycast.Clear();
}
}
第二步新建脚本InteractionSys,该脚本需要添加在手柄物体上,代码如下:
using UnityEngine;
using Valve.VR;
public class InteractionSys : MonoBehaviour
{
private SteamVR_Action_Boolean m_UIPoint;
private SteamVR_Action_Boolean m_UIClick;
private VRInputModel m_InputModel;
LineRenderer m_Line;
bool isShowLine = false;
private void Awake()
{
m_UIPoint = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("Teleport");
m_UIClick = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("InteractUI");
if (m_InputModel == null)
m_InputModel = gameObject.AddComponent<VRInputModel>();
if (m_Line == null)
{
m_Line = gameObject.AddComponent<LineRenderer>();
Keyframe[] ks = new Keyframe[2];
ks[0] = new Keyframe(0, 0.007f);
ks[1] = new Keyframe(1, 0.007f);
m_Line.widthCurve = new AnimationCurve(ks);
m_Line.material = new Material(Shader.Find("Unlit/Color"));
m_Line.material.SetColor("_Color", new Color32(0, 255, 255, 64));
m_Line.SetPosition(0, Vector3.zero);
m_Line.SetPosition(1, Vector3.zero);
}
m_UIPoint.onStateDown += M_UIPoint_onStateDown;
m_UIClick.onState += M_UIClick_onState;
m_UIClick.onStateUp += M_UIClick_onStateUp;
}
private void Update()
{
if (isShowLine)
{
m_Line.SetPosition(0, transform.position);
m_Line.SetPosition(1, transform.forward * 10000);
}
}
//圆盘按下
private void M_UIPoint_onStateDown(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
{
Debug.Log("圆盘按下");
//射线开启关闭
isShowLine = !isShowLine;
m_InputModel.isActive = isShowLine;
if (!isShowLine)
{
m_Line.SetPosition(0, Vector3.zero);
m_Line.SetPosition(1, Vector3.zero);
}
}
//扳机抬起
private void M_UIClick_onStateUp(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
{
Debug.Log("扳机抬起");
m_InputModel.isExecute = false;
}
//扳机按下
private void M_UIClick_onState(SteamVR_Action_Boolean fromAction, SteamVR_Input_Sources fromSource)
{
Debug.Log("扳机按下");
m_InputModel.isExecute = true;
}
}
然后,在RightHand上,添加一个Camera组件,并且可以设置enable为false,添加InteractionSys脚本在RightHand上面,同时把Canvas画布上面的RenderMode设置成World Space,把Event Camera赋值成RightHand即可。如下图所示:
InteractionSys是交互逻辑表现,需要哪只手发射线交互,就把这个脚本添加到哪只手上,其他组件会自动添加,比如LineRender、EventSysem、VRInputModel等。
完成!!!
交互方式是按圆盘键呼出射线,扳机键点击交互。
有疑问欢迎vx咨询159-7084-3394