At the beginning of my scene I set up all my OnClick- and OnFocus-Listeners (which you see in the code snippet at the end of this post).
As I'm using the given mrtk-prefab-buttons, which have the Interactable
script on them, there is already under Receivers
an InteractableOnFocusReceiver
, you see in the screenshot below.
My issue:
By adding an receiver (via the code below), everything works fine, but I get these message:
I guess this happens because there is already a OnFocusReceiver
and I'm adding another one or I missing some extra data/component. So I tried to access somehow the OnFocusReceiver
that already exists, but could not find a way to achieve that. Trying comp.GetReceiver<InteractableOnFocusReceiver>();
is giving me null
even though in the unity editor I see a OnFocusReceiver. Does sb know how to access this receiver so I can just add to it another trigger-method?
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);
}
To add a method for OnFocusReceiver on runtime, you just need to add a listener to the existing receiver:
var onFocusReceiver = gameObject.GetComponent<Interactable>().GetReceiver<InteractableOnFocusReceiver>();
onFocusReceiver.OnFocusOn.AddListener(() => Debug.Log("ONtest"));
onFocusReceiver.OnFocusOff.AddListener(() => Debug.Log("OFFtest"));