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

[Solved] Finding out wich text I clicked?

Discussion in 'Scripting' started by Napivo, Feb 28, 2015.

  1. Napivo

    Napivo

    Joined:
    Feb 9, 2015
    Posts:
    39
    Here we go again.

    I have several texts that have been dynamically created in a panel. They have been instantiated from a prefab.

    Now when the Pointer Up event gets triggered I want to find out which text is clicked, Not only the name as their might be duplicates. I tried :

    Code (CSharp):
    1. public void TextPointerUP(PointerEventData eventData)
    but unity does not accept this as a valid function to send events to. I tried object and GameObject as well as the BaseEventData.

    And then I found this

    Unity 4.6 GUI - Dynamically change the functions called by GUI elements

    I'd implement it but it looks overkill to me. Is there an easier way?
     
  2. Napivo

    Napivo

    Joined:
    Feb 9, 2015
    Posts:
    39
    The short answer is you can't :)

    The long answer is use a button instead of text that solved also my other problem of having to group 3 items together (1 icon and 2 texts) for alignment.

    Adding events isn't even that hard.

    I wrote this little code with the help of the link above (Actually just copied it and made it into a static class)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. using UnityEngine.EventSystems;
    5.  
    6.     //Create an event delegate that will be used for creating methods that respond to events
    7.     public delegate void EventDelegate(BaseEventData baseEvent);
    8.  
    9.     public static class EventHelper {
    10.  
    11.         public static void CreateEventTriger(
    12.                           Component component,
    13.                           EventDelegate delegateFunction,
    14.                           EventTriggerType eventType)
    15.         {
    16.             //Get the event trigger attached to the UI object
    17.             EventTrigger eventTrigger = component.GetComponent<EventTrigger> ();
    18.  
    19.             //Create a new entry. This entry will describe the kind of event we're looking for
    20.             // and how to respond to it
    21.             EventTrigger.Entry entry = new EventTrigger.Entry ();
    22.      
    23.             //This event will respond to an event
    24.             entry.eventID = eventType;
    25.      
    26.             //Create a new trigger to hold our callback methods
    27.             entry.callback = new EventTrigger.TriggerEvent ();
    28.      
    29.             //Create a new UnityAction, it contains our DropEventMethod delegate to respond to events
    30.             UnityEngine.Events.UnityAction<BaseEventData> callback =
    31.                 new UnityEngine.Events.UnityAction<BaseEventData> (delegateFunction);
    32.      
    33.             //Add our callback to the listeners
    34.             entry.callback.AddListener (callback);
    35.      
    36.             //Add the EventTrigger entry to the event trigger component
    37.             eventTrigger.delegates.Add (entry);
    38.         }
    39.     }
    40.  
    Everything now works as it should.