1
\$\begingroup\$

So, in my game I create a bunch of UI Text game objects dynamically (in the C# script), but I just can not find out how to properly detect Left and Right mouse clicks on these UI Text that were dynamically generated.

I have tried adding Colliders and Colliders2D to the UI Text game objects for raycasting, but with no luck. And differently from UI Button, it seems that Unity has left proper "on mouse click" functions out from UI Text

Can anyone help out on how to accomplish that?

PS: no, I can not use UI Buttons, it has to be Texts. And the UI Texts have to be generated on the fly, not in the editor

\$\endgroup\$
  • \$\begingroup\$ You can use ui button with empty image and text attached or text as child \$\endgroup\$ – Hamza Hasan Feb 6 '16 at 20:04
  • \$\begingroup\$ This is what you want youtube.com/watch?v=YAnLL1MCNoI \$\endgroup\$ – Savlon Feb 6 '16 at 22:59
1
\$\begingroup\$

I suppose that when you create the UI Text objects, you are adding to them a RectTransform component, just like I answered you here: How to dynamically create an UI text Object in Unity 5?. Knowing that, one simple and very efficient solution for what you want is to detect if the desired mouse button was pressed AND if mouse cursor is over the RecTransform of the UI Text.

To do that last bit, you just get the 4 vertices of the UI Text's RectTransform at Screen Space and then run a plain and usual test of whether the mouse cursor position (which is given in Screen Space by default) is contained within the 2D rectangle formed by those 4 vertices.

Something like this:

//retrieves the position of the UI Text's RectTransform:
float rectX = yourUItextgo.GetComponent<RectTransform>().anchoredPosition.x;
float rectY = yourUItextgo.GetComponent<RectTransform>().anchoredPosition.y;

//retrieves half width and half height of the UI Text's RectTransform:
float rec_width = yourUItextgo.GetComponent<RectTransform>().sizeDelta.x*0.5f;
float rec_height = yourUItextgo.GetComponent<RectTransform>().sizeDelta.y*0.5f;

//calculates the 4 vertices of the UI Text's RectTransform:
Vector2 _X = new Vector2(rectX - rec_width, rectY - rec_height );
Vector2 _Y = new Vector2(rectX + rec_width, rectY - rec_height );
Vector2 _W = new Vector2(rectX - rec_width, rectY + rec_height );
Vector2 _Z = new Vector2(rectX + rec_width, rectY + rec_height );

//here you call any function that detects whether a 2D points is within a 2D rectangle:
bool wasoverUIText = YourPointInRectangleFunction(Input.mousePosition , _X, _Y, _W, _Z);

Then, it becomes easy to detect a left, right, middle or even double-clicks: you just have to put the usual Input inside an ifcondition, followed by && wasoverUIText).

For the right mouse button, for example, that would mean:

if(Input.GetMouseButtonDown(1) && wasoverUIText )
{
    //run whatever you intended to run when the UI Text is clicked
}
\$\endgroup\$
0
\$\begingroup\$

Attach this script to the "UI Text prefab" that you are creating Dynamically.

using UnityEngine;
using UnityEngine.EventSystems;

public class TextClick : MonoBehaviour,IPointerClickHandler
{
    #region IPointerClickHandler implementation
    public void OnPointerClick (PointerEventData eventData)
    {
    if (eventData.button == PointerEventData.InputButton.Left) // Checks if left mouse button is pressed
      {
        print ("left");
      } 
    else if (eventData.button == PointerEventData.InputButton.Right) // checks if right mouse button is pressed
      { 
        print ("Right");
      }

    }
    #endregion

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.