I need to do something when the mouse hovers over a button, particularly a Toggle. Is there an event I can implement or something? Unity 4.6+
Yep, Add Component → EventTrigger
Then add an event for PointerEnter and PointerExit.
Now you haven’t said what you want to do but if script based write the script with a public function for each event.
Drag that script onto a gameObject e.g. The toggle. Drag the toggle onto the event slots and from the dropdown select your script → the relevant function.
If you need to know when the mouse hovers over a specific button you can test if the rect used to create the button contains the object.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Texture btnTexture;
Rect rectGUI;
Rect rectUpdate;
void Start() {
rectGUI=new Rect(0, 0, 100, 100);
// unity draws GUI from top right but mousePosition is from the bottom left
rectUpdate=new Rect(rectGUI.x, Screen.height+rectGUI.x-rectGUI.height, rectGUI.width, rectGUI.height);
}
void Update() {
if(rectUpdate.Contains(Input.mousePosition)) {
Debug.Log("Scrolled over button");
}
}
void OnGUI() {
if(GUI.Button(rectGUI, btnTexture)) {
Debug.Log("Clicked the Button");
}
}
}