Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
8
Question by Subglitch · Dec 28, 2015 at 11:18 AM · rendertexture

Unexpected Release of RenderTexture

I have a RenderTexture which is targetTexture for the camera.

This RenderTexture created from a script in a following way:

 RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 0);
 rt.Create();
 camera.targetTexture = rt;

The problem is that sometimes, after about 2-3 min of game play the unexpected "release" of this RenderTexture occurs and I have:

  1. RenderTexture which state is "not created" (IsCreated() == false)

  2. RenderTexture is no more camera's target (camera.targetTexture == null)

  3. Message in the log: "Releasing render texture that is set as Camera.targetTexture!"


This also happens each time when I change vSync:

 QualitySettings.vSyncCount = QualitySettings.vSyncCount == 0 ? 1 : 0;


There is a notice about possibility of release on certain events on the RenderTexture scripting API's page: http://docs.unity3d.com/ScriptReference/RenderTexture.html

But this not explains much. Why these releases happen?

I've managed to handle this problem by simply checking IsCreated() and restoring the state. This code I placed in the end of the Update() method:

 if (!rt.IsCreated())
 {
     rt.Create();
     camera.targetTexture    = rt;
 }

While this solution works, for me it fills like it some kind of workaround and I still missing something...

Is this a good solution for the problem? Or maybe it should be handled in a different way ?

Comment
Add comment · Show 6
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 ai_enabled · Jan 20, 2016 at 06:59 AM 0
Share

I've the same problem. It seems checking IsCreated() is right thing to do. I would like to have an event when texture is reset...

avatar image pgomes · Feb 11, 2016 at 08:06 PM 0
Share

Even if isCreated is necessary, the log message "Releasing render texture that is set as Camera.targetTexture!" implies something is going wrong. Was this ever filed as a bug?

avatar image Ricna · Mar 21, 2016 at 09:27 PM 0
Share

Same problem here. Unity 5.3.3f1. I can not trace to understand when it occurs :/ Some news about this issue? Thanks!

avatar image flashframe Ricna · Mar 21, 2016 at 10:11 PM 0
Share

Have you tried updating to 5.3.4? I had a similar render texture issue that got fixed

avatar image Ricna flashframe · Mar 22, 2016 at 09:24 PM 0
Share

Same problem in 5.3.4 :(

avatar image Ricna · Mar 21, 2016 at 09:31 PM 1
Share

PS: In my case this error does not happen in Editor mode... Only in the build.

1 Reply

· Add your reply
  • Sort: 
avatar image
6

Answer by Ricna · Mar 26, 2016 at 10:28 PM

After a lot of debugs i found a solution (at least for me)....

So, I already tried to solve using the Create method. In true nothing changes using Create. Using or not the result after new RenderTexture will be "isCreated == true".

I solved my issue disabling the camera that use rendertexture every time that I need to do some of this actions:

1. When changing any attribute of RenderTexture after isCreated().

2. When changing Screen size or Resolution of the camera where was added a RenderTexture.

3. When changing VSyncCount of QualitySettings.

4. When changing Antialising of QualitySettings.

We don't need to use the WaitForEndOfFrame or even Coroutine in most cases. The only case for is while changing the resolution to Fullscreen, because it take some time and even with WaitForEndOfFrame I didn't have success, so was better to use a delay using WaitForSeconds(2);

In short, all that we need is to disable the camera before some of those actions and then enable again after all changes was completed.

I created this method to be called from any case:

 public void SwitchOnOffAllCameras(bool enabled) {
         for (int i = 0; i < XMLHandler.Instance.layersNow; i++) {
             camLayer[i].GetComponent<Camera>().enabled = enabled;
         }
 }

NOTE: In my case I use 1,2,3 or 4 cameras using RenderTexture, but you can simplify using only something like:

 public void SwitchOnOffCameraWithRenderTexture(bool enabled) {
         camera.enabled = enabled;
 }

I like to use methods to identify where I'm changing the attribute and if needed change something only there, as I need to call it from other script too its very better to handle.

So... I called that method before to change something that was causing the error. After all is done I called it again: SwitchOnOffAllCameras(true);

Example 1 (quality settings):

 SwitchOnOffAllCameras(false);
 QualitySettings.antiAliasing = QualitySettings.antiAliasing ==0?2: QualitySettings.antiAliasing *2;
 SwitchOnOffAllCameras(true);


Example 2 (fullscreen issue):

 private IEnumerator SwitchFullscreen() {
         CameraHandler.Instance.SwitchOnOffAllCameras(false);
         if (Screen.fullScreen) {
             Screen.SetResolution(940, 720, false);
         } else {
             Screen.SetResolution(Screen.currentResolution.width, Screen.currentResolution.height, true);
         }
         yield return new WaitForSeconds(2);
         CameraHandler.Instance.SwitchOnOffAllCameras(true);
 }

Example 3 (creating camera and RenderTexture): In this case I didn't use that method as I'm creating cameras and RenderTextures for the first time and would generate a null exception.

 private void CreateCamerasForRenderTexture() {
         Debug.LogAssertion("Before Create RT");
         camLayer = new GameObject[XMLHandler.Instance.layersNow];
         renderTextureLayer = new RenderTexture[XMLHandler.Instance.layersNow];
         for (int i = 0; i < XMLHandler.Instance.layersNow; i++) {
             camLayer[i] = new GameObject("CamLayerAB" + i);
             camLayer[i].AddComponent<Camera>();
             camLayer[i].GetComponent<Camera>().enabled = false;
             camLayer[i].GetComponent<Camera>().orthographic = true;
             camLayer[i].GetComponent<Camera>().orthographicSize = 0.6f * (XMLHandler.Instance.scale);
             camLayer[i].transform.parent = cam.transform;
             camLayer[i].transform.localPosition = Vector3.zero;
             camLayer[i].transform.localRotation = Quaternion.identity;
             camLayer[i].GetComponent<Camera>().backgroundColor = new Color(0, 0, 0, 0);
             camLayer[i].GetComponent<Camera>().cullingMask = 1 << LayerMask.NameToLayer(XMLHandler.Instance.layerNameToApply[i]);
             camLayer[i].GetComponent<Camera>().useOcclusionCulling = false;
             renderTextureLayer[i] = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32);
             renderTextureLayer[i].name = "RenderTextureLayerAB" + i;
             camLayer[i].GetComponent<Camera>().targetTexture = renderTextureLayer[i];
             camLayer[i].GetComponent<Camera>().renderingPath = RenderingPath.Forward;
             camLayer[i].GetComponent<Camera>().enabled = true;
         }
         Debug.LogAssertion("After Create RT");
 }

To discover where (in my case) was occurring the error I create a LOT of Debug.LogAssertion() entries to see in the console of Standalone build.

NOTE: I listed 4 cases, but probably have more situations where the issue could happens.

I hope that it can help someone.

Comment
Add comment · Show 2 · 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 SoOoOnh · Jun 10, 2016 at 07:09 AM 0
Share

In case "Alt+Enter", how can i switch off camera before screen change.

avatar image gianglthbl · Dec 30, 2016 at 01:44 AM 0
Share

Incase full screen, I implemented SwitchOnOffAllCameras function. It worked on Windows but not on OSX. do you know any other solution?

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

10 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

Related Questions

How to get the depth values of the camera view? 1 Answer

Possible to simulate screen raycast? 0 Answers

Problem with Texture Scaling for Mobile Resolutions 1 Answer

Creating a portal/window view with rendertextures 0 Answers

Render textures doesnt exist in unity 4.7.2 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