Change color of half texture

Hi,
I am using this lines of code to change the color of a texture from red to pink:

texture = Texture2D.redTexture; 
material.mainTexture = texture;

// Reset all pixels color to pink
            Color32 resetColor = new Color32(255, 0, 255, 0);
            Color32[] resetColorArray = texture.GetPixels32();

            for (int i = 0; i < resetColorArray.Length; i++)
            {
                resetColorArray[i] = resetColor;
            }

            texture.SetPixels32(resetColorArray);
            texture.Apply();

I would like to make only half of my texture pink, leaving the other half red. How can I do?
thank you

Hi,

How about two textures, and then just swap between them?

1 Like

Line 6 above is getting every single pixel into what is commonly called a “raster” array of pixels.

Lines 8 to 11 just blindly set each one to the resetColor

You can always set any pixel to anything you like. It’s just not a super-performant way to go, but it does have some application.

If x is the pixels across and y is the pixels up, then the index into the above array would be:

int index = x + y * texture.width;

And you can then use index to modify a particular resetColorArray[ ] entry however you like.

Thank you for your answer! Can you please tell me where and what in my code I have to say if I want to change the color of only one pixel? I’m quite new to programming and having some difficulties…

Line 10 above assigns the color to the pixel.

Before that point you would need to decide the color, and assign the decided color.

First off, welcome, but realize that what you are doing has virtually ZERO to do with the massive capability and function of Unity3D and what it brings to the table.

Unity is far more powerful than that, as a fully-3D engine, and as such it isn’t really set up for pixel manipulation.

Setting and clearing individual pixels in realtime today is a whole special subset of game development, embodied by such systems as Pico-8 and other suchlike low-level pixel-oriented engines.

Unity is NOT that engine, but of course Unity lets you do just about anything. Just know that NOTHING you are doing with pixels above has anything directly to do with Unity. You’re basically just changing a color cell (pixel), which is simply copying a piece of data (the color) into that pixel. It’s merely incidental that Unity is presenting that colored texture to you (I presume) somewhere down the line.

Ok, Thank you!