SteamVR 2.3.2 射线检测Button

1. 直接将Player 预制件拖到场景里

2.在LeftHand/ RightHand 上添加脚本

 Steam VR_Laser Pointe (插件里自带)

Steam VR Laser Wrapper(自己写的,封装一层,让按钮事件按原来Untiy的交互方式执行)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using Valve.VR.Extras;

public class SteamVRLaserWrapper : MonoBehaviour
{
    private SteamVR_LaserPointer steamVrLaserPointer;

    private void Awake()
     {
         steamVrLaserPointer = gameObject.GetComponent<SteamVR_LaserPointer>();
         steamVrLaserPointer.PointerIn += OnPointerIn;
         steamVrLaserPointer.PointerOut += OnPointerOut;
         steamVrLaserPointer.PointerClick += OnPointerClick;
    }

private void OnPointerClick(object sender, PointerEventArgs e)
{
    IPointerClickHandler clickHandler = e.target.GetComponent<IPointerClickHandler>();
       if (clickHandler == null)
    {
        return;
    }


             clickHandler.OnPointerClick(new PointerEventData(EventSystem.current));
}

private void OnPointerOut(object sender, PointerEventArgs e)
  {
         IPointerExitHandler pointerExitHandler = e.target.GetComponent<IPointerExitHandler>();
         if (pointerExitHandler == null)
         {
             return;
       }
 
         pointerExitHandler.OnPointerExit(new PointerEventData(EventSystem.current));
     }
 
    private void OnPointerIn(object sender, PointerEventArgs e)
{
    IPointerEnterHandler pointerEnterHandler = e.target.GetComponent<IPointerEnterHandler>();
    if (pointerEnterHandler == null)
    {
              return;
    }

    pointerEnterHandler.OnPointerEnter(new PointerEventData(EventSystem.current));
}

}

3. 按钮高亮, 事件了, 自己设置,就是正常的button设置,正常的关联方法

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页