Search Unity

Getting button's coordinates.

Discussion in 'Scripting' started by DarkRain, Jan 24, 2011.

  1. DarkRain

    DarkRain

    Joined:
    Oct 7, 2010
    Posts:
    68
    I have a button on the scene (A GUI element). I hover mouse over this button. How could I get the X and Y coordinates of that button?

    I mean, when I hover my mouse over a button - some Event should trigger. I would like to get some data from that event (like coordinates). But I just don't know from where to get this data.

    Thank you for your help.
     
    Last edited: Jan 24, 2011
  2. Kemical

    Kemical

    Joined:
    Oct 4, 2010
    Posts:
    24
    GUI Elements coord are in Screen Coord, so what you seems to want is this :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class SimpleButton : MonoBehaviour {
    5.  
    6.     Rect myButtonRect = new Rect(20,20,100,50) ;
    7.    
    8.     void OnGUI()
    9.     {
    10.         if(Event.current.type == EventType.repaint  myButtonRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y)))
    11.         {
    12.             //Do on rollOver
    13.         }
    14.        
    15.         if(GUI.Button(myButtonRect, "myButton"))
    16.         {
    17.             //Do on click
    18.         }
    19.        
    20.     }
    21. }
    22.  
    23.