发射激光:
在StemVR中自带有SteamVR_LaserPointer的脚本,是用来控制激光。
SteamVR_LaserPointer.png
开启激光:
SteamVrLaserPointer.GetComponent<SteamVR_LaserPointer>().enabled = true;
在开启激光时,会创建一个cube,用来模拟激光
关闭激光:
SteamVrLaserPointer.GetComponent<SteamVR_LaserPointer>().enabled = false; //射线关闭
SteamVrLaserPointer.GetComponent<SteamVR_LaserPointer>().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键,按下就执行互交
网友评论