- Home /
Stretching a Texture2D to fill entire button
How do you stretch a texture2d to fill an entire button (with button being larger than the texture2d)?
How do you do this if the texture2d is square, and the button is rectangular - how do you get it to stretch the entire long length of the rectangle?
Sorry, are you trying to make the texture the same size as the button, or the button larger than the texture? You're kind of contradicting yourself here.
the button is larger than the actual texture, but i want to make ti the same size as the button.. thus stretching the texture to fill the button
Ok then. Do it with a GUIStyle, where the background image is the texture.
hmm i tried applying a guiskin with the stretch options set to both horizontal and vert, but the texture does not stretch to fill the whole button
guistyle (as i understand) is just guiskin, but specified in code, ins$$anonymous$$d of as a component?
Answer by Bunny83 · Oct 31, 2011 at 03:39 AM
To make a GUIStyle to stretch it's background image you:
have to set fixedHeight and / or fixedWidth to 0
have to set stretchHeight and / orstretchWidth to true
may want to setup a border (that isn't streched) or set it to (0,0,0,0)
may want to set an overflow value to make the image larger than the element is.
btw. A GUISkin is just a collection of GUIStyles (and some general settings). Every GUI element is drawn with a GUIStyle. A GUISkin is just an easy way to exchange all styles at once.
I had those parameters before, but it looks like disproportionate stretching does not work. Appears stretching will only try for proportional. So, if your texture is square, and you try putting it in a large, long rectangular button, it will stretch to the smallest side of the rectangle only.
I have the same problem now.. how did you solve it ?
never $$anonymous$$d I got it working.. I will post the answer here incase someone needs it
Answer by smartkiran143 · Dec 11, 2012 at 06:09 AM
when you import images unity resizes it to the nearest power of two..
so click on your image, go to your inspector and then change 'Texture Type' to 'GUI' .
and now give that texture to your button like this and it works..
GUI.Button(new Rect(Screen.width/2-100, Screen.height/2, 200, 50), texture, btnStyle);
btnStyle is a custom GUI style.. just check 'Stretch Width' and 'Stretch Height'
i still wasn't able to stretch it just by changing the texture type to gui :|
smartkiran's answer is what fixed a similar issue with GUI textures being stretched for me - make sure texture type is GUI in import settings (and apply!) and then change your GUI style to get the scaling required. Thanks!
Your answer
Follow this Question
Related Questions
Empty spaces in buttons are detected by mouse 2 Answers
How do I Define the Size of a Texture Inside GUI.Button? 0 Answers
Making texture cover whole button 1 Answer
Button texture 2 Answers
Assiging an Image to a Button 1 Answer