So today, we were trying to horizontally flip a texture to be used within GUI. There is no option to flip a texture via the inspector so we had to do this through our script.
Here’s the little function we used for this, it’s in unityscript but can very easily be converted to C# by just changing how variables are declared. This can also be adjusted to flip things vertically.
function FlipTexture(orig:Texture2D):Texture2D {
var flip:Texture2D = new Texture2D(orig.width,orig.height);
var xN:int = orig.width;
var yN:int = orig.height;
for(var i:int=0; i < xN; i++) {
for(var j:int=0; j < yN; j++) {
flip.SetPixel(xN-i-1, j, orig.GetPixel(i,j));
}
}
flip.Apply();
return flip;
}
Needed some pics of the results 😉
LikeLike
There’s a picture in the header 😀
LikeLike
I guess I didn’t realize it. Thought it was just a nice header picture.
LikeLike
I’ve been coding for a long time in Java and although I still prefer Java overall with blogs like this and some time working with you, I’ve learned to like C just a bit more ;). You have really shown us that girls can really code and that’s why I’m nominating you to the Creative Blogger’s Award! Keep the TCG coming!
Go here for details: https://dreamgardening.wordpress.com/2015/03/26/the-creative-bloggers-award/
LikeLike
I like it, it’s very short and simple, I hope you don’t mind I made a C# version of it and linked to the blog!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TextureFlip.cs
hosted with ❤ by GitHub
LikeLiked by 1 person
Thank you! And of course not 🙂 I actually code mainly in C# but at that time we were using unityscript
LikeLike
Here’s a one liner that I was able to use to do something similar.
using System.Linq;
target,SetPixels32(target.GetPixels32().Reverse().ToArray());
LikeLiked by 1 person
This one is cpu-bound, so if its just displaying, you can just use a material with a shader that flips the texcoords of each vertex to 1-input_tex_coords in the vert shader or if you do it in the frag shader, you could sample using 1-vert_output_coords.
Certainly a lot faster than doing it via script and while this way uses shaders, its a very simple version of them, so it should be rather easy to get working. And you wouldn’t waste cpu time to do it, especially if its to be done in multiple UI elements.
LikeLike
Just did it, simple as adding a material to a UI Image Component, creating a simple Unlit shader through Unity to add all the boilerplate, set the material to use that new shader and in the shader you just do:
o.uv = TRANSFORM_TEX(v.uv, _MainTex); //This is provided
o.uv.x = 1 – o.uv.x; //This is what you add
You can see the effect in the editor without pressing play.
LikeLike
Yup, I hadn’t learned about shaders yet when i posted it 🙂 Shaders can do a lot of cool things! Thanks for posting!
LikeLike
I thought as much but I posted anyway for anyone else stumbling on this.
LikeLiked by 2 people