L
L
Luc1FeR2015-12-20 21:24:53
Unity
Luc1FeR, 2015-12-20 21:24:53

How to check if the mouse is hovering over a button in Unity?

Hello. Maybe a stupid question, but I had it. There is a window with buttons that are created programmatically. More or less like this:

public Texture one;  // Какая-то текстура

void OnGUI(){
    GUI.Window (2, new Rect (Screen.width * 0.6f, 0, Screen.width * 0.4f, 
 			Screen.height), WindowCharacterUpgrade, "");
}

void WindowCharacterUpgrade(int id){
  if (GUI.Button (new Rect (((Screen.width * 0.4f) / 2) - 20, 20, 40, 40), one)) 
    // Какое-то действие
}

BF4XPu2.png
Is it possible to track the mouse pointer over this button, either created in the same window or not? If yes, then how, otherwise I can’t understand ... or will I have to create all the elements not through code and do as it is written just below?
The search only returned answers for non-code generated GUI elements, in which this process was tracked either via onMouseEnter() or via events.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2015-12-21
@Luc1FeR

Don't use OnGUI, it's old, slow and cumbersome. Use the new UI, you can start with the official tutorial or guide .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question