- Home /
Show image when a GUI button is pressed [problem]
Hi.. I've created this script for show/hide an image when a button is pressed, but in play mode I don't see the image, and I cannot set its values.. Any ideas?
#pragma strict
import System.Collections.Generic;
var buttonstyle : GUISkin ;
var texture : Texture;
var mostra : boolean;
var on : boolean;
var texWidth : float; // texture width
var texHeight : float; // texture height
var texpositionx : float;
var texpositiony : float;
function OnGUI (){
GUI.skin = buttonstyle;
if(GUI.Button(new Rect(808,9,35,34),"")){
GUI.DrawTexture(new Rect(Screen.width / texWidth, Screen.height / texHeight, texpositionx, texpositiony),texture);
on = !on;
if (!on){
mostra = true;
}
if (on){
mostra = true;
}
}
}
Add some logging so you know if (and when) the code is getting into the block with the DrawTexture call.
It looks to me like you're setting the Rect up incorrectly. The 4 parameters to the consructor should be in the order ( xpos, ypos, width, height)
It's been a while since I used OnGUI stuff, so I'm sorry if this is a red herring, but my feeling is that GUI.Button returns true when the user clicks the button (ie presses and releases), not when the button is held down. That would mean that even if your texture were being displayed when the button was clicked, it would only be for a single frame. In the following frame, when it gets to this OnGUI, the button is not clicked, and so the texture is not drawn.
I've no idea what it's for but just in case it's significant, you have code at the bottom that sets 'mostra' to true for both possible values of 'on'. This is either redundant, or an error.
You have a point (#3)
True GUI.Buttons don't have states with delay so it gets true just once. Same gui more or less is used in custom inspectors and editor scripts. And they're just there for practical reasons. Nothing fancy.
Answer by npatch · Oct 27, 2015 at 01:14 PM
Is there a particular reason why you're using instant GUI and not the Canvas-based uGUI?
uGUI is way handier and what you're trying to do wouldn't even need a script. It's ready. UI Button Tutorial
I made all my button's game with scripts, unfortunately I don't understand the video tutorial in english.. :/ I just wanted to know why in my script the image didn't appear..
Can't see values but I think you're using the wrong order in your new Rect() inside DrawTexture. Looks like you entered the position last and the width and height last. Or at least you entered what looks to be a position as width and height.
I discovered that the GUI.DrawTexture
doesn't work inside if(GUI.Button(new Rect(808,9,35,34),"")){}
, but I need to show the image If the button is pressed, for this reason I have to put DrawTexture inside that.. :/
Answer by starikcetin · Oct 27, 2015 at 05:29 PM
I didn't fully understand you script but, i think there is a mistake here:
if (!on){
mostra = true;
}
if (on){
mostra = true;
}
You assing true for both conditions.
Your answer
![](https://koobas.hobune.stream/wayback/20220612042922im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI OnClick() issue... 0 Answers
Change GUI.Button Texture on runtime 1 Answer
Moving GUIbutton over an object that does not follow the player? 2 Answers
How to change GUI.Button Texture? 2 Answers