• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Hamesh81 · Feb 09, 2014 at 09:42 AM · texturecolorpixel

How can I change all of a textures pixels to one color?

I have a painting app in which colors are painted over the top of a completely transparent, blank texture. I need to create a "clear" functionality where all the pixels in the texture are reset to a specific color (1, 1, 1, 0); which is a completely transparent white. Rather than changing out the texture each time, I would rather simply wipe all the pixels on the one texture. Here is what I've got so far but it's not working:

 #pragma strict
 
 var resetColor : Color = Color(1, 1, 1, 0);
 var tex : Texture2D;
 var resetColorArray = new Array ();
 
 function Start () {
 
     tex = renderer.material.GetTexture ("_MainTex");
     resetColorArray =  tex.GetPixels();
 
 }
 
 function Update () {
     
     if (Input.GetKeyDown(KeyCode.Return)) {
         for(var i = 0; i < resetColorArray.length; ++i)
         {
             resetColorArray[i] = resetColor;
         }
          
         tex.SetPixels( resetColorArray );
          
         tex.Apply();
     }    
 }

How can I do this?

Comment
ahungrybear
Xonatron

People who like this

2 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image highpockets · Feb 09, 2014 at 10:49 AM 0
Share

In the start function put:

 var startingX = 0; //beginning left most point of rect that will show your texture
 var startingY = 0; //beginning top most point of rect that will show your texture
 var pixelWidth = tex.width; //texture width
 var pixelHeight = tex.height; //texture height
 
 resetColorArray = tex.GetPixels(startingX, startingY, pixelWidth, pixelHeight);

I don't know if this works

avatar image highpockets · Feb 09, 2014 at 10:56 AM 1
Share

maybe try this too

renderer.material.mainTexture = tex;

avatar image Hamesh81 · Feb 09, 2014 at 11:17 AM 0
Share

I shouldn't need to set the x,y etc when using SetPixels because as oppose to SetPixel (no "s") you only need to specify a color. I'm getting an error on line 22 about the array:

 No appropriate version of 'UnityEngine.Texture2D.SetPixels' for the argument list '(Array)' was found.

But I'm not sure what's wrong with the array.

Tried your other suggestion too but it didn't solve the problem :(

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Eric5h5 · Feb 09, 2014 at 11:25 AM

The resetTextureArray needs to be either a Color array, or preferably a Color32 array, which you can use with SetPixels32. Never use the Array class (for this or anything else; it's useless). Also resetTextureArray should ideally be filled once in Start, rather than every time you hit return.

Comment
Hamesh81
greenpower
ahungrybear
codewing

People who like this

4 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hamesh81 · Feb 09, 2014 at 11:49 AM 0
Share

Ah awesome thanks for that, that solved the problem! Cheers

avatar image greenpower · Mar 22, 2016 at 02:33 PM 0
Share

This also worked for me. Thanks!

By the way, in C# it looks like this:

     // New texture
     texture_ = new Texture2D(x, y);

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

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

Cheers

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3D Text Object does not change to the color specified on Mouse Over 0 Answers

new Color() not working properly? 1 Answer

Get Average Colour From Texture? 1 Answer

How do I change the font and color of GUI text? 0 Answers

Get pixel from Rotated Texture2D 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges