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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by lollie009 · May 22, 2015 at 04:41 PM · camerarendertextureunity 4.6memory-leakreadpixels

Unity4 problem - Bypassing camera renderTexture with ReadPixels but system out of memory

I made a game in Unity 4 Pro and it doesn't seem to be running in Unity 5 so I want to keep working in Unity 4. The problem is the pro trial expired and therefore I no longer have the render texture to camera feature. So I'm trying to bypass this issue by using ReadPixels instead.

What I need to do is to create a render of couple of layers from a camera and apply it as a texture onto an object over a period of time. Just to give a bit more context, it's a hinting mechanism for this point and click game, when you go over the scene with this magnifying glass, the magnifying glass displays highlighted objects that you can interact with. I have it sort of working. It takes screenshots of the layers over a period of time and displays it on the magnifying glass but then the memory runs out and Unity crashes saying:

"Fatal error! Could not allocate memory; System out of memory!... "

As you'll see in the code, I'm trying to release or reset the grabTexture because I believe this is what is causing the problem but I'm not sure of the proper way to do this.

 using UnityEngine;
 using System.Collections;
 
 public class CameraGrabScript : MonoBehaviour
 {    
     public bool grab = true;
     //public Texture2D grabTexture;
     public Renderer magglass;
     
     private void OnPostRender()
     {
         if(grab)
         {
             Debug.Log("Grabbing");
             Texture2D grabTexture = new Texture2D(Screen.width, Screen.height);
             grabTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
             grabTexture.Apply();// mip maps
             magglass.material.mainTexture = grabTexture;
 
             grabTexture = null;
             //Resources.UnloadAsset(grabTexture); //function can only be called on Assets that are stored on disk.
             //Destroy(grabTexture);
 
             //grab = false;
 
             Debug.Log("grabTexture is null");
         
         }
 
 
     }
 }


Thank you for your help.

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 komodor · May 28, 2015 at 06:59 PM 0
Share

not sure where your error is, i just don't get why you null that grabTexture ... it's reference, so if you null it you null also the mainTexture on material

and also, you should uncomment the grab = false; (that might make the memory leak)

and if you are trying to do it every frame ... then just forget it and use render texture

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hu90 · May 28, 2015 at 07:38 PM

You are allocating a new Texture every frame, with the size of the screen. Even if the garbage collector could keep up with this, it's terrible.

You should instantiate a texture the size of the screen just once instead of OnPostRender (then ReadPixels on it OnPostRender if you want).

But my advice in your case would be to upgrade to Unity 5, all the engines features are free. Upgrading your project will likely take minutes and you'll save yourself some headaches.

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

20 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

Related Questions

Render Texture does not work on only IOS devices 0 Answers

Camera size and RenderTexture 1 Answer

ReadPixels Performance 0 Answers

Can a Texture2D be created at runtime from a snapshot of a RenderTexture? 3 Answers

Why is ReadPixels on my RenderTexture creating a pure gray texture? 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