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
3
Question by Catlard · Oct 14, 2012 at 02:18 PM · texture2dpngformatreadpixelsencode

Saving a Texture2D as a PNG

SOLVED 5 MINUTES AFTER ASKING, BY SHEER FORCE OF WILL. AND FOR REMEMBERING GETPIXELS.

Hey, folks!

So, I've got this function. It attempts to save a Texture2D from a material's mainTexture property, so it can be saved to the desktop. I can't use readpixels, because the size of the texture is actually larger than the screen. But when I attempt to either change the format of a current texture2D so it can be encoded, I run into problems. Specifically, I'm getting this problem: Assets/Scripts/Utils/SaveTextureToDesktop.cs(16,30): error CS0200: Property or indexer `UnityEngine.Texture2D.format' cannot be assigned to (it is read only) My function is below. Is there something I'm missing on how to do this?

 public void Save() {
         Texture2D savedTexture = _materialToSave.mainTexture as Texture2D;
         Texture2D newTexture = new Texture2D(savedTexture.width, savedTexture.height, TextureFormat.ARGB32, false);
         
         newTexture.SetPixels(0,0, savedTexture.width, savedTexture.height, savedTexture.GetPixels());
         newTexture = FillInClear(newTexture);
         newTexture.Apply();
         byte[] bytes = newTexture.EncodeToPNG();
         File.WriteAllBytes(Application.dataPath + "/../testscreen-" + imageCount + ".png", bytes);
         //Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
     
         imageCount++;
     }    
Comment
Add comment · Show 5
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 wlad_s · May 04, 2013 at 11:09 PM 0
Share

Hi! Thanks for sharing this. I tried to implement this in UnityScript. I translated everything but it says: "$$anonymous$$ identifier: 'FillInClear'." I tried searching the net for that "FillInClear" but nothing came up. Could you please tell me what is it and what does it do?

avatar image Catlard · May 05, 2013 at 09:12 AM 0
Share

Hi there. FillInClear isn't a unity feature. It's a function I wrote that's not in this script. So just tak,e out that line, and you're good.

All it did was fill in transparent pixels with white.

avatar image wlad_s · Sep 07, 2013 at 12:54 PM 0
Share

Forgot to say thank you for FillInClear explanation, it was a busy day, so... thank you :)

avatar image Catlard · Sep 07, 2013 at 03:14 PM 0
Share

Great! glad I could help. Here's to busy days!

avatar image Obsdark · Jul 28, 2017 at 07:24 PM 0
Share

So... this code in the question is actualy working? or is the one from before you solve it?

3 Replies

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

Answer by Catlard · Oct 14, 2012 at 02:29 PM

AVAST! I have figured it out, and edited the script above, to reflect my solution. Thank ye, gigantic teddy bear of the internet to whom I tell all my problems!

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 wonker21 · Sep 07, 2013 at 12:04 PM 0
Share

Can you help me? :(

avatar image Catlard · Sep 07, 2013 at 03:03 PM 0
Share

Sure! I'll post the full script that I used here:

using UnityEngine; using System.Collections; using System.IO; using System;

public class SaveTextureToDesktop : Singleton$$anonymous$$onoBehaviour {

     public Camera _cameraWithRendTexture;
     public Text$$anonymous$$esh _percent$$anonymous$$esh;
     public Text$$anonymous$$esh _name$$anonymous$$esh;
     public int _heightInPixelsOfCertificate = 1488;
     
     public void SaveCertificate() {
                     
             if(!_cameraWithRendTexture)
                     _cameraWithRendTexture = GameObject.Find("CertificateCamera").GetComponent<Camera>();
             if(!_name$$anonymous$$esh)
                     _name$$anonymous$$esh = GameObject.Find("bName").GetComponent<Text$$anonymous$$esh>();
             if(!_percent$$anonymous$$esh)
                     _percent$$anonymous$$esh = GameObject.Find ("dPercent").GetComponent<Text$$anonymous$$esh>();
             RenderTexture.active = _cameraWithRendTexture.targetTexture;
             Texture2D newTexture = new Texture2D(_cameraWithRendTexture.targetTexture.width, _cameraWithRendTexture.targetTexture.height, TextureFormat.ARGB32, false);
             newTexture.ReadPixels(new Rect(0,0,_cameraWithRendTexture.targetTexture.width, _cameraWithRendTexture.targetTexture.height), 0,0,false);
             newTexture.Apply();
             Color[] colorArrayCropped = newTexture.GetPixels(0,newTexture.height-_heightInPixelsOfCertificate, newTexture.width, _heightInPixelsOfCertificate);
             Texture2D croppedTex = new Texture2D(newTexture.width,_heightInPixelsOfCertificate, TextureFormat.ARGB32, false);
             croppedTex.SetPixels(colorArrayCropped);
             croppedTex = FillInClear(croppedTex, _cameraWithRendTexture.backgroundColor);
             croppedTex.Apply();
     byte[] bytes = croppedTex.EncodeToPNG();
     File.WriteAllBytes(PathBuilder.instance.GetFilePath(FileType.Certificate) + _name$$anonymous$$esh.text +"'s certificate (" +_percent$$anonymous$$esh.text + ")" +".png", bytes);
             
     
     }       
     
     public Texture2D FillInClear(Texture2D tex2D, Color whatToFillWith) {
             
             for(int i = 0; i < tex2D.width; i++) {
                     for(int j = 0; j < tex2D.height; j++) {
                             if(tex2D.GetPixel(i,j) == Color.clear)
                                     tex2D.SetPixel(i,j, whatToFillWith);
                     }
             }
             return tex2D;
     }

}

avatar image wonker21 · Sep 07, 2013 at 06:17 PM 0
Share

Thanks :) but do you have a free $$anonymous$$ute to check out my problem? Can you help me :) Thanks http://answers.unity3d.com/questions/531887/encodetopng-and-webcamtexture-problem.html

avatar image
0

Answer by SunzWong · Jun 26, 2015 at 07:56 AM

can i ask what are these for? if(!_nameMesh) nameMesh = GameObject.Find("bName").GetComponent(); if(!_percentMesh) percentMesh = GameObject.Find ("dPercent").GetComponent();

Comment
Add comment · Show 1 · 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 andrew-lukasik · Mar 25, 2016 at 02:56 PM 1
Share
 if( !_name$$anonymous$$esh ) {  }

just means:

 if( _name$$anonymous$$esh==null ) {  }
avatar image
0

Answer by jemonsuarez · Aug 27, 2016 at 05:39 AM

If anybody finds this useful, this asset does it all and includes a lot of other features:

File Management: Easy way to save and read files.

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

15 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

Related Questions

Save rendertexture to image in file 2 Answers

EncodeToPng and WebCamTexture PROBLEM 4 Answers

Wait for Texture.ReadPixels 1 Answer

ReadPixels requires an offset while in the Editor? 2 Answers

Issue With Simultaneous ReadPixels Calls 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