MRTK V2 - 如何访问接收器?
MRTK V2 - How to access Receivers?
在我的场景开始时,我设置了我所有的 OnClick 和 OnFocus 侦听器(您可以在本 post 末尾的代码片段中看到)。
当我使用给定的 mrtk-prefab-buttons 时,它们上面有 Interactable
脚本,Receivers
下已经有一个 InteractableOnFocusReceiver
,您在下面的屏幕截图中看到。
我的问题:
通过添加接收器(通过下面的代码),一切正常,但我收到这些消息:
我猜这是因为已经有一个 OnFocusReceiver
而我正在添加另一个,或者我缺少一些额外的 data/component。所以我试图以某种方式访问已经存在的 OnFocusReceiver
,但找不到实现该目标的方法。尝试 comp.GetReceiver<InteractableOnFocusReceiver>();
给了我 null
,即使在统一编辑器中我看到了 OnFocusReceiver。 某人知道如何访问此接收器以便我可以向其添加另一个触发方法吗?
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);
}
要在运行时为 OnFocusReceiver 添加一个方法,您只需要为现有的接收器添加一个侦听器:
var onFocusReceiver = gameObject.GetComponent<Interactable>().GetReceiver<InteractableOnFocusReceiver>();
onFocusReceiver.OnFocusOn.AddListener(() => Debug.Log("ONtest"));
onFocusReceiver.OnFocusOff.AddListener(() => Debug.Log("OFFtest"));
在我的场景开始时,我设置了我所有的 OnClick 和 OnFocus 侦听器(您可以在本 post 末尾的代码片段中看到)。
当我使用给定的 mrtk-prefab-buttons 时,它们上面有 Interactable
脚本,Receivers
下已经有一个 InteractableOnFocusReceiver
,您在下面的屏幕截图中看到。
我的问题:
通过添加接收器(通过下面的代码),一切正常,但我收到这些消息:
我猜这是因为已经有一个 OnFocusReceiver
而我正在添加另一个,或者我缺少一些额外的 data/component。所以我试图以某种方式访问已经存在的 OnFocusReceiver
,但找不到实现该目标的方法。尝试 comp.GetReceiver<InteractableOnFocusReceiver>();
给了我 null
,即使在统一编辑器中我看到了 OnFocusReceiver。 某人知道如何访问此接收器以便我可以向其添加另一个触发方法吗?
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);
}
要在运行时为 OnFocusReceiver 添加一个方法,您只需要为现有的接收器添加一个侦听器:
var onFocusReceiver = gameObject.GetComponent<Interactable>().GetReceiver<InteractableOnFocusReceiver>();
onFocusReceiver.OnFocusOn.AddListener(() => Debug.Log("ONtest"));
onFocusReceiver.OnFocusOff.AddListener(() => Debug.Log("OFFtest"));