4

In the new Unity3D UI (Unity > 4.6), I'm trying to create a simple script I can attach to a UI component (Image, Text, etc) that will allow me to wedge in a custom tooltip handler. So what I need is to capture a PointerEnter and PointerExit on my component. So far I'm doing the following with no success. I'm seeing the EVentTrigger component show up but can't get my delegates to fire to save my life.

Any ideas?

public class TooltipTrigger : MonoBehaviour {

  public string value;

  void Start() {

    EventTrigger et = this.gameObject.GetComponent<EventTrigger>();

    if (et == null)
      et = this.gameObject.AddComponent<EventTrigger>();

    EventTrigger.Entry entry;
    UnityAction<BaseEventData> call;

    entry = new EventTrigger.Entry();
    entry.eventID = EventTriggerType.PointerEnter;
    call = new UnityAction<BaseEventData>(pointerEnter);
    entry.callback = new EventTrigger.TriggerEvent();
    entry.callback.AddListener(call);
    et.delegates.Add(entry);

    entry = new EventTrigger.Entry();
    entry.eventID = EventTriggerType.PointerExit;
    call = new UnityAction<BaseEventData>(pointerExit);
    entry.callback = new EventTrigger.TriggerEvent();
    entry.callback.AddListener(call);
    et.delegates.Add(entry);
  }

  private void pointerEnter(BaseEventData eventData) {
    print("pointer enter");
  }
  private void pointerExit(BaseEventData eventData) {
    print("pointer exit");
  }

}

Also... the other method I can find when poking around the forums and documentations is to add event handlers via interface implementations such as:

public class TooltipTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {

  public string value;

  public void OnPointerEnter(PointerEventData data) {
    Debug.Log("Enter!");
  }

  public void OnPointerExit(PointerEventData data) {
    Debug.Log("Exit!");
  }

}

Neither of these methods seems to be working for me.

slumtrimpet
  • 3,159
  • 2
  • 31
  • 44

1 Answers1

2

Second method (implementation of IPointerEnterHandler and IPointerExitHandler interfaces) is what you're looking for. But to trigger OnPointerEnter and OnPointerExit methods your scene must contain GameObject named "EventSystem" with EventSystem-component (this GameObject created automatically when you add any UI-element to the scene, and if its not here - create it by yourself) and components for different input methods (such as StandaloneInputModule and TouchInputModule).

Also Canvas (your button's root object with Canvas component) must have GraphicRaycaster component to be able to detect UI-elements by raycasting into them.

I just tested code from your post and its works just fine.

Utamaru
  • 848
  • 7
  • 19
  • Curious, what exact Unity editor version are you using? I'm on 5.0.2p2. Don't think it should matter, but I could swear I've got everything setup exactly as you describe and I'm still getting nothing on my end. (all the other 'normal' UI events work just fine, only issue is the programatic one I'm trying to setup here) – slumtrimpet May 26 '15 at 13:35
  • I'm at 5.0.1f1. Can you show me your scene's hierarchy and what exactly you're doing with object, to which you attached script with IPointHandlers? – Utamaru May 26 '15 at 13:45
  • 1
    Another useful check that you can do is to Select the EventSystem game object and look at the inspector, on the bottom you should see all the events as they change, and if your proejct is set up correctly you should see the "pointerEnter" name of the object that the pointer enter is being called on (if any) in real time. – Colton White May 26 '15 at 14:25
  • Shame... intense feeling of shame. My console output was toggled to only show errors and not normal log messages. Thanks for being my sanity check on this one and making me check to find my stupid error. :-) @Utamaru – slumtrimpet May 26 '15 at 14:33
  • Thanks for that tip, I actually hadn't realized that one either. That'll come in handle on many capers in the future. @ColtonWhite – slumtrimpet May 26 '15 at 14:38