unity SteamVR2.0 激光与UGUI交互(一)

效果图:
UGUI交互E.gif

发射激光:
在StemVR中自带有SteamVR_LaserPointer的脚本,是用来控制激光。


SteamVR_LaserPointer.png
开启激光:
SteamVrLaserPointer.GetComponent().enabled = true; 
在开启激光时,会创建一个cube,用来模拟激光
关闭激光:
   SteamVrLaserPointer.GetComponent().enabled = false; //射线关闭
        SteamVrLaserPointer.GetComponent().pointer.transform.localScale = new Vector3(0, 0, 0);

UI互交:
使用UnityEvent的目的是,绑定事件可视化


image.png
  private PointerEventArgs pointerEventArgs;
    public SteamVR_LaserPointer SteamVrLaserPointer;
    public UnityEvent mOnEnter = null;
    public UnityEvent mOnClick = null;
    public UnityEvent mOnUp = null;

    void OnEnable()
    {
        SteamVrLaserPointer.PointerClick += SteamVrLaserPointer_PointerClick;
        SteamVrLaserPointer.PointerIn += SteamVrLaserPointer_PointerIn;
        SteamVrLaserPointer.PointerOut += SteamVrLaserPointer_PointerOut;
    }

    void OnDestroy()
    {
        SteamVrLaserPointer.PointerClick -= SteamVrLaserPointer_PointerClick;
        SteamVrLaserPointer.PointerIn -= SteamVrLaserPointer_PointerIn;
        SteamVrLaserPointer.PointerOut -= SteamVrLaserPointer_PointerOut;
    }

    private void SteamVrLaserPointer_PointerOut(object sender, PointerEventArgs e)
    {
        if (e.target.gameObject == this.gameObject)
        {
            if (mOnUp != null) mOnUp.Invoke();
        }
    }

    private void SteamVrLaserPointer_PointerIn(object sender, PointerEventArgs e)
    {
        if (e.target.gameObject == this.gameObject)
        {
            if (mOnEnter != null) mOnEnter.Invoke();
        }
    }
     
    private void SteamVrLaserPointer_PointerClick(object sender, PointerEventArgs e)
    {
        if (e.target.gameObject == this.gameObject)
        {
            Debug.Log(e);
            if (mOnClick != null)
            {
                mOnClick.Invoke();
            }
        }
    }

当获取手柄的GrabGrip键,按下就执行互交

你可能感兴趣的:(unity SteamVR2.0 激光与UGUI交互(一))