学校网站平台建设app网站推广平台
5. 创建一个 XR 场景 | PICO 开发者平台https://developer.picoxr.com/zh/document/unity/create-an-xr-scene/一、地面瞬移
1.在地面物体上添加组件
2.在XR Origin (XR Rig)上添加组件
二、更改按键
1.将握手按键更改为扳手键,双击XRI LeftHand Interaction
2.更改按键
3.更改激光颜色
三、锚点传送
1.在地面上添加组件脚本Teleportation Anchor,修改相应的参数。
四、通过手柄摇杆控制物体与自身的移动,创建脚本ContorlObject,将其挂载到想要移动的物体上面。
using UnityEngine;
using UnityEngine.XR;public class ContorlObject : MonoBehaviour
{void Update(){Vector2 vec2DAxis = Vector2.zero;//获取右手控制器的2D轴// 尝试从XR节点(右手)获取输入设备,并读取二维轴的值InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.primary2DAxis, out vec2DAxis);// 根据获取的二维轴值更新物体的位置// 沿着x轴和z轴根据二维轴的值和时间的流逝进行移动transform.position = new Vector3(transform.position.x + vec2DAxis.x * Time.deltaTime, // 更新x轴位置transform.position.y, // 保持y轴位置不变transform.position.z + vec2DAxis.y * Time.deltaTime // 更新z轴位置);}
}
五、测试手柄按键API
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;public class ContorlObject : MonoBehaviour
{void Update(){Vector2 vec2DAxis = Vector2.zero;//获取右手控制器的2D轴// 尝试从XR节点(右手)获取输入设备,并读取二维轴的值InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.primary2DAxis, out vec2DAxis);// 根据获取的二维轴值更新物体的位置// 沿着x轴和z轴根据二维轴的值和时间的流逝进行移动transform.position = new Vector3(transform.position.x + vec2DAxis.x * Time.deltaTime, // 更新x轴位置transform.position.y, // 保持y轴位置不变transform.position.z + vec2DAxis.y * Time.deltaTime // 更新z轴位置);bool isGrip = false;bool isTrigger = false;bool isMenu = false;bool isPrimaryButton = false;bool isSecondButton = false;//握手按键检测InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.gripButton, out isGrip);if (isGrip){this.gameObject.GetComponent<MeshRenderer>().material.color = Color.red;}//扳机按键检测InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.triggerButton, out isTrigger);if (isTrigger){this.gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;}//菜单按键检测InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.menuButton, out isMenu);if (isMenu){this.gameObject.GetComponent<MeshRenderer>().material.color = Color.green;}//主键按键检测InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.primaryButton, out isPrimaryButton);if (isPrimaryButton){this.gameObject.GetComponent<MeshRenderer>().material.color = Color.yellow;}//次键按键检测InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.secondaryButton, out isSecondButton);if (isSecondButton){this.gameObject.GetComponent<MeshRenderer>().material.color = Color.magenta;}}
}
六、PICO中开发UI界面
1.选择XR中的UICanvase
2.把Canvase中的Scale属性缩小,(0.1,0.1,0.1)
3.如果UI上的文字不清晰,调整Canvas属性Canvas Scaler中的Dynamic Sacles Per Unit调大
七、射线回调函数
1.在被射线交互的物体上挂载XR Simple Interactable脚本
2.编写事件脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;public class RaycastEvent : MonoBehaviour
{//悬停进入事件回调函数public void HoverEnter(HoverEnterEventArgs args){this.gameObject.GetComponent<Renderer>().material.color = Color.red;Debug.Log("Hover Enter");}//悬停退出事件回调函数public void HoverExit(HoverExitEventArgs args){this.gameObject.GetComponent<Renderer>().material.color = Color.white;Debug.Log("Hover Exit");}//选中进入事件回调函数public void SelectEnter(SelectEnterEventArgs args){this.gameObject.GetComponent<Renderer>().material.color = Color.green;Debug.Log("Select Enter");}//选中退出事件回调函数public void SelectExit(SelectExitEventArgs args){this.gameObject.GetComponent<Renderer>().material.color = Color.blue;Debug.Log("Select Exit");}
}
3.将脚本RaycastEvent挂载想让回调函数触发的物体上(该例子为颜色改变)
4.在挂载XR Simple Interactable脚本下,类似于绑定UGUI事件,绑定射线回调事件