Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change color of half texture

Discussion in 'Scripting' started by Federica_96, Mar 19, 2021.

  1. Federica_96

    Federica_96

    Joined:
    Feb 19, 2021
    Posts:
    30
    Hi,
    I am using this lines of code to change the color of a texture from red to pink:

    Code (CSharp):
    1. texture = Texture2D.redTexture;
    2. material.mainTexture = texture;
    3.  
    4. // Reset all pixels color to pink
    5.             Color32 resetColor = new Color32(255, 0, 255, 0);
    6.             Color32[] resetColorArray = texture.GetPixels32();
    7.  
    8.             for (int i = 0; i < resetColorArray.Length; i++)
    9.             {
    10.                 resetColorArray[i] = resetColor;
    11.             }
    12.  
    13.             texture.SetPixels32(resetColorArray);
    14.             texture.Apply();
    I would like to make only half of my texture pink, leaving the other half red. How can I do?
    thank you
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hi,

    How about two textures, and then just swap between them?
     
    Federica_96 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    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:

    Code (csharp):
    1. int index = x + y * texture.width;
    And you can then use
    index
    to modify a particular
    resetColorArray[]
    entry however you like.
     
  4. Federica_96

    Federica_96

    Joined:
    Feb 19, 2021
    Posts:
    30
    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...
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    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.
     
  6. Federica_96

    Federica_96

    Joined:
    Feb 19, 2021
    Posts:
    30
    Ok, Thank you!