• 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 SirGive · May 09, 2011 at 04:17 PM · guitexture2d

Swap GUI Texture with button press in C#

alt text

So essentially, what I want to do is highlight the button I press. I have got a way to do it but the problem is that my frame rate goes from 300 to 10 whenever I press a button. Here is what I did, but is there a better way to do it? I couldn't figure out how to just swap textures so this is the long way:

    if (playerScript.GPress == true)
    {
        GUI.DrawTexture(GPressImage);
    }
    if (playerScript.HPress == true)
    {
        GUI.DrawTexture(HPressImage);
    }
    if (playerScript.VPress == true)
    {
        GUI.DrawTexture(VPressImage);
    }
    if (playerScript.BPress == true)
    {
        GUI.DrawTexture(BPressImage);
    }
    if (playerScript.NPress == true)
    {
        GUI.DrawTexture(nNPressImage);
    }
    if (playerScript.NoPress == true)
    {
        GUI.DrawTexture(NoPressImage);
    }

This does work. Its just not efficient in the least.

SOLVED: Essentially this is the change I made:

if (KeyCode.G) { Resources.Load("GPress"); }

 else if (KeyCode.H)
 {
     Resources.Load("HPress");
 }

 else if (KeyCode.V)
 {
     Resources.Load("VPress");
 }

 else if (KeyCode.B)
 {
     Resources.Load("BPress");
 }

 else if (KeyCode.N)
 {
     Resources.Load("NPress");
 }

 else if (KeyCode.G || KeyCode.H || KeyCode.V || KeyCode.B
     || KeyCode.N)
 {
     Resources.Load("NoPress");
 }

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 poncho · May 09, 2011 at 04:47 PM

you can have variables with the different images of your buttons, pressed and unpressed, this will give you like 7 images 5 of 'g-h-v-b-n' unpressed, and 2 of 'g-h' pressed you only have the GUI.DrawTexture() once, but having a texture that will change its image value, once the button is pressed

GUITexture  myDisplayedImage;
void Update()
{
    if(playerScript.GPress)
        myDisplayedImage = GpressedImage;
    //this you can change the material or the texture, your choice
}
void OnGUI()
{
    GUI.DrawTexture(myDisplayedImage);
}

this is the main idea of what i would do, also instead of drawing on the OnGUI, i would change that for a plane object, with the plane you will need change the material, as i know is more efficient than OnGUI, but first make the try

hope this helps you

Comment
Add comment · Show 4 · 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 SirGive · May 09, 2011 at 05:06 PM 0
Share

Ah, thats close to what I was doing. I was wondering if I could search through the project to find a texture and assign it to one variable so that I don't have to allocate 7 images. And even then, would that fix the 300 to 10 frames issue?

avatar image SirGive · May 09, 2011 at 05:07 PM 0
Share

Ahh I missed that last part. Thanks, I'll give it a shot.

avatar image poncho · May 09, 2011 at 05:29 PM 0
Share

actually ins$$anonymous$$d of the assigment it could be a Resources.Load("myimage") as Texture or something, i think the frame rate problem should not be there anymore, if dont work with that, try the planes thing

avatar image SirGive · May 10, 2011 at 07:45 PM 0
Share

That is awesome. This totally fixed the problem. Thank you.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is there a way to import png images with TextureType = GUI automatically? 1 Answer

'Mission Complete/Game Over' screen once objective is complete. 1 Answer

GUI Scroll View 1 Answer

I need help with my pause menu 1 Answer

Sliding Gui auto button generator... help 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges