Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ina · Oct 30, 2011 at 07:58 AM · guibuttontexture2d

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?

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Oct 30, 2011 at 08:06 AM 0
Share

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.

avatar image ina · Oct 30, 2011 at 10:49 PM 0
Share

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

avatar image syclamoth · Oct 30, 2011 at 11:33 PM 0
Share

Ok then. Do it with a GUIStyle, where the background image is the texture.

avatar image ina · Oct 31, 2011 at 02:35 AM 0
Share

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

avatar image ina · Oct 31, 2011 at 02:36 AM 0
Share

guistyle (as i understand) is just guiskin, but specified in code, ins$$anonymous$$d of as a component?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ina · Oct 31, 2011 at 10:30 AM 0
Share

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.

avatar image smartkiran143 · Dec 11, 2012 at 03:31 AM 0
Share

I have the same problem now.. how did you solve it ?

avatar image smartkiran143 · Dec 11, 2012 at 05:51 AM 0
Share

never $$anonymous$$d I got it working.. I will post the answer here incase someone needs it

avatar image
0

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'

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NavySeal · Mar 30, 2013 at 10:30 PM 0
Share

i still wasn't able to stretch it just by changing the texture type to gui :|

avatar image Simeon2020 · May 09, 2013 at 02:16 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges