Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by hollym16 · Oct 15, 2014 at 11:17 AM · guitexturebuttonguitextureongui

GUI.DrawTexture on GUI.Button press

I've got a GUI menu with a GUI button on it, when the button is pressed I want a GUI texture to come up which will display information. Ive got the following script but when I click the button, it doesn't bring the texture up (I've assigned it in the Inspector):

 using UnityEngine;
 using System.Collections;
 
 public class GUIDisplay : MonoBehaviour 
 {
     public Texture2D background;
     public Texture2D helpPage;
 
     void OnGUI()
     {
         GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),background);
         GUI.backgroundColor = Color.clear;
         if(GUI.Button(new Rect(Screen.width * (4.5f/6.55f),Screen.height * (5.25f/6.3f),Screen.width * (0.8f/6.55f), Screen.height * (0.85f/6.3f)),""))
             GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),helpPage);
                 }
             }

Any ideas what I'm doing wrong?

Alternatively, I could activate and deactivate the texture, I tried:

 helpPage.SetActive(true);

and

 helpPage.enabled = true;

But neither of these worked. (I'm using C# by the way)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by troien · Oct 15, 2014 at 01:16 PM

The problem is that the OnGUI method is called a lot. (depending on the event) OnGUI gets called once per frame for the Repaint event for instance. And whenever you mousedown, mouseup, mousemove etc. OnGUI will be called additional times for each event.

Your problem is that the content of the if(GUI.Button()) call only gets called on the mouseUP event. Not the repaint event. so any GUI calls inside are basically ignored.

So the thing you could do is something like the following

 using UnityEngine;
 using System.Collections;
  
 public class GUIDisplay : MonoBehaviour 
 {
     public Texture2D background;
     public Texture2D helpPage;
 
     private bool clicked = false;
  
     void OnGUI()
     {
         GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),background);
         GUI.backgroundColor = Color.clear;
         if(GUI.Button(new Rect(Screen.width * (4.5f/6.55f),Screen.height * (5.25f/6.3f),Screen.width * (0.8f/6.55f), Screen.height * (0.85f/6.3f)),""))
             clicked = true;
 
         if (clicked)
             GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),helpPage);
     }
 }

For more information about how OnGUI works you should check the docs.

Comment
Add comment · Show 2 · 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 hollym16 · Oct 15, 2014 at 01:41 PM 0
Share

Thanks, it works brilliantly. How would I go about 'undrawing' the texture? There's a button on the texture I activated that, when clicked, I want to deactivate. If that makes any sense?

avatar image troien · Oct 15, 2014 at 02:03 PM 0
Share

well, setting "clicked" to false again will stop everything inside "if (clicked)" from geting called. Therefore I think that the following example should point you in the right direction.

 if (clicked)
         {
             GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), helpPage);
             if (GUI.Button(new Rect(0, 0, 100,50),""))
                 clicked = false;
         }

ps. you should really take note to the "gets called once per frame" part of my answer. with this I also meant that every frame the entire GUI will be clearded and re-drawn. and therefore there doesn't exist something like 'undrawing' something in Unity OnGUI.

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

29 People are following this question.

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

Related Questions

Change GUI.Button Texture on runtime 1 Answer

Making texture cover whole button 1 Answer

Making GUI Buttons on a GUI Texture 1 Answer

GUI.Box not showing Texture on device 1 Answer

OnGUI button created by a foreach loop 4 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