x


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");
    }
more ▼

asked May 09 '11 at 04:17 PM

SirGive gravatar image

SirGive
1.2k 25 32 50

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered May 09 '11 at 04:47 PM

poncho gravatar image

poncho
932 2 3 14

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?

May 09 '11 at 05:06 PM SirGive

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

May 09 '11 at 05:07 PM SirGive

actually instead 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

May 09 '11 at 05:29 PM poncho

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

May 10 '11 at 07:45 PM SirGive
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3698
x539
x355

asked: May 09 '11 at 04:17 PM

Seen: 1774 times

Last Updated: May 10 '11 at 07:49 PM