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 :slight_smile:

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 :slight_smile:


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):

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 :confused:

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)…