Show part of a Texture, from the bottom

I am using the following code inside my OnGUI function so as to show part of my texture:

percentIndicatorRect.height = getPercentage() * indicator.height;
GUI.BeginGroup(percentIndicatorRect);
GUI.DrawTexture(indicatorRect, indicator);
GUI.EndGroup();

The thing is that it is showing a percentage of the texture only from the top through the bottom of the image.

For example, if getPercentage returns 0.35, 35% of the image will be shown from its top through the bottom.

I’ve understood pretty well how the BeginGroup function works and I cannot think of a way that I can make it show a percentage of the image from the bottom.

I’ve tried several things, like changing the value of the percentIndicatorRect.y (but then the whole texture moves along with it, so there is no use).

I have an idea of altering the percentIndicatorRect.y to += (1-percentage)*percentIndicatorRect.height (so as to move the “container” down and now its x, y values to be the upper left edge of the part of the image that I want to show) and then to abstract this value from indicatorRect.y (and thus it will take a negative value) (so as to move the texture up and place it to the right location)

But I have not implemented this because it doesn’t sound correct.

I would think the best way to do this is through a shader, not through the OnGUI method.

While iwaldrop’s shader suggestion is a good one, your “doesn’t sound correct” idea is what Unity suggests in the Unity Manual at Unity - Manual: IMGUI Layout Modes .

Basically, you need to create the group you want the texture to show. Then any parts of the image that do not fit inside the group are clipped.

	void OnGUI () {
		GUI.BeginGroup (new Rect (0,0,256,32));{
			//only 256 pixels are drawn, rest are clipped.
			GUI.Box (new Rect (0,0,300,32), texture);
		}GUI.EndGroup ();
	}