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 PuerNoctis · Oct 06, 2013 at 03:16 PM · uiguitexturestretchstretching

Stretching Texture2D

Hi all!

Although I am versed in programming in general, I am fairly new to Unity, so excuse this question if its solution is quite simple in the end :)

I have the following issue:

For my custom GUI skin I have a base texture for say, a button. This base texture is rectangular in size, so if I want to draw a longer button I simply assign the 'border' attributes to the GUIStyle and have Unity do the rest for me.

But that did not work out well enough, because some of these textures require the center of the button (so everything aside the borders) to be tiled instead of stretched as Unity does by default, at least when using the GUIStyle background property. I worked around this 'problem' by writing a texture generator which can 'bake' textures once for a specific size at script startup, behaving similar to GUIStyles, but with the benefit of defining TileModes (stretch or tile) for the horizontal/vertical borders as well as the center of the texture along the given width/height of the button.

This generator makes heavy use of GetPixels()/SetPixels() - and with that I just swapped the problem. I now can't get pieces of my texture stretched as I may want for the borders:

Basically, when the user wants the top border to be stretched for a button with a 150px length, the generator takes the 'top border' piece with e.g. 10px length from the base texture and has to stretch the returned Color[] from GetPixels() to the 150px of the target texture. But I don't know how to achieve this... if I try to just call SetPixels() on my target texture it complains about the source array being too small (150px needed in array, but only 10px found), so it seems that SetPixels() doesn't stretch itself.

My next attempt was to create a temporary Texture2D and afterwards change its size (and I actually didn't wanted to do this in the first place, as it seems way too much overhead), but then the state of the contents of the texture is undefined. So Texture2D.Resize() doesn't stretch a texture either.

So long story short: How can I stretch an existing texture or Color[] to another size in code, leaving its contents intact (stretched)?

I really appreciate any help in this - hopefully there is just a method somewhere that manages to do exactly that :)

Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by PuerNoctis · Oct 06, 2013 at 04:57 PM

I somewhat found a solution, but I still want to leave the question open for answering. I found this code that does exactly what I want (and judging form the description, I am not the only one having the resize-problem):

http://wiki.unity3d.com/index.php/TextureScale

For a very very small texture, this piece of code takes about 0,011 seconds for resizing on my machine. Well, these operations are just expensive, aren't they :/

Just FYI: The C# code does cause problems on multi core machines with the version there at time of writing. If you encounter mutex ownership problems, either disable the multithreaded path of the script, or adapt the mutex-lock-release logic accordingly, so that only the owning thread releases it.

I wonder why this functionality is not provided by Unity (yet)...

Comment
Add comment · 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

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

16 People are following this question.

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

Related Questions

scaling behaviour of the standard box texture? 0 Answers

uGUI Image component not auto-generating mesh around sprite? 0 Answers

How to stretch only the middle of a GUI texture? 1 Answer

All UI's appear as black 0 Answers

Unable to stretch texture over entire 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