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 if
condition, 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
}