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. Dismiss Notice

Question why event trigger gameobject does not work ?

Discussion in 'Scripting' started by pitibonom, Sep 16, 2021.

  1. pitibonom

    pitibonom

    Joined:
    Aug 17, 2010
    Posts:
    188
    no info, no examples on this, no help....

    does anyone know how this component work ?

    I'd be interrested in it for getting clicks from a canvas...

    thanks :)
     
  2. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    Sometimes old versions of the manual have the pages you seek:
    https://docs.unity3d.com/2018.4/Documentation/ScriptReference/EventSystems.EventTrigger.html
    https://docs.unity3d.com/2018.4/Documentation/Manual/script-EventTrigger.html

    Here is a handy function I use often:
    Code (CSharp):
    1.  
    2. protected void addEventTrigger(EventTrigger eventTrigger, EventTriggerType type, UnityEngine.Events.UnityAction<BaseEventData> call)
    3. {
    4.    EventTrigger.Entry entry = new EventTrigger.Entry();
    5.    entry.eventID = type;
    6.    entry.callback.AddListener(call);
    7.    eventTrigger.triggers.Add(entry);
    8. }
    9.  
    10. // usage
    11. addEventTrigger(GetComponent<EventTrigger>(), EventTriggerType.PointerDown, OnPointerDown);
    12.  
     
  3. pitibonom

    pitibonom

    Joined:
    Aug 17, 2010
    Posts:
    188
    Thanks @_geo__ for this answer :)

    Yes triggers are candies when using scripting.
    However i was not talking about those ones but about those ones:
    upload_2021-9-17_15-12-37.png

    the component sounds good for what i need but there's no way to make it call any of the GO's method....
    So i got 2 conclusions:
    - i'm dumb and missing some obvious thing
    or
    - this component is just fake and useless^^

    Any idea ?

    thanks and happy unitying !
     
  4. pitibonom

    pitibonom

    Joined:
    Aug 17, 2010
    Posts:
    188
    okay.... i answer to myself....

    This component reacts to only UI clickable elements ( like an image or raw image ) whith the 'clickable' flag active.

    It appears that no canva is able to catch and dispatch events.... this is too bad as this means
    that no events can be raised without some UI content...
    Now whith an image UI, not set and with a color with alpha=0, the event trigger works as intended :)

    Happy unitying !
     
  5. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    Oh, I did not know you were referring to adding the handlers in the inspector.

    The script I posted uses the very same component. The only difference is that the handlers are added via script. The culprit was (as you identified correctly) the fact that there was nothing for the GraphicsRaycaster to hit. As the name suggests it will trigger on anything which derives from Graphic.

    To fake a clickable area I have another handy component for you (to avoid that alpha zero image trickery). Using this you can also implement custom shapes like circles, stars, ... .

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4.     public class Imaginary : MaskableGraphic
    5.     {
    6.         public override bool Raycast(Vector2 sp, Camera eventCamera)
    7.         {
    8.             // This just checks if the raycast hits the rect but you can add any custom shape or criteria here.
    9.             return base.Raycast(sp, eventCamera);
    10.         }
    11.  
    12.         protected override void OnPopulateMesh(VertexHelper vh)
    13.         {
    14.             vh.Clear();
    15.         }
    16.     }
    17.  
    Cheers!
     
    pitibonom likes this.