2018导入新版Steam VR报 缺失OpenVR :解决方案:https://blog.csdn.net/weixin_44247362/article/details/109165496
主脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hand_demo : MonoBehaviour {
public static Hand_demo instance;
SteamVR_TrackedObject trackobj;
SteamVR_Controller.Device device;
[Header("Bool值")]
//按下trigger
public bool trigger_demo;
//长按trigger
public bool triggerPress_Demo;
//按下Grip
public bool Grip_demo;
//按下touchpad
public bool TouchPade_demo;
//长按touchpad
public bool TouchPadePress_demo;
//按下applicationmenu
public bool ApplicationMenu_demo;
//按下圆盘左
public bool touch_left;
//按下圆盘右
public bool touch_right;
//按下圆盘上
public bool touch_forward;
//按下圆盘下
public bool touch_back;
[Header("null object")]
//触发enter检测
private GameObject enter_triger;
//触发stay检测
private GameObject stay_triger;
public GameObject Enter_triger
{
get
{
return enter_triger;
}
set
{
enter_triger = value;
}
}
public GameObject Stay_triger
{
get
{
return stay_triger;
}
set
{
stay_triger = value;
}
}
public SteamVR_TrackedObject Trackobj
{
get
{
return trackobj;
}
set
{
trackobj = value;
}
}
public SteamVR_Controller.Device Device
{
get
{
return device;
}
set
{
device = value;
}
}
// Use this for initialization
void Start () {
instance = this;
Trackobj = transform.GetComponent<SteamVR_TrackedObject>();
}
// Update is called once per frame
void Update () {
try
{
Device = SteamVR_Controller.Input((int)Trackobj.index);
}
catch (System.Exception)
{
throw;
}
JIEKOU();
}
void JIEKOU()
{
Vector2 aaaa = Device.GetAxis();
if (Device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
{
trigger_demo = true;
print("按下trigger");
}
else if (Device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
{
triggerPress_Demo = true;
print("长按trigger");
}
else if (Device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
{
trigger_demo = false;
triggerPress_Demo = false;
print("抬起trigger");
}
//-----------------------------
else if (Device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
{
Grip_demo = true;
print("按下Grip");
print("dddd"+SteamVR_Controller.ButtonMask.Grip);
}
else if (Device.GetPressUp(SteamVR_Controller.ButtonMask.Grip))
{
Grip_demo = false;
print("抬起Grip");
print("uuuu"+SteamVR_Controller.ButtonMask.Grip);
}
//---------------------------
else if (Device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
{
TouchPade_demo = true;
print("按下touchpad");
if (aaaa.x > 0.5 && aaaa.y < 0.5 && aaaa.y > -0.5)
{
print("右");
touch_right = true;
}
else
if (aaaa.x < -0.5 && aaaa.y < 0.5 && aaaa.y > -0.5)
{
print("左");
touch_left = true;
}
else
if (aaaa.y > 0.5 && aaaa.x < 0.5 && aaaa.x > -0.5)
{
print("上");
touch_forward = true;
}
else
if (aaaa.y < -0.5 && aaaa.x < 0.5 && aaaa.x > -0.5)
{
print("下");
touch_back = true;
}
}
else if (Device.GetPress(SteamVR_Controller.ButtonMask.Touchpad))
{
TouchPadePress_demo = true;
print("长按touchPad");
}
else if (Device.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad))
{
TouchPade_demo = false;
TouchPadePress_demo = false;
print("抬起touchpad");
touch_left = false;
touch_right = false;
touch_forward = false;
touch_back = false;
}
//------------------------
else if (Device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
{
ApplicationMenu_demo = true;
print("按下applicationMenu");
}
else if (Device.GetPressUp(SteamVR_Controller.ButtonMask.ApplicationMenu))
{
ApplicationMenu_demo = false;
print("抬起applicationMenu");
}
}
private void OnTriggerEnter(Collider other)
{
Enter_triger = other.gameObject;
}
private void OnTriggerStay(Collider other)
{
Stay_triger = other.gameObject;
}
private void OnTriggerExit(Collider other)
{
Enter_triger = null;
Stay_triger = null;
}
}
第一种外部所有调一个
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Zhuaqu_demo : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (Hand_demo.instance.triggerPress_Demo == true)
{
if (Hand_demo.instance.Enter_triger != null)
{
if (Hand_demo.instance.Enter_triger.GetComponent<Rigidbody>() != null)
{
Hand_demo.instance.Enter_triger.GetComponent<Rigidbody>().isKinematic = true;
}
Hand_demo.instance.Enter_triger.transform.SetParent(Hand_demo.instance.transform);
Hand_demo.instance.transform.GetComponent<Collider>().enabled = false;
}
}
else if (Hand_demo.instance.triggerPress_Demo == false)
{
if (Hand_demo.instance.Enter_triger != null)
{
if (Hand_demo.instance.Enter_triger.GetComponent<Rigidbody>() != null)
{
Hand_demo.instance.Enter_triger.GetComponent<Rigidbody>().isKinematic = false;
//tossObject(Hand_demo.instance.Enter_triger.GetComponent<Rigidbody>());
}
Hand_demo.instance.Enter_triger.transform.SetParent(null);
Hand_demo.instance.Enter_triger = null;
Hand_demo.instance.transform.GetComponent<Collider>().enabled = true;
}
}
}
//物体投掷
void tossObject(Rigidbody rigidbody)
{
//如果是起始位置,则设置为起始位置,否则设置为父类
Transform origin = Hand_demo.instance.Trackobj.origin ? Hand_demo.instance.Trackobj.origin : Hand_demo.instance.Trackobj.transform.parent;
//如果起始位置部位空
if (origin != null)
{
//将局部坐标转换成世界坐标
rigidbody.velocity = origin.TransformVector((Hand_demo.instance.Device.velocity));
rigidbody.angularVelocity = origin.TransformVector((Hand_demo.instance.Device.angularVelocity));
}
else
{
//设置速度
rigidbody.velocity = Hand_demo.instance.Device.velocity;
//设置角速度
rigidbody.angularVelocity = Hand_demo.instance.Device.angularVelocity;
}
}
}
第二种一个调外部所有
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReceiveButton : MonoBehaviour
{
public static void Receive(string button/*SteamVR_Controller.ButtonMask button*/)
{
print("222");
switch (button)
{
default:
break;
}
}
}