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 Ucuri · Jan 22, 2014 at 12:16 AM · spritetexture2ddecalsetpixel

SetPixel on a sprite texture without changing it globally

Hi, so I spent the last days experimenting with different solutions for a kind of "decal" system for my game. I found that setPixel() would probably be the best for my specific case.

The situation now is that I have a Sprite (in 3D-Space), and I want to change some Pixels on it. In order to set pixels on the sprite I had to make the texture of the sprite writable. But now everytime I set a pixel, the texture changes on all objects that use it. It even stays like that when I restart the scene.

Now this does not seem like unintended behaviour and at least kind of makes sense, but is there some way to just edit the texture on one specific object?
(And if yes, is it still feasible performance-wise?)

I don't know if its needed, but here is the code I am using. The calculation which pixel is supposed to be set is only a placeholder, I'm going to deal with that if I get SetPixel() to work

 //C#
 SpriteRenderer renderer = hit.collider.GetComponentInChildren<SpriteRenderer>();
 Texture2D tex = renderer.sprite.texture;
 Vector2 pixelUV = hit.textureCoord;
 pixelUV.x *= tex.width;
 pixelUV.y *= tex.height;
 tex.SetPixel((int) pixelUV.x, (int) pixelUV.y, Color.black);
 tex.Apply();
Comment
Add comment · Show 2
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 ArkaneX · Jan 22, 2014 at 11:39 AM 2
Share

As Drakulo answered your question, I will only add, that I would bother more about memory consumption than performance. This will depend on your texture size and number of decals that you want to create, but you have to keep an eye on this.

And don't forget to call Compress on your texture clone, after you call Apply.

avatar image Ucuri · Jan 22, 2014 at 11:21 PM 0
Share

Oh, I didn't even notice there was a compress-method.

Thanks for the good advice!

1 Reply

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

Answer by Drakulo · Jan 22, 2014 at 11:08 AM

By doing this, you write the decal in your asset folder texture. That's why restarting keep it on the texture : you modified the texture file itself. You should disable the possibility to write your base texture and use a clone instead. Modifying the clone's pixels will not affect the original one.

Comment
Add comment · Show 3 · 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 Ucuri · Jan 22, 2014 at 11:19 PM 0
Share

Thanks, that was it!

I am now using this code, this seems to do what I want.

 SpriteRenderer renderer = hit.collider.GetComponentInChildren<SpriteRenderer>();
 Texture2D tex = renderer.sprite.texture;
 //Get hit position (placeholder)
 Vector2 pixelUV = hit.textureCoord;
 pixelUV.x *= tex.width;
 pixelUV.y *= tex.height;
 
 //Replace texture
 Texture2D newTex = new Texture2D (tex.width, tex.height, TextureFormat.ARGB32, false);
 newTex.SetPixels32(tex.GetPixels32());
 newTex.SetPixel((int) pixelUV.x, (int) pixelUV.y, Color.black);
 newTex.Apply();
 renderer.sprite = Sprite.Create(newTex, renderer.sprite.rect, new Vector2(0.5f, 0.5f));
avatar image ArkaneX · Jan 23, 2014 at 08:25 AM 0
Share

Ins$$anonymous$$d of creating new texture and getting/setting pixels, you can use Instantiate:

 Texture2D newTex = (Texture2D)GameObject.Instantiate(tex);
avatar image Drakulo · Jan 23, 2014 at 08:42 AM 0
Share

Yes, I agree with @ArkaneX. Apply() can be an expensive operation, use it wisely : Texture2D.Apply

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

Erasing a part of an image using SetPixel without gaps 2 Answers

How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 1 Answer

Painting sprite in unity 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Render a text on the sprite 1 Answer


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