- Home /
Is there a way to know when mouse is 'hovering' over a Toggle?
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+
Answer by Mmmpies · Feb 26, 2015 at 09:07 AM
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.
Answer by maccabbe · Feb 26, 2015 at 09:05 AM
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");
         }
     }
 }
Thanks, this would work for old (4.5-) GUI code, I'm looking for 'new GUI' solution
Your answer
 
 
             Follow this Question
Related Questions
Toggle Event passing bool [4.6+] 2 Answers
How to have a callback when a toggle is clicked in a toggle group? 3 Answers
Is there an event other than OnValueChanged for Toggle UI? 0 Answers
Toggle's 'On Value Changed' checkbox is shown for bool parameter function 0 Answers
How to make a toggle not be deselected within a toggle group? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                