Scripting UI elements without EventSystem and Event Triggers ?

Hello everyone!
Is there a way to script UI elements without the need to use Event Triggers on them?
As far as I know, I have to use an EventSystem object in the scene and an Event Trigger component on a UI element in order to get scripts running.
Ideally, I would just like to attach a script to a UI element and call functions like
OnMouseDown (), OnMouseUp (), OnMouseOver (), OnMouseExit (), OnClick ()
directly without the need of referencing the functions in the Event Trigger component.
I already tried using different colliders which might be missing, but it didn’t work.
Does the new UI system work differently than NGUI?
Because when using NGUI, one can attach a collider to a UI element and call functions directly
( OnClick (), etc ).

Many thanks in advance,
Greetings,
Shushustorm

Yes, it is possible. You have to import UnityEngine.EventSystems on your Script. Then the class has to use the IPointerDownHandler and/or IPointerUpHandler. For either you have to implement the corresponding functions. Here is a sample. I put the Script on a uGUI Button.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;  // needs to be imported

public class ButtonAction : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
   // the following Methods have to be implemented
   public void OnPointerDown(PointerEventData eventData) {
       // Do stuff when object is touched/clicked
    }

    public void OnPointerUp(PointerEventData data) {
        // do stuff when touch/click is released
    }
}
3 Likes

There is a full list of the supported event system interfaces here.

Note that you don’t even have to have this on UI. As long as there is an appropriate raycaster in the scene that will pick up your object this will work in any component.

2 Likes

is there a way that i can attach a script to the canvas and let the canvas handle what component it is

Yes and no. First, why do you want to do that? It’s not how Unity is designed to work. For example if the script you have attached to your canvas has the IPointerEnterHandler interface implemented, OnPointerEnter will be raised/called when the mouse enters (goes over) the canvas. It won’t raise the event when the mouse enters a button on that canvas. BUT you can call a centralized script from the script you’ve attached to your button (who has OnPointerEnter implemented).

From this post and your previous one it sounds like you trying to figure out how to architect your scripts so they can talk to each other. Is that correct?

yes thats what im trying to figure out and to fix talking script to make it more dynamically thanks for the advice, do you have any tips, or what im trying to do is possible or far from truth?

Yes what you are trying to do is possible. I might suggest studying MVC, MVVM or other architectural patterns. And I would study them not in the context of Unity. Bringing experience from other disciplines helps when applying it to Unity.

But a small bit to help you on your way. Create an empty game object. Attach a script to it called “GameManager”. On that script create three methods. “public void HandleButton1” 2 3.

Then create three buttons on your canvas. Wire the buttons to those events. Now you have a centralized script that knows when each button is clicked.