- Home /
Is there anyway to only show a part of a GUI image during the game? (C#)
I used to make games using XNA, and one of the cool things about it was that for healthbars, you didn't need to create tonnes of images for each value of health.
Instead, you just have one image, and you can make it so that you can only show part of an image, and make it get smaller or bigger in game by using a Rect in the draw script, you do this by on the draw function, you can put in a rectangle, and it will only show what is in the rectangle
For example, a health bar, you can make it as a big or small as you like, depending on the health, the rect that the game has to show is the same height as the health bar, but the width is the changes depending on the health, the lower your health, the smaller the bar.
Is there anyway to do this with Unity?
I want to use a GUITexture for it, because the healthbar I've made had a gradient going from red to green, and the end of it is slanted, so a box wouldn't be the best thing to use, I don't want to scale it either, because I think that would ruin it.
UPDATE: Here's my code:
health = (masterScript.health / masterScript.maxHealth);
GUI.DrawTextureWithTexCoords(new Rect(gameObject.transform.position.x,
gameObject.transform.position.y, 1,1)
, texture, new Rect(0,0, 1, health), false);
I'm getting a "Null Reference Exception" error for the "GUI.DrawTextureWithTexCoords" line, what am I doing wrong?
Untested, but I think Graphics.DrawTexture() might do the job.
Answer by gfvfubb · Feb 01, 2013 at 12:49 AM
http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTextureWithTexCoords.html
If you google this method you'll see a lot of people complaining. The 'texture coordinates' are normalized float values that range around ~ 0 - 1.0f and they represent percents of the sprite image horizontally and vertically I think. Google for more. It does work like you want though.
So it does the rect as a percentage? That's perfect! I wanted to do percentage, and thinking it would be hard to do, it seems that won't be the case!
One more question, seeing as how without any code, it shows up in full at all times, if I added this code into a script, and attached it to my GUI, would it autmatically no longer show that way and just draw with the script? Or would I have to do something to disable the way it draws now?
I'm pretty sure texcoord is percentage float for horizontal and vertical but I'm not sure. I've only seen references to this in forums in passing and I ended up using another method so I'm not totally sure but lots of people said it works like you want.
You'll need to disable the way it draws now. This method is it's own drawing function that is intended to be run independently and without any support. Simply attach some sort of update code to texcoords somewhere and it should change to suit it.
Sorry, I'm a newbie at Unity... How do I do that? Do I disable the GUITexture component, and just use the script?
Yes, GUI.DrawTextureWithTexCoords actually draws the texture. You don't need the GUITexture component.
Okay. What if I wanted pixel inset on my GUI? How would I do that?