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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scripting UI elements without EventSystem and Event Triggers ?

Discussion in 'UGUI & TextMesh Pro' started by Shushustorm, Nov 28, 2014.

  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    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
     
  2. habsi70

    habsi70

    Joined:
    Jan 30, 2013
    Posts:
    25
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;  // needs to be imported
    4.  
    5. public class ButtonAction : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
    6.    // the following Methods have to be implemented
    7.    public void OnPointerDown(PointerEventData eventData) {
    8.        // Do stuff when object is touched/clicked
    9.     }
    10.  
    11.     public void OnPointerUp(PointerEventData data) {
    12.         // do stuff when touch/click is released
    13.     }
    14. }
     
    Corvinuz1227, Kiwasi and ensiferum888 like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    Corvinuz1227 and habsi70 like this.
  4. Corvinuz1227

    Corvinuz1227

    Joined:
    Mar 2, 2014
    Posts:
    29
    is there a way that i can attach a script to the canvas and let the canvas handle what component it is
     
  5. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    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?
     
  6. Corvinuz1227

    Corvinuz1227

    Joined:
    Mar 2, 2014
    Posts:
    29
    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?
     
  7. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    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.