- Home /
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.
You might want to take a look at Graphics.DrawTexture. It allows you to specify a source rect.
http://docs.unity3d.com/Documentation/ScriptReference/Graphics.DrawTexture.html
Thanks for this comment. I tried to use DrawTexture with these parameters but I wasn't able to. I got an error that BCE0023: No appropriate version of 'UnityEngine.GUI.DrawTexture' for the argument list '(UnityEngine.Rect, UnityEngine.Texture, UnityEngine.Rect, int, int, int, int, null)' was found.
while clearly this is the needed parameters list.
But, then I found what SWGraham said in this thread: http://forum.unity3d.com/threads/9771-DrawTexture-to-draw-part-of-a-texture (3rd from the bottom post)
Answer by iwaldrop · Jul 05, 2013 at 09:44 PM
I would think the best way to do this is through a shader, not through the OnGUI method.
Are you 100% sure that this cannot be done through GUI Grouping? I've managed to do the opposite from what I want to do :P (not that I've a particular problem with shaders, but I'm looking for a lightweight solution, apart from the fact that I haven't worked with shaders yet)
I didn't say it couldn't be done using Unity GUI, I said it'd be better done using a shader. And, computationally speaking, shaders are much more performant than any script you can write in Unity, so I don't know what you mean by 'lightweight solution'.
Ok! Thanks a lot! I will try to implement it with shaders and I will post my final solution to my specific problem here :)
Answer by Jamora · Jul 06, 2013 at 09:58 AM
While iwaldrop's shader suggestion is a good one, your "doesn't sound correct" idea is what Unity suggests in the Unity Manual at http://docs.unity3d.com/Documentation/Components/gui-Layout.html .
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 ();
}
Thanks for the answer and yes, I got what Grouping allows me to do. But, as in my occasion, how would you rewrite the code that you posted so as to clip the image from the right and not from the left?
Use negative coordinates for x inside the group. The coordinates inside groups are calculated relative to the coordinates of the Rect of the group. so GUI.Box (new Rect (-30,0,300,32), texture);
would clip texture
by 30 pixels from the left and 14 from the right.
Your answer

Follow this Question
Related Questions
How to make a Texture change in GUI.Window 2 Answers
How to assign and find Textures by tag 1 Answer
Assigning UV Map to model at runtime 0 Answers