SteamVR Plugin2.2.0获取手柄按键+射线控制UGUI按钮点击事件

SteamVR Plugin2.2.0获取手柄按键+射线控制UGUI按钮点击事件_第1张图片

SteamVR_LaserPointer是自带的脚本直接挂上就行

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
using Valve.VR.Extras;
//写的一个控制射线关闭开启的功能

public class LaserPointer_Chh : MonoBehaviour
{
    public SteamVR_LaserPointer SteamVrLaserPointer;
    /// 
    /// 什么类型的按键 
    /// 
    public SteamVR_Action_Boolean disk;
    void Start()
    {
        if (SteamVrLaserPointer == null)
            SteamVrLaserPointer = this.GetComponent();

    }

    void Update()
    {
        //SteamVR_Input_Sources.RightHand 只获取右手的
        if (disk.GetStateDown(SteamVR_Input_Sources.RightHand))
        {
            //按下圆盘开启射线 
            OpenLaserPointer();
        }
        if (disk.GetStateUp(SteamVR_Input_Sources.RightHand))
        {
            //抬起圆盘关闭射线 
            CloseLaserPointer();
        }
    }

    public void OpenLaserPointer()
    {
        GetComponent().enabled = true; //射线开启
    }

    public void CloseLaserPointer()
    {
        GetComponent().enabled = false; //射线关闭
        GetComponent().pointer.transform.localScale = new Vector3(0, 0, 0);
    }
}

SteamVR Plugin2.2.0获取手柄按键+射线控制UGUI按钮点击事件_第2张图片

自己写了一个PlayerUIButton脚本来响应SteamVR_LaserPointer脚本里面的事件

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


public class PlayerUIButton : MonoBehaviour
{
    private PointerEventArgs pointerEventArgs;
    public SteamVR_LaserPointer SteamVrLaserPointer;
    public UnityEvent mOnEnter = null;
    public UnityEvent mOnClick = null;
    public UnityEvent mOnUp = null;

    void Start()
    {
        mOnEnter.AddListener(OnButtonEnter);
        mOnClick.AddListener(OnButtonClick);
        mOnUp.AddListener(OnButtonUp);
    }

    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)
        {
            if (mOnClick != null) mOnClick.Invoke();
        }
    }

    /// 
    /// 按下
    /// 
    public void OnButtonClick()
    {
        Debug.Log("OnButtonClick");
    }
    /// 
    /// 经过
    /// 
    public void OnButtonEnter()
    {
        Debug.Log("OnButtonEnter");
    }
    /// 
    /// 抬起
    /// 
    public void OnButtonUp()
    {
        Debug.Log("OnButtonUp");
    }
}

 

你可能感兴趣的:(unity)