【发布时间】:2020-03-10 14:11:20
【问题描述】:
在我的场景开始时,我设置了所有的 OnClick- 和 OnFocus-Listeners(您可以在本文末尾的代码 sn-p 中看到)。
当我使用给定的 mrtk-prefab-buttons 时,它们上面有 Interactable 脚本,在 Receivers 和 InteractableOnFocusReceiver 下已经存在,你可以在下面的屏幕截图中看到。
我的问题:
通过添加接收器(通过下面的代码),一切正常,但我收到以下消息:
我想这是因为已经有一个OnFocusReceiver 并且我正在添加另一个,或者我缺少一些额外的数据/组件。所以我试图以某种方式访问已经存在的OnFocusReceiver,但找不到实现这一目标的方法。尝试comp.GetReceiver<InteractableOnFocusReceiver>(); 给了我null,即使在统一编辑器中我看到了 OnFocusReceiver。 sb 是否知道如何访问此接收器,以便我可以添加另一个触发方法?
public static void ManageListenerForFocus(
bool addReceiver,
Interactable comp,
Listener methodForFocusEnter,
Listener methodForFocusExit)
{
var onFocusReceiver = comp.AddReceiver<InteractableOnFocusReceiver>();
if (addReceiver)
{
onFocusReceiver.OnFocusOn.AddListener(() => methodForFocusEnter(comp));
onFocusReceiver.OnFocusOff.AddListener(() => methodForFocusExit(comp));
}
else
{
onFocusReceiver.OnFocusOn.RemoveListener(() => methodForFocusEnter(comp));
onFocusReceiver.OnFocusOff.RemoveListener(() => methodForFocusExit(comp));
}
}
public static void ManageListenerForClicks(
bool addListener,
Interactable comp,
UnityAction actionForOnClick)
{
if (addListener)
comp.OnClick.AddListener(actionForOnClick);
else
comp.OnClick.RemoveListener(actionForOnClick);
}
【问题讨论】:
-
如果您想在这个游戏对象上添加另一个触发方法,您应该在 OnFocusOn() 列表中单击“+”并选择每次都会调用的函数。为什么要在同一个游戏对象下的 Interactable 类中添加另一个?您能否提供有关您的业务需求的更多信息?
-
我只想知道如何在运行时添加一个由
OnFocus触发的方法。而且因为预制按钮已经在interactable-component 上有一个接收器(你在屏幕截图中看到)我只想添加一个方法而不是添加一个新的接收器等等......
标签: unity3d augmented-reality hololens mrtk