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!