How to set up multiple Delegate overrides ?
I am new to Delegates, and must be doing something stupid?! I'm trying to make a script that when attached to a GameObject with a standard Unity "Event Trigger" will make the GameObject perform different things when for example clicked on and dragged on.
I think it should be straight forward, and it looks like everything is working (at runtime there's added Events under the Event Trigger, and if I only have ONE delegate-thingey, it works just fine..
But when I try and make 2 individual actions, the latter overrides the first one I make?!?
using UnityEngine;
using UnityEngine.EventSystems;
public class eventDelegateTest : MonoBehaviour
{
void Start()
{
//General Setup
EventTrigger trigger = GetComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry();
// A: This works alone
// I want this to happen on "PointerDown"
entry.eventID = EventTriggerType.PointerDown;
entry.callback.AddListener((data) => { delegate_PointerDown((PointerEventData)data); });
trigger.triggers.Add(entry);
// B: When this is added, A is not working, but A is ALSO called on BeginDrag?!?
// I want this to happen on "BeginDrag"
entry.eventID = EventTriggerType.BeginDrag;
entry.callback.AddListener((data) => { delegate_BeginDrag((PointerEventData)data); });
trigger.triggers.Add(entry);
}
public void delegate_PointerDown(PointerEventData data)
{
// This Only working when theres no B
}
public void delegate_BeginDrag(PointerEventData data)
{
// This is working fine
}
}
Answer by hexagonius · Sep 26, 2017 at 11:49 AM
You have to create a new Entry for B. Right now you're ending up with two callbacks on one id.
Your answer
Follow this Question
Related Questions
Reverse Unity Event possible ? 1 Answer
UnityEngine.GameObject' does not contain a definition for `SetSelectedGameObject' 0 Answers
UnityEvent not showing extension methods 2 Answers
Is it OK to create lots of event listeners? 0 Answers
How to detect if button is created under a mouse pointer taking into account UI hierarchy? 0 Answers