Search Unity

How do you show an image on a button click?

Discussion in 'Scripting' started by Gwolf-Studios, Jan 19, 2013.

  1. Gwolf-Studios

    Gwolf-Studios

    Joined:
    Oct 7, 2012
    Posts:
    30
    I'm making a animal RPG and a map button was added using a GUI button so people don't get lost much. The only problem is that I don't know how to make the picture of the map appear when you press the map button and disappear when you press it again. Can anybody help me wit this? Here's my script already:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Map : MonoBehaviour {
    5.  
    6.     public Texture2D icon;
    7.  
    8.     void OnGUI () {
    9.         if (GUI.Button (new Rect (10,10, 100, 50), icon)) {
    10.             print ("you clicked the map icon");
    11.         }
    12.     }
    13. }
    I also wanted it so the map icon only shows the texture, I didn't want the grey-ish looking box around the map image:

     
  2. Timer

    Timer

    Joined:
    Jan 14, 2013
    Posts:
    35
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Map : MonoBehaviour {
    5.  
    6.     public Texture2D icon;
    7.     private bool showIcon = true;
    8.  
    9.     void OnGUI () {
    10.  
    11.         if (GUI.Button (new Rect (10,10, 100, 50), icon)  showIcon) {
    12.             print ("you clicked the map icon");
    13.             showIcon = false;
    14.         }
    15.        
    16. if (GUI.Button (new Rect (10,10, 100, 50))  !showIcon) {
    17.  
    18.             print ("you clicked the map icon");
    19.             showIcon = true;
    20.  
    21.         }
    22.  
    23.     }
    24.  
    25. }
    there are other ways also like using GUIStyles and using GUITextures.
     
  3. ModStoryGames

    ModStoryGames

    Joined:
    Apr 27, 2012
    Posts:
    179
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Map : MonoBehaviour
    5. {
    6.    public Texture2D icon;
    7.    public string noIconText = "Show Map";
    8.    public GUIContent content;
    9.  
    10.    void OnGUI()
    11.    {
    12.       if (GUI.Button(new Rect (10, 10, 100, 50), content))
    13.       {
    14.          content.image = content.image != null ? null : icon;
    15.  
    16.          if (content.image == null)
    17.             content.text = noIconText;
    18.          else
    19.             content.text = "";
    20.       }
    21.    }
    22. }
    23.  
    It's only slightly more complicated if you want to change the button's size or position. Just store the button's position in a Rect variable and change it when you change the content's text.
     
  4. Gwolf-Studios

    Gwolf-Studios

    Joined:
    Oct 7, 2012
    Posts:
    30
    @EddyEpic

    The image is showing up ON the button, I wanted it to take up the whole screen, like a world map.

    @Timer

    Your script gave me an error
     
  5. ModStoryGames

    ModStoryGames

    Joined:
    Apr 27, 2012
    Posts:
    179
    Ah, my bad.

    Here are two possible options. The first still uses a button. If you click the button, the map will appear, taking up the whole screen (with a space of 10 around the edges). Click this map, and it will turn into a button, again. The second option is the same, but the button is replaced by another DrawTexture call, as I believe you said you wanted to avoid drawing the gray area of the button. (Sorry, again, if I misunderstood. But these should be enough to get you started down the right path, I think. :D)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Map : MonoBehaviour
    5. {
    6.    public Texture2D icon;
    7.    public bool showMap = false;
    8.    public Rect iconPosition;
    9.  
    10.    void OnGUI()
    11.    {
    12.       if (showIcon)
    13.       {
    14.          iconPosition = new Rect(10, 10, Screen.width - 10, Screen.height - 10);
    15.  
    16.          GUI.DrawTexture(iconPosition, icon);
    17.  
    18.          if (Event.current.type == EventType.MouseDown  iconPosition.Contains(Event.current.mousePosition))
    19.          {
    20.             showIcon = false;            
    21.  
    22.             Event.current.Use();
    23.          }
    24.       }
    25.       else
    26.       {
    27.          iconPosition = new Rect(10, 10, 100, 50);
    28.  
    29.          if (GUI.Button(iconPosition, icon))
    30.          {
    31.             showIcon = true;
    32.          }
    33.       }
    34.    }
    35. }
    36.  
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Map : MonoBehaviour
    5. {
    6.    public Texture2D icon;
    7.    public bool showMap = false;
    8.    public Rect iconPosition;
    9.  
    10.    void OnGUI()
    11.    {
    12.       if (showIcon)
    13.       {
    14.          iconPosition = new Rect(10, 10, Screen.width - 10, Screen.height - 10);
    15.  
    16.          GUI.DrawTexture(iconPosition, icon);
    17.  
    18.          if (Event.current.type == EventType.MouseDown  iconPosition.Contains(Event.current.mousePosition))
    19.          {
    20.             showIcon = false;            
    21.  
    22.             Event.current.Use();
    23.          }
    24.       }
    25.       else
    26.       {
    27.          Rect iconPosition = new Rect(10, 10, 100, 50);
    28.  
    29.          GUI.DrawTexture(iconPosition, icon);
    30.          
    31.          if (Event.current.type == EventType.MouseDown  iconPosition.Contains(Event.current.mousePosition))
    32.          {
    33.             showIcon = true;
    34.  
    35.             Event.current.Use();
    36.          }
    37.       }
    38.    }
    39. }
    40.  
     
  6. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Save yourself some time and just pick up NGUI learn that instead.. Unity's current GUI system has far too many draw calls.
     
  7. Gwolf-Studios

    Gwolf-Studios

    Joined:
    Oct 7, 2012
    Posts:
    30
    Actually epic's last post helped a lot :D no more problems