- Home /
Mixing two textures
I'm trying to mix two textures together. One is an empty health bar texture and the other the full counterpart. When the health bar is, say, half empty, there should be the right half of the empty bar and the left half of the full part on the screen.
I tried drawing both as seperate textures, but the clipping modes don't include "align right" and "align left" (or at least non that I know of) so I can't do that. Also tried declaring a new texture in code and filling it with pixels from the two textures, but it EATS up processor power, reducing my framerate from 500fps to 30fps. What should I do? Tnx in advance.
Answer by DavidDebnar · Jul 18, 2011 at 10:21 AM
When it's an healthbar, than don't try to mix them. Instead of that just multiply. Draw the 2 textures, and than multiply the health texture/that texture that will be filling it up, by health. And if you don't wan't to deform the texture, than you have to make 3 textures. Background, the texture that will be filling it up and a texture that will hide the unfilled parts. Like the opposite. You won't make the filling texture bigger, but you will make the hiding texture, that will be colored the same as the background smaller.
David
I can see what you're telling me to do, but I don't really know how to do it in code! Could you please provide a bit of source code for doing this? Tnx. Oh, and BTW, I don't wanna deform either of the textures.
So u wan't the third method. Like imagine that this O is the health that will be going smaller, and this X is both background and the texture that will be going on the health. So what you wan't to do, is to stretch the texture, or whatever, so it will be bigger as the health is smaller like this. So you aren't defor$$anonymous$$g the health, just hiding a part of it.
OOOOO OOOOX OOOXX OOXXX OXXXX XXXXX
And well the script.. It's 1 at morning and I needed a really big strength to get this outta my head :D. Ummm. What you wanna do is to make a gui texture trough code, and it's x and y size/the one that you wanna change, will be variables. Than let's say that the maximum health is 100. $$anonymous$$imum 0. So you have a 3 gui - 1. the health image, 2. the background image, 3. the background image second time, we will be resizing this one. So the code will look like this.
var lengthX : float = 0; var hp : float = 100;
function TakeDamage() { hp -= damage; }
function OnGUI() { //draw the health image, make it's depth smaller //draw the background, make it's depth bigger that the hp image, but smaller than the hiding image we are gonna resize. lengthX = 100 - hp; //lengthX set to maximum health $$anonymous$$us the current one GUI.DrawTexture(Rect(100-lengthX,0,lengthX,10), texture, Scale$$anonymous$$ode.ScaleToFit, true, 0.0); }
I don't know if this is going to work, but it should. You might wan't to max out the last float in the DrawTexutre, because it's the aspect ratio and you might wan't to change that. Google the depth, if you dont know the know how.
$$anonymous$$
It was 1 A$$anonymous$$ 18 $$anonymous$$utes ago, so you should be from somewhere around eastern Europe, if I'm not totally mistaken! :D Tnx for the code!
Answer by Arshia001 · Jul 19, 2011 at 04:35 PM
I also found another way to do it. Just add a new style to your GUI skin, such as "full health" and set the normal state's texture to the full health texture. do the same with the empty texture and another style. In the draw code, figure out the width of the full portion (let's name it f). Set the "full health" style's Border.left property to f (make sure others are zero) and draw a box which is f pixels wide using the full health style. Do the same for the empty health texture, only set Border.right instead. Works perfectly.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150014im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
mix diffuse with alpha channel 1 Answer
Change GUI.Button Texture on runtime 1 Answer
Change crosshair text when hitting a trigger 0 Answers
How to achieve realistic models 0 Answers