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
5
Question by brunopava · Jun 23, 2014 at 12:53 PM · spritetexture2dscreenshot

[CLOSED]How to take a screenshot and apply it as a texture to a sprite?

So what im trying to do is take a render from the screen, turn it into a Texture2D and apply it to a sprite using Sprite.Create();

Here is my code:

 // instance of the camera im getting on Awake()
 private Camera _camera;
 
 private Texture2D _screenShot;
 
     private IEnumerator TakeScreenShot()
         {
             yield return new WaitForEndOfFrame();
     
             RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
             _camera.targetTexture = rt;
             _screenShot= new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
             _camera.Render();
             RenderTexture.active = rt;
             _screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
             _camera.targetTexture = null;
             RenderTexture.active = null;
             Destroy(rt);
     
             string filename = ScreenShotName(resWidth, resHeight);
     
             //byte[] bytes = _screenShot.EncodeToPNG();
             //System.IO.File.WriteAllBytes(filename, bytes);
 
             Debug.Log(string.Format("Took screenshot to: {0}", filename));
     
             Sprite tempSprite = Sprite.Create(_screenShot,new Rect(0,0,resWidth,resHeight),new Vector2(0,0));
             GameObject.Find("SpriteObject").GetComponent<SpriteRenderer>().sprite = tempSprite;
         }
 

All it does is change the SpriteObject.sprite color to white. But when I try to save the content of the screenshot it turns out that there is actualy an image and not just white.

Comment
Add comment · Show 7
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 Jamy4000 · Jul 24, 2017 at 02:39 PM 0
Share

I know it's an old post, but you saved my life with your code mate, so just wanted to say thank you sooooooo much !

avatar image Akylbekuly · Sep 11, 2017 at 08:15 AM 0
Share

What is the ScreenShotName, it says it doesn't exist, am I missing something?

avatar image Jamy4000 · Sep 11, 2017 at 08:27 AM 0
Share

ScreenShotName is a method directly made by brunopova, you don't really need it. Here's my version of the code if you're interested :

 private IEnumerator OnPause()
     {
         yield return new WaitForEndOfFrame();
 
         //Create a new Texture2D of the Height and width of the screen, before reading all the pixels and 
         //put them in the ney Texture2D
         texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
         texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
         
         texture.Apply();
 
         //Create a bytes array containing the texture2D
         byte[] b = texture.EncodeToPNG();
 
         //Path + name of the screenshot
         string date = System.DateTime.Now.ToLongTimeString();
         screenShotPath = Application.persistentDataPath + "/ScreenShot_" + date + ".png";
 
         //save the screenshot
         File.WriteAllBytes(screenShotPath, b);
 
         texture.LoadImage(b);
 
         //create a new sprite with the texture2D
         Sprite sprite = Sprite.Create(texture, 
                                       new Rect(0.0f, 0.0f, texture.width, texture.height), 
                                       new Vector2(0.5f, 0.5f),
                                       100.0f);
 
         //Put the new created sprite inside a panel
         Comment$$anonymous$$odeBackground.GetComponent<UnityEngine.UI.Image>().sprite = sprite;
         Comment$$anonymous$$odeBackground.GetComponent<UIController>().Show();
 
         RemoveAndAddElementFromScreen(true);
     }

The last line is another method I created to disable all UI element of screen to avoid having them when we make the screenshot.

avatar image Akylbekuly Jamy4000 · Sep 11, 2017 at 10:57 AM 0
Share

Thanks, man. I made it, but this is not what I searched for =(. This code makes rectangular screenshots, what I searched is can be any shape.

avatar image Jamy4000 Akylbekuly · Sep 11, 2017 at 12:06 PM 0
Share

Oh ok, so maybe should you add before calling this method another function which create a mask with the shape that you want ?

Show more comments

1 Reply

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

Answer by PAEvenson · Jun 23, 2014 at 01:16 PM

Perhaps you are missing apply after you read the pixels?

 _screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
 
 _screenShot.Apply(); //Add this?
 
 _camera.targetTexture = null;
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 brunopava · Jun 23, 2014 at 01:18 PM 0
Share

DUUUUDE! You nailed it! Thank you so much!

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

25 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Texture2D.GetPixels() to take a screenshot and then show it on an Image - iOS problems. 1 Answer

Struggling to find how to access the Texture 2D of a sprite. 1 Answer

Coding my own auto-slicer, getting "islands" of pixels at runtime? 1 Answer

Convert a Texture2D to Sprite 2 Answers

Show Screenshot image on GUI, linear color space , Need help 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