如何关闭运动控制器线指针
How to turn off Motion Controller line pointer
就这样吧,我去踩我的骄傲!
我正在使用 MRTK v2 并且工作正常,除了在某些时候我想关闭从运动控制器延伸到对象并提供输入的线。环顾四周并试图在 MRTK 文档中找到它(这一定很容易,对吧?),我仍在用头撞墙并且开始疼了....
MRTK 文档在此处很好地解释了如何配置它:
但我希望在脚本中执行此操作,根据需要在我的应用程序中启用和禁用它。
知道怎么做吗?
非常感谢!
好问题!这是一种对我有用的方法。您可以在此存储库中查看我的解决方案:https://github.com/julenka/MixedRealityToolkit-Unity/tree/so/linepointer_off。打开场景 Assets/TurnOffLinePointerTest.unity
然后用手模拟按下按钮。转换指针的代码 on/off 在 Assets/PointerConfigurationExample.cs
.
注意:您需要使用这种修改中介而不是直接设置的方法的原因 myPointer.IsActive = false
是因为默认中介每帧都会覆盖这些值。幸运的是,您可以自定义此行为。
第 1 步:稍微更新 MRTK 以便可以访问 PointerMediator
将实现此更改的更改 from this commit to your MRTK clone. This change updates the FocusProvider in MRTK to make the PointerMediator publicly accessible, and makes the DefaultPointerMediator extensible by updating fields to be protected instead of private, and making methods virtual. See this pull request 应用到 MRTK 中。
第 2 步:创建将关闭远指针的自定义 PointerMediator
创建一个自定义指针中介
using System.Collections.Generic;
using Microsoft.MixedReality.Toolkit.Input;
public class CustomPointerMediator : DefaultPointerMediator
{
public bool FarPointersDisabled {get; set;}
public override void UpdatePointers()
{
base.UpdatePointers();
if (FarPointersDisabled)
{
foreach(var pointer in farInteractPointers)
{
pointer.IsActive = false;
}
}
}
}
请注意,此调解器扩展了 DefaultPointerMediator
,因此它适应了几乎所有默认调解器逻辑。确保您已完全应用更改 from the first commit 否则您将无法扩展 DefaultPointerMediator
.
第 3 步:告诉 MRTK 使用您的自定义指针中介
在您的指针配置文件中,将 MRTK 配置为使用自定义指针中介而不是默认指针中介。请注意,在图片中我创建了自定义指针配置文件作为自定义输入系统的一部分(请勿修改默认配置文件,否则在更新 MRTK 时您的更改可能会被覆盖)。
第 4 步:使用自定义中介打开/关闭行指针的组件
您现在可以编写您的组件,它将使用您的自定义调解器打开和关闭线指针。
public class PointerConfigurationExample : MonoBehaviour
{
/* Turns off all far interaction pointers */
public void TurnOffFarPointers()
{
Debug.Log("Line pointers off");
SetFarPointersDisabled(true);
}
public void TurnOnFarPointers()
{
Debug.Log("Line pointers on");
SetFarPointersDisabled(false);
}
private void SetFarPointersDisabled(bool isDisabled)
{
FocusProvider focusProvider = (FocusProvider) MixedRealityToolkit.InputSystem.FocusProvider;
if (focusProvider != null)
{
foreach(var mediator in focusProvider.PointerMediators)
{
// Note: you could check here to make sure you only disable pointers for hands
CustomPointerMediator myMediator = (CustomPointerMediator) (mediator.Value);
if (myMediator != null)
{
myMediator.FarPointersDisabled = isDisabled;
}
}
}
}
}
回答了我自己的问题。简单:
PointerUtils.SetMotionControllerRayPointerBehavior(PointerBehavior.AlwaysOff);
就这样吧,我去踩我的骄傲!
我正在使用 MRTK v2 并且工作正常,除了在某些时候我想关闭从运动控制器延伸到对象并提供输入的线。环顾四周并试图在 MRTK 文档中找到它(这一定很容易,对吧?),我仍在用头撞墙并且开始疼了....
MRTK 文档在此处很好地解释了如何配置它:
但我希望在脚本中执行此操作,根据需要在我的应用程序中启用和禁用它。
知道怎么做吗?
非常感谢!
好问题!这是一种对我有用的方法。您可以在此存储库中查看我的解决方案:https://github.com/julenka/MixedRealityToolkit-Unity/tree/so/linepointer_off。打开场景 Assets/TurnOffLinePointerTest.unity
然后用手模拟按下按钮。转换指针的代码 on/off 在 Assets/PointerConfigurationExample.cs
.
注意:您需要使用这种修改中介而不是直接设置的方法的原因 myPointer.IsActive = false
是因为默认中介每帧都会覆盖这些值。幸运的是,您可以自定义此行为。
第 1 步:稍微更新 MRTK 以便可以访问 PointerMediator
将实现此更改的更改 from this commit to your MRTK clone. This change updates the FocusProvider in MRTK to make the PointerMediator publicly accessible, and makes the DefaultPointerMediator extensible by updating fields to be protected instead of private, and making methods virtual. See this pull request 应用到 MRTK 中。
第 2 步:创建将关闭远指针的自定义 PointerMediator
创建一个自定义指针中介using System.Collections.Generic;
using Microsoft.MixedReality.Toolkit.Input;
public class CustomPointerMediator : DefaultPointerMediator
{
public bool FarPointersDisabled {get; set;}
public override void UpdatePointers()
{
base.UpdatePointers();
if (FarPointersDisabled)
{
foreach(var pointer in farInteractPointers)
{
pointer.IsActive = false;
}
}
}
}
请注意,此调解器扩展了 DefaultPointerMediator
,因此它适应了几乎所有默认调解器逻辑。确保您已完全应用更改 from the first commit 否则您将无法扩展 DefaultPointerMediator
.
第 3 步:告诉 MRTK 使用您的自定义指针中介
在您的指针配置文件中,将 MRTK 配置为使用自定义指针中介而不是默认指针中介。请注意,在图片中我创建了自定义指针配置文件作为自定义输入系统的一部分(请勿修改默认配置文件,否则在更新 MRTK 时您的更改可能会被覆盖)。
第 4 步:使用自定义中介打开/关闭行指针的组件
您现在可以编写您的组件,它将使用您的自定义调解器打开和关闭线指针。
public class PointerConfigurationExample : MonoBehaviour
{
/* Turns off all far interaction pointers */
public void TurnOffFarPointers()
{
Debug.Log("Line pointers off");
SetFarPointersDisabled(true);
}
public void TurnOnFarPointers()
{
Debug.Log("Line pointers on");
SetFarPointersDisabled(false);
}
private void SetFarPointersDisabled(bool isDisabled)
{
FocusProvider focusProvider = (FocusProvider) MixedRealityToolkit.InputSystem.FocusProvider;
if (focusProvider != null)
{
foreach(var mediator in focusProvider.PointerMediators)
{
// Note: you could check here to make sure you only disable pointers for hands
CustomPointerMediator myMediator = (CustomPointerMediator) (mediator.Value);
if (myMediator != null)
{
myMediator.FarPointersDisabled = isDisabled;
}
}
}
}
}
回答了我自己的问题。简单:
PointerUtils.SetMotionControllerRayPointerBehavior(PointerBehavior.AlwaysOff);