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 JuanGomez · Jun 20, 2014 at 03:24 PM · spritetexture2dassetssetpixelsapply

Texture2D SetPixels/Apply changes not permanent.

I am trying to modify a Texture2D and the changes are not being saved. Here is my problem:

 Texture2D texture = spriteRenderer.sprite.texture;
 texture.SetPixels(pixels);
 texture.Apply();

Now the changes are visible in the screen. If I go to another scene where the texture is not visible, it returns to the original (like re-imported). It happens everywhere (editor, mobile) and I don't want it. Also happens if I restart the game.

According to the docs: "Texture2D assets are never automatically instantiated when modifying them in iOS and Android projects. Any modifications made to these assets through scripting are always permanent, and never undoable. So if you change the pixels of a texture file, the change is permanent." (http://docs.unity3d.com/Manual/ModifyingSourceAssetsThroughScripting.html)

Does anyone knows why my assets are not being modified as the documentation clearly states? am I missing something?

Thanks

Comment
Add comment · Show 1
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 robertbu · Jun 20, 2014 at 03:42 PM 0
Share

Note that the reference page is referring to modifying the texture of the shared$$anonymous$$aterial of a normal game object, not a Sprite. And I believe this only works in the Editor (though never tested it).

2 Replies

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

Answer by JuanGomez · Aug 12, 2014 at 11:59 AM

As avianling hinted, this is not possible to to at all outside the inspector.

This a possible workaround:

 protected void SaveTexture(Texture2D texture)
     {
         path = Application.persistentDataPath + "/" + texture.name;
         File.WriteAllBytes(path, texture.EncodeToPNG());
 
     }
     
 protected Texture2D RetriveTexture(Texture2D texture)
     {
         
         Texture2D newTexture = new Texture2D(texture.width, texture.height);
 
         if (File.Exists(path)){
             newTexture.LoadImage(File.ReadAllBytes(path));
         } else {
             newTexture = texture;
         }
   
         return newTexture;
     }


Then you can set this two functions OnDisable and in OnEnable to automatically do it. In the editor you can still get some weird behaviors. But on play mode it would be like if you effectively override the original texture (which is sadly impossible).

Also note that you need to know the width and height of the texture before loading, as you cannot easily retreive that information from the file.

You can mark this as answered :)

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

Answer by avianling · Jun 24, 2014 at 10:56 PM

I encountered a similar problem - I was able to bypass the issue by the following:

 System.IO.File.WriteAllBytes( path, tex.EncodeToPNG() )

If you are working in an editor script you can automatically determine the path of the script with this:

 string path = Application.dataPath + "/../" + AssetDatabase.GetAssetPath( tex );

This won't work on some build targets, such as the web player, but hopefully will suffice as a workaround.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

SetPixels/Apply: Texture2D resets on restart 0 Answers

Draw tilemap with Texture2D.SetPixels: Performance and Memory? 0 Answers

Fastest rendering of 2D world with lots of changes to screen every frame? 3 Answers

For different texture sizes, which is faster? SetPixel or SetPixels? 2 Answers

Applying Textures from atlas? 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