Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
This question was closed Feb 25, 2018 at 06:23 PM by Chambers88 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Chambers88 · Feb 25, 2018 at 03:38 PM · spriteui imagescreenshot

Show screen capture as UI image

Hello, I'm trying to take a picture of the screen and assign it as a sprite for an UI image.

However, for some reason, all I get is a white sprite.

 public Camera renderCam;
 int photoWidth = 100, photoHeight = 100;
 
 public void LateUpdate()
     {
         if (snapped)
         {
             RenderTexture rt = new RenderTexture(photoWidth, photoHeight, 24);
             renderCam.targetTexture = rt;
             RenderTexture.active = rt;
             renderCam.Render();
             Texture2D screenShot = new Texture2D(photoWidth, photoHeight, TextureFormat.RGB24, false);
             screenShot.ReadPixels(new Rect(0, 0, photoWidth, photoHeight), 0, 0);
             renderCam.targetTexture = null;

             GameObject flyToHud = GameObject.Instantiate(photoToHudAnimation);    
             flyToHud.GetComponent<Image>().sprite = Sprite.Create(screenShot, new Rect(0, 0, photoWidth, photoHeight), new Vector2(0, 0));
 
             Camera.main.targetTexture = null;
             RenderTexture.active = null; // JC: added to avoid errors
             Destroy(rt);
             snapped = false;
         }
     }

I'm pretty sure the problem is with Sprite.Create(), for 2 reasons:

1) I tried saving the RenderTexture rt to disk as PNG right before the Sprite.Create() method and it worked, so it is indeed storing a capture of the screen.

2) I also already tried simply assigning a public sprite otherSprite to the instantiated object like this:

 flyToHud.GetComponent<Image>().sprite = otherSprite;

and the proper sprite was shown. So I'm sure there is no other part of the code replacing the sprite with a white rectangle.

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

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by Chambers88 · Feb 25, 2018 at 06:22 PM

I realised it was not a pure white sprite, but a very bright grey (205) sprite, which allowed me to find similar questions.

Just fixed it by calling screenShot.Apply() before creating the sprite.

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 angelonit · Nov 02, 2021 at 06:37 PM 0
Share

Thank you for this, I used it to make a cool transition :)

Follow this Question

Answers Answers and Comments

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

Related Questions

Resource.Load always returning null when trying to load an image 1 Answer

HOW TO GET AN APPEARANCE OF A SCENE BY SCRIPT?????????? 0 Answers

Can I use unity UI image in the ARKIT ? 0 Answers

Object reference not set to instance of an object? 1 Answer

How to check if two sprites are the same 2 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