Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PaxStyle · Oct 27, 2015 at 12:08 PM · guiguibutton

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;
 }
 }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Oct 27, 2015 at 05:39 PM 0
Share
  1. Add some logging so you know if (and when) the code is getting into the block with the DrawTexture call.

  2. 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)

  3. 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.

  4. 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.

avatar image npatch Bonfire-Boy · Oct 27, 2015 at 05:43 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PaxStyle · Oct 27, 2015 at 05:14 PM 0
Share

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..

avatar image npatch PaxStyle · Oct 27, 2015 at 05:27 PM 0
Share

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.

avatar image PaxStyle npatch · Oct 27, 2015 at 06:48 PM 0
Share

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.. :/

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI OnClick() issue... 0 Answers

Change GUI.Button Texture on runtime 1 Answer

A shop UI with Scroll Rect runs very smoothly in Unity Playmode(140fps) and hangs a lot in android(8fps). Please see the description 0 Answers

Moving GUIbutton over an object that does not follow the player? 2 Answers

How to change GUI.Button Texture? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges