在前面的文档中,介绍了如何在Unity工程中配置号MRTK和Pico SDK
本文将介绍如何运行一个简单的带有MRTK的示例
以下正文
导入MRTK示例
导入目的
这里我们主要是需要MRTK中手部相关预制件和部分UI示例。
快捷导入
导入MRTK示例场景的Unitypackage,下载地址
注意事项
- 这个package不包含MRTK3的相关依赖,需确保在这之前已成功导入MRTK3。
- 出现TMP Importer的弹窗,点击Import导入TMP
- 部分资源有缺失,去掉这些资源。(因为这个暂时用不上)
例如,去掉下图中的MRTK_Logo等对象
接入Pico的手部模型
打开示例场景
打开Assests/Scenes/SampleScene.unity
修改手部模型
- 修改"MRTK RightHand Controller"组件的Model设置。
修改前:
修改后:
-
同理,修改另外一个手部模型
-
禁用Pico的默认射线对象
若不禁用,则会出现MRTK的射线与Pico的手部射线对象同时存在的情况
注意
Pico SDK文档中介绍,将手部预制件(如HandLeft)添加到"XR-Rig"->"Camera Offest"的节点下面。而当接入MRTK后,请勿添加。否则场景中会出现多个手部模型。
修改组件
注意:若是采用前文的整合包资源,则无需这个步骤。我已做修改。
下载地址:MRTK3整合包下载链接
修改MRTK的脚本
这里参考了MRTK3在PICO4上的使用小结
这里会用到之前设置的"PICO_INSTALL",采用条件编译
- 修改ArticulatedHandController
此脚本挂载于MRTK XR Rig下的MRTK LeftHand Controller和MRTK RightHand Controller物体上。修改其中的UpdateInput方法,添加条件编译指令,示例如下:
c#
/// <inheritdoc />
protected override void UpdateInput(XRControllerState controllerState)
{
//...
// Debounce the polyfill pinch action value.
bool isPinched = pinchAmount >= (pinchedLastFrame ? 0.9f : 1.0f);
#if !PICO_INSTALL
// Inject our own polyfilled state into the Select state if no other control is bound.
if (!selectAction.action.HasAnyControls() || isTrackingStatePolyfilled)
{
controllerState.selectInteractionState.active = isPinched;
controllerState.selectInteractionState.activatedThisFrame = isPinched && !pinchedLastFrame;
controllerState.selectInteractionState.deactivatedThisFrame = !isPinched && pinchedLastFrame;
}
if (!selectActionValue.action.HasAnyControls() || isTrackingStatePolyfilled)
{
controllerState.selectInteractionState.value = pinchAmount;
}
// Also make sure we update the UI press state.
if (!uiPressAction.action.HasAnyControls() || isTrackingStatePolyfilled)
{
controllerState.uiPressInteractionState.active = isPinched;
controllerState.uiPressInteractionState.activatedThisFrame = isPinched && !pinchedLastFrame;
controllerState.uiPressInteractionState.deactivatedThisFrame = !isPinched && pinchedLastFrame;
}
if (!uiPressActionValue.action.HasAnyControls() || isTrackingStatePolyfilled)
{
controllerState.uiPressInteractionState.value = pinchAmount;
}
pinchedLastFrame = isPinched;
#else
controllerState.selectInteractionState.active = isPinched;
controllerState.selectInteractionState.activatedThisFrame = isPinched && !pinchedLastFrame;
controllerState.selectInteractionState.deactivatedThisFrame = !isPinched && pinchedLastFrame;
controllerState.selectInteractionState.value = pinchAmount;
controllerState.uiPressInteractionState.active = isPinched;
controllerState.uiPressInteractionState.activatedThisFrame = isPinched && !pinchedLastFrame;
controllerState.uiPressInteractionState.deactivatedThisFrame = !isPinched && pinchedLastFrame;
controllerState.uiPressInteractionState.value = pinchAmount;
pinchedLastFrame = isPinched;
#endif
}
// Cast to expose hand state.
//...
}
}
添加PXR组件
添加PXR_Manager,并启用手势追踪