Hi there.
I’m experimenting with delegates and envents for the first time after watching this Events - Unity Learn.
Now… let’s say I have something similar to the EventManager from the link:
public class ControllerManager : MonoBehaviour {
#region events
public delegate void IdlePrimaryIndexTriggerDownAction();
public event IdlePrimaryIndexTriggerDownAction OnIdlePrimaryTriggerDownAction;
#endregion
/*...................*/
void HandleIdleAnyTriggerDown()
{
if (OnIdlePrimaryTriggerDownAction != null)
OnIdlePrimaryTriggerDownAction();
}
private void Awake()
{
m_inputManager = FindObjectOfType<InputManager>();
/*....................*/
}
Where HandleIdleAnyTriggerDown() is already being called and working as expected. Now assume that I need two instances of ControllerManager (in my case because ecah one of them controlls one different Oculus Touch).
I also have my InteractiveItem class like this:
/*................*/
void OnEnable()
{
controllerManagerLeft.OnIdlePrimaryTriggerDownAction += DoSomething;
controllerManagerRight.OnIdlePrimaryTriggerDownAction += DoSomething;
}
void OnDisable()
{
controllerManagerLeft.OnIdlePrimaryTriggerDownAction += DoSomething;
controllerManagerRight.OnIdlePrimaryTriggerDownAction += DoSomething;
}
void DoSomething()
{
//do things here
}
/*................*/
This one is also working as expected with DoSomething being called when evet happens on any of my two controllers.
My point is… is there any way to know from InteractiveItem which instace of ControllerManger triggered the event? So far the best way I find to do this is doing something like this:
[code=CSharp]
public class ControllerManager : MonoBehaviour {
InteractiveItem currentTarget;
#region events
public delegate void IdlePrimaryIndexTriggerDownAction();
public event IdlePrimaryIndexTriggerDownAction OnIdlePrimaryTriggerDownAction;
#endregion
/*...................*/
void Update () {
/**keep updated the object target by this controller*/
}
void HandleIdleAnyTriggerDown()
{
if (OnIdlePrimaryTriggerDownAction != null){
currentTarget.SetWhoTriggered (this);
OnIdlePrimaryTriggerDownAction();
}
}
private void Awake()
{
m_inputManager = FindObjectOfType<InputManager>();
/*....................*/
}
public class InteractiveItem
/*................*/
ControllerManager whoTriggered;
public void SetWhoTriggered (ControllerManager who) {
whoTriggered = who;
}
void OnEnable()
{
controllerManagerLeft.OnIdlePrimaryTriggerDownAction += DoSomething;
controllerManagerRight.OnIdlePrimaryTriggerDownAction += DoSomething;
}
void OnDisable()
{
controllerManagerLeft.OnIdlePrimaryTriggerDownAction += DoSomething;
controllerManagerRight.OnIdlePrimaryTriggerDownAction += DoSomething;
}
void DoSomething()
{
//now before doing something I can check which controller triggered everything in whoTriggered var.
//do things here
}
I guess something like this should work but it doesn’t seam as a ‘clean’ solution. Besides the whole point of using events for me was avoiding hierarchies and, working like this, every interactive object will need to be child of InteractiveItem…
…So… is there any way to recover the instance of the caller event or, maybe, a better workaround?
Thanks!