Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How can i separate from Custom GUI, normal and hoover function

Discussion in 'Scripting' started by Metalbreath, Mar 29, 2013.

  1. Metalbreath

    Metalbreath

    Joined:
    Jul 27, 2012
    Posts:
    90
    Hello everyone,

    I wanted to ask if there is a way to separate normal from hoovered function.
    i have this code
    Code (csharp):
    1.  if(GUI.Button(new Rect(50, 80 , 300 , 100),"", gSkin.GetStyle("Start_Button")))
    but for example, when its normal i want the button to be found at Rect (50, 80 , 300,100)
    and when mouse hoover over the button, to be change to Rect (80 , 80, 300, 1000

    Is that possible in Unity?
    I have both Images (normal and hoover) in a custom Element (GUISkin)
     
  2. stridervan

    stridervan

    Joined:
    Nov 11, 2011
    Posts:
    141
    It is possible in unity but you have to write a custom Button control yourself, here I'll save you the trouble.
    There are two ways you can either create a custom control or just check the mouse position do this

    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.   var btnRect = new Rect(0, 0, 100, 20);
    5.   if(btnRect.contains(Event.current.mousePosition))
    6.   {
    7.     GUI.Button(btnRect, "Button", style);
    8.   }
    9.   else
    10.   {
    11.     var hoverRect = new Rect(50, 50, 100, 20);
    12.     GUI.Button(hoverRect, "Button", style);
    13.   }
    14. }
    15.