Clipping a GUITexture

Hi All.

I have a HUD that is comprised of 3 GUITexture objects layered one on top of the other, creating a certain effect.
This works perfectly, however, the middle on can be moved around, as it creates a gradient effect in this health bar.

So, when I move the middle one too far up or down, it will exit the bounds of the original texture, which is what i’d like to clip out.

Does anyone have an Idea on how to achieve this?

2 Answers

2

You may want to look at GUI.Window which can create a specific area for any GUI elements to be drawn in. You'll probably want to adjust its GUIStyle so it doesn't have a 'window' backdrop

An alternative could be to use a Scrollview that's small enough that there are no scrollbars.

Another alternative would be to use the offset of your texture without changing its bounded Rect

Edit: I don't know how to clip a GUITexture Object; this answer requires that you do a little bit of scripting for OnGUI(). I'm sorry I thought I changed the third option, it should link to DrawTextureWithTexCoords and is probably the easiest way to accomplish what you want, I think.

Using that a texture can easily be animated via script:

var myTexture: Texture2D;
var myRect : Rect = Rect(0,0,1,1);

function OnGUI(){   

    myRect.y += Time.deltaTime; 
    GUI.DrawTextureWithTexCoords(Rect(10,10,100,100),myTexture,myRect);

}

I did a little more looking into the script reference, and if you're still looking to clip things, I also strongly recommend looking into GUI.BeginGroup. This is all stuff you have to script, but as far as I can tell, thats the only way to get the control it seems you need.

I'm actually not using a full gui. I'm just using the GUITexture to draw a 2D "HUD" it doesn't need any interactivity, and there aren't any other classes, only these 3 GUITexture. I would imagine that if i put a scroll view, it will put the scroll bars on screen one I exceed the bounds. How can I do the third recommendation?

This answer does require switching to OnGUI() calls to draw your textures.

Yes, thank you. DrawTextureWithTexCoords works great to clip the actual texture, while BeginGroup works well if you have a number of controls. I'd probably end up with a combination of both :)

Well, I’ve been able to solve this with GUI.DrawTextureWithTexCoords(), by just calculating the mask and UVs on the spot. since this is a single call, it is a good solution. However a 2d mask interface would have been great :slight_smile:

Gah I just wrote a detailed answer regarding that, too! (thats what I meant by offsetting the texture but failed to link). UnityAnswers has been taking forever to update comments and answers :(

Sorry... :) Thank you for your help though. It did point me in the right direction!

Glad you figured it out. It is crazy that it takes up to a few hours for edits to go through... I hope this is something resolved soon.