Skip to main content

Texture from Assets folder to Texture2D and the save?

I have a Texture in the Assets folder with (Texture Type = Advanced, Read/Write Enabled = true, Max Size = 512, Format = RGB 24 bit). Now I want to read this texture and then copy the pixels into another texture2d and save that.

private Texture2D sourceImg;

...

Texture2D tex = new Texture2D(512, 512, TextureFormat.RGB24, false);
tex.SetPixels(sourceImg.GetPixels(0,0,512,512));
tex.Apply();
byte[] png = tex.EncodeToPNG();
File.WriteAllBytes(Application.persistentDataPath + "/Screenshot.png", png);

I don't know, why this simple code is not working?

When I add the new tex to a quad with:

quad.renderer.material.mainTexture = tex;

It works.

Solved

OK, I solved it. Just do this: Go to Edit -> Project Settings -> Player -> Other Settings and change Write Access to External.


Thanks! Works for me. Suggest change this: File.WriteAllBytes(Application.persistentDataPath + "/Screenshot.png", png); by a custom output folder, like this: File.WriteAllBytes("C:/OutputFolder" + "/Screenshot.png", png);


Comments

Popular posts from this blog

Imitate join for NoSQL document database

Are there any workarounds to execute join-like query with NoSQL document database? Example: We need to select last month articles by users with rating more than thousand. SQL solution is SELECT a.* FROM Articles as a INNER JOIN Users as u ON a.UserId = u.Id WHERE a.Date > (Now - Month) AND u.Rating > 1000 I can imagine several NoSQL solutions. First is two queries solution: Retrieve users with rating more than 1000 Retrieve last month articles for these users I don't like it as I have to make two queries and I have to retrieve all users with rating > 1000 (what if I have 1kk of users?) The other NoSQL solution which comes to my mind is denormalization. But I am not big fan of it. I would be not against of putting comments collection to post entity (because comments belong to post), but I don't like to put user inside article or articles inside user. Are there any other solutions? Solved You can do that using Multi Maps / Reduce indexes with Rave...