Search Unity

GUI Texture Button

Discussion in 'Immediate Mode GUI (IMGUI)' started by da7thSkull, Feb 23, 2010.

  1. da7thSkull

    da7thSkull

    Joined:
    Feb 15, 2010
    Posts:
    22
    hello everyone

    hope u feel fine...

    i post the code, because i don't know to do a scale, of the Button (Texture) if the Mouse are over. also, the x position should transform 10 pixel down.

    allso, i whan't to know, how or is it possible, to snipe the name of the game object, on whitch the script is attached, and give the name as text output to the player?


    Code (csharp):
    1.  var normalTexture : Texture;
    2.  var hoverTexture : Texture;
    3.  var pressedTexture : Texture;
    4.  var messagee : GameObject;
    5.  var message = "ButtonPressed";
    6.  var caro : String;
    7.  
    8.  
    9.  private var state = 0;
    10.  private var myGUITexture : GUITexture;
    11.  
    12.  myGUITexture = GetComponent(GUITexture);
    13.  
    14.  function OnMouseEnter()
    15.  {
    16.     state++;
    17.     if (state == 1)
    18.         myGUITexture.texture = hoverTexture;
    19.  }
    20.  
    21.  function OnMouseDown()
    22.  {
    23.     state++;
    24.     if (state == 2)
    25.         myGUITexture.texture = pressedTexture;
    26.         caro = "_Caro_Front_";
    27.         GameObject.Find(caro).renderer.sharedMaterial.mainTexture = normalTexture;
    28.  }
    29.  
    30.  function OnMouseUp()
    31.  {
    32.     if (state == 2)
    33.     {
    34.         state--;
    35.         if (messagee)
    36.             messagee.SendMessage(message, gameObject);
    37.     }
    38.     else
    39.     {
    40.         state --;
    41.         if (state < 0)
    42.             state = 0;
    43.     }
    44.     myGUITexture.texture = normalTexture;
    45.  }
    46.  
    47.  function OnMouseExit()
    48.  {
    49.     if (state > 0)
    50.         state--;
    51.     if (state == 0)
    52.         myGUITexture.texture = normalTexture;
    53.  }

    cheers

    michael
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    In the OnGUI function, you can check if the mouse is inside the button using Rect.Contains on the mouse position and the button rectangle. You can then just swap between two different rectangles depending on whether the mouse is inside or not:-
    Code (csharp):
    1. var actualButtonRect: Rect;
    2. private var buttonNormalRect = new Rect(0, 0, 100, 100);
    3. private var buttonHoverRect = new Rect(10, 10, 120, 120); // Scaled and offset.
    4.  
    5. function Start() {
    6.     actualButtonRect = buttonNormalRect;
    7. }
    8.  
    9.  
    10. function OnGUI() {
    11.     var mouse: Vector2 = Input.mousePosition;
    12.    
    13.     if (actualButtonRect.Contains(new Vector2(mouse.x, Screen.height - mouse.y))) {
    14.         print("Hovering");
    15.         actualButtonRect = buttonHoverRect;
    16.     } else {
    17.         actualButtonRect = buttonNormalRect;
    18.     }
    19.    
    20.     if (GUI.Button(actualButtonRect, "Hello")) {
    21.         // Do something...
    22.     }
    23. }
     
  3. da7thSkull

    da7thSkull

    Joined:
    Feb 15, 2010
    Posts:
    22
    hello andeeee

    again thx 4 your quick reply...

    i found a solution that work, but i will test your version also...

    here are my code:
    Code (csharp):
    1.  
    2. //da7thSkull Color Button
    3.  
    4. var hover : String;
    5. var icon : Texture2D;
    6. var caro : String;
    7. var White : GUISkin;
    8. var xpos : int = 10;
    9. var ypos : int = 120;
    10. var output : String;
    11.  
    12. function OnGUI () {
    13.     GUI.skin = White;
    14.     GUI.DrawTexture (Rect (xpos+1,ypos+1,18,43), icon);
    15.   if (GUI.Button(Rect(xpos,ypos,20,45),GUIContent(icon, "Color")))
    16.   {
    17.     DoSomething();
    18.   }
    19.   hover=GUI.tooltip;
    20.   if(hover=="Color")
    21.   {
    22.     GUI.Label(Rect(xpos,ypos+50,180,23), output);
    23.   }
    24.  
    25. }
    26.  
    27. function DoSomething () {
    28.  
    29.         caro = "_Caro_Front_";
    30.         GameObject.Find(caro).renderer.sharedMaterial.mainTexture = icon;
    31. }
    32.  

    cheers

    michael