Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Aladine · Jan 22, 2014 at 11:05 PM · texturesetpixel

Why does setPixel persist after exit ??

Hello,

I wrote this code to change a part of texture with another texture

         // the texture that i will paint with 
         public Texture2D targetTexture ;
         //temporary texture
         public Texture2D tmpTexture, tmpMainTexture;
 
         void Start ()
         {
                 //setting temp texture width and height 
                 tmpMainTexture = (Texture2D)renderer.material.mainTexture;
                 tmpTexture = (Texture2D)renderer.material.mainTexture;
                 
                 for (int y =30; y<tmpTexture.height; y++) {
                         for (int x = 30; x<tmpTexture.width; x++) {
                                 //filling the temporary texture with the target texture
                                 tmpTexture.SetPixel (x, y, targetTexture.GetPixel (x, y));
                         }
                 }
                 //Apply 
                 tmpTexture.Apply ();
                 //change the object main texture 
                 renderer.material.mainTexture = tmpTexture;
             
                 
         }

what i don't understand is that why the texture itself (in the project hierarchy)also get changed ? i checked out the PNG file and it is intact, also when i restart the project or change the texture type (from Advanced to Texture for example), the texture get back to it's original state, any help please ?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Jan 22, 2014 at 11:18 PM

You should operate on an instance of the texture, rather than the actual texture in the project.

Comment
Add comment · Show 5 · 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 Aladine · Jan 22, 2014 at 11:59 PM 0
Share

so ins$$anonymous$$d of renderer.material.mainTexture i use another texture ? if yes, how can i apply it to the object without using the main texture ?

avatar image Aladine · Jan 23, 2014 at 12:21 AM 0
Share

this is what instance mean right ? cause the problem is not solved yet,

 // the texture that i will paint with 
             public Texture2D targetTexture ;
             //temporary texture
             public Texture2D tmpTexture;
             public Texture theTexture;
     
             void Start ()
             {
                     //setting temp texture width and height 
                     theTexture = renderer.material.mainTexture;
                     tmpTexture = (Texture2D)theTexture;
                     for (int y =30; y<tmpTexture.height; y++) {
                             for (int x = 30; x<tmpTexture.width; x++) {
                                     //filling the temporary texture with the target texture
                                     tmpTexture.SetPixel (x, y, targetTexture.GetPixel (x, y));
                             }
                     }
                     //Apply 
                     tmpTexture.Apply ();
                     //change the object main texture 
                     theTexture = tmpTexture;
             
                     
             }
 
avatar image Aladine · Jan 23, 2014 at 12:39 AM 0
Share

Okay i fixed it, but is this a good approach ? knowing that i will need to do this at runtime with every mouse click (looking for a painting effect)

 void Start ()
         {
                 //setting temp texture width and height 
                 tmpTexture = new Texture2D (renderer.material.mainTexture.width, renderer.material.mainTexture.height);
                 //fill the new texture with the original one
                 for (int y =0; y<tmpTexture.height; y++) {
                         for (int x = 0; x<tmpTexture.width; x++) {
                                 tmpTexture.SetPixel (x, y, originalTexture.GetPixel (x, y));
                         }
                 }
                 //Apply 
                 tmpTexture.Apply ();
         
                 //filling the temporary texture with the target texturetheT
                 for (int y =30; y<tmpTexture.height; y++) {
                         for (int x = 30; x<tmpTexture.width; x++) {
                             tmpTexture.SetPixel (x, y, targetTexture.GetPixel (x, y));
                         }
                 }
                 //Apply 
                 tmpTexture.Apply ();
                 
                 //change the object main texture 
                 renderer.material.mainTexture = tmpTexture;
         }
avatar image Eric5h5 · Jan 23, 2014 at 02:24 AM 1
Share

You would only need to create the texture once. You can also just instantiate the texture like you do with other objects.

avatar image Aladine · Jan 23, 2014 at 02:26 AM 0
Share

GENIUS!!

avatar image
0

Answer by Ucuri · Jan 23, 2014 at 01:52 AM

I had the same question just yesterday, take a look here:

http://answers.unity3d.com/questions/622444/setpixel-on-a-sprite-texture-without-changing-it-g.html

I supplied some code there, I don't know if it is the most elegant solution, but I created a new texture with the same height and width and just took all the pixels from the previous texture via setPixels32().

Comment
Add comment · 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 Aladine · Jan 23, 2014 at 01:53 AM 0
Share

that's exactly what i did :-) thank you

avatar image Eric5h5 · Jan 23, 2014 at 02:24 AM 1
Share

Use Instantiate ins$$anonymous$$d, it's a lot simpler.

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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Assigning UV Map to model at runtime 0 Answers

GetPixel Returning Inaccurate Color 1 Answer

Why are these two textures always the same? 1 Answer

setPixel not working with custom Colors 1 Answer

Set pixels on light cookie texture via script? 0 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