Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Add a Select Entry in a Event Trigger doesn't work ?

Discussion in 'Scripting' started by Diblow, Aug 22, 2018.

  1. Diblow

    Diblow

    Joined:
    Dec 9, 2015
    Posts:
    3
    Hey guys !
    I'm currently working on some UI and I'm creating a bunch of buttons by script.
    In those buttons I add an event trigger , and until now, I only add in this Event Trigger a PointerEnter Event.

    And it's working great, but now I would like to do the same with a Select Event but for some reason it doesn't seem to work.

    Here is the part of the script where I try to do that:

    Code (CSharp):
    1. //CREATE ACTION OF EVENT TRIGGER
    2.             EventTrigger trigger = cloneMusicPrefab.GetComponentInChildren<EventTrigger>();
    3.             EventTrigger.Entry entry = new EventTrigger.Entry();
    4.             entry.eventID = EventTriggerType.PointerEnter;
    5.  
    6.             entry.callback.AddListener(delegate { MouseOnButton(trackdef, trackdef); });
    7.             trigger.triggers.Add(entry);
    8.  
    9.             EventTrigger.Entry newEntry = new EventTrigger.Entry();
    10.             newEntry.eventID = EventTriggerType.Select;
    11.  
    12.             newEntry.callback.AddListener(delegate { MouseOnButton(trackdef, trackdef); });
    13.             trigger.triggers.Add(newEntry);
    The first part is for the PointerEnter and it works, the second part is for the Select and it doesn't work.

    If anyone has a solution, I would gladly take it !

    Thanks :)
     
    MilenaRocha likes this.
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    There's probably a way to do it in much more cleaner manner.

    Also, you can use Button.onClick listener instead or add events beforehand in the prefab & instantiate it instead of creating buttons.
     
  3. Diblow

    Diblow

    Joined:
    Dec 9, 2015
    Posts:
    3
    Thanks for your answer,
    but why would I use onClick when it's not what I want ? I want to do something when the mouse is on the button or the button is selected.

    Also I'm already instantiating prefabs of my buttons, but I can't put a gameobject in the BaseEventData of the Event Trigger in a prefab.
    That's why I'm doing it by script, but for a reason I don't know the Select won't work when the others work.
     
    MilenaRocha likes this.
  4. Diblow

    Diblow

    Joined:
    Dec 9, 2015
    Posts:
    3
    If anyone is interested, I managed to do what I wanted to do by creating a new script in which I use ISelectHandler and
    created a method OnSelect and adding this new script when I create my button using AddComponent.

    This way it's working fine !
     
  5. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    this is the correct approach, mainly cause of the major flaw in how Unity Event Trigger works. Try to never use them.

    Why? because that Component will catch ALL IEventSystemHander callbacks that it can possibly catch... even if the it doesn't have a listener assigned to that callback. This means that it will consume any event the eventsystem tries to send past it without bubbling it. You may not have set it up to handle onClicks but it will gladly block a parent button from receiving that onclick, or any select, submit, cancel, and move events breaking keyboard navigation
     
    Diblow likes this.