Search Unity

Downloading multiple images takes up a lot of RAM in VR

Discussion in 'Scripting' started by alejandroestebanjaime, Jun 22, 2018.

  1. alejandroestebanjaime

    alejandroestebanjaime

    Joined:
    Jan 3, 2017
    Posts:
    1
    I present my problem / concern.
    I am developing an App in which I have a skybox of 6 sides and this represents a place, then to go forward to the next place I should replace the textures with the following ones ... and so on. So to make it look more fluid, I use another cubemap maintaining the previous texture and through an animation I made a "fade" effect after downloading the new textures, showing the skybox with 6 sides.

    Here I call the coroutina that downloads the textures of the 6-sided skybox:
    Code (CSharp):
    1. StartCoroutine (CorFrame ("_ RightTex", ServerUrl + nx + ext));
    2. StartCoroutine (CorFrame ("_ DownTex", ServerUrl + ny + ext));
    3. StartCoroutine (CorFrame ("_ BackTex", ServerUrl + nz + ext));
    4. StartCoroutine (CorFrame ("_ LeftTex", ServerUrl + px + ext));
    5. StartCoroutine (CorFrame ("_ UpTex", ServerUrl + py + ext));
    6. StartCoroutine (CorFrame ("_ FrontTex", ServerUrl + pz + ext));
    7. StartCoroutine (Wait ());
    Texture download method:

    Code (CSharp):
    1.  private IEnumerator CorFrame(string nameTex, string url)
    2. {
    3.       Destroy(skybox.GetTexture(nameTex));
    4.       UnityWebRequest www =
    5.       UnityWebRequestTexture.GetTexture(url);
    6.       // Wait for download to complete
    7.       yield return www.SendWebRequest();
    8.       if (www.isNetworkError)
    9.       {
    10.           Debug.Log(www.error);
    11.       }
    12.       else
    13.       {
    14.           skybox.SetTexture(nameTex,
    15.          
    16.          Instantiate(DownloadHandlerTexture.GetContent(www)));
    17.          skybox.GetTexture(nameTex).wrapMode =
    18.          TextureWrapMode.Clamp;
    19.           Destroy(DownloadHandlerTexture.GetContent(www));
    20.           countFrame++;
    21.         }
    22. }

    Then I wait for it to finish downloading the 6 textures showing the cubemap with the previous textures and at the end I apply the fade effect as follows:

    Code (CSharp):
    1. private IEnumerator FadeOutAnimation ()
    2. {
    3.      // Get current color
    4.       Color spriteColor =
    5.      mngBttnInteraction.cubeMap.GetComponent
    6.     <MeshRenderer> (). Material.color;
    7.     float counter = 0;
    8.     float duration = 0.5f;
    9.     float alpha = 1;
    10.     while (alpha> 0)
    11.     {
    12.         counter + = Time.deltaTime;
    13.         // Fade from 1 to 0
    14.         alpha = Mathf.Lerp (1, 0, counter / duration);
    15.         // Change alpha only
    16.        SetAlpha (spriteColor, alpha);
    17.        yield return null;
    18.     }
    19.     // Disable cubemap
    20.     mngBttnInteraction.cubeMap.SetActive (false);
    21.     SetAlpha (spriteColor, 1);
    22.     // Destroy textures cubemap
    23.     for (int i = 0; i <6; i ++)
    24.     {
    25.         DestroyImmediate
    26.        (mngBttnInteraction.cubeMap.GetComponent
    27.       <MeshRenderer> (). Materials [i] .GetTexture ("_ MainTex"),
    28.       true);
    29.     }
    30.     // Update with new textures of skybox
    31.     mngBttnInteraction.cubeMap.GetComponent
    32.     <MeshRenderer> (). materials [0] .SetTexture ("_ MainTex",
    33.     (Texture2D) Instantiate (skybox.GetTexture ("_ DownTex")));
    34.     mngBttnInteraction.cubeMap.GetComponent
    35.     <MeshRenderer> (). materials [1] .SetTexture ("_ MainTex",
    36.     (Texture2D) Instantiate (skybox.GetTexture ("_ UpTex")));
    37.     mngBttnInteraction.cubeMap.GetComponent
    38.     <MeshRenderer> (). materials [2] .SetTexture ("_ MainTex",
    39.     (Texture2D) Instantiate (skybox.GetTexture ("_ RightTex")));
    40.     mngBttnInteraction.cubeMap.GetComponent
    41.     <MeshRenderer> (). materials [3] .SetTexture ("_ MainTex",
    42.     (Texture2D) Instantiate (skybox.GetTexture ("_ FrontTex")));
    43.     mngBttnInteraction.cubeMap.GetComponent
    44.     <MeshRenderer> (). materials [4] .SetTexture ("_ MainTex",  
    45.    (Texture2D) Instantiate (skybox.GetTexture ("_ LeftTex")));
    46.    mngBttnInteraction.cubeMap.GetComponent
    47.    <MeshRenderer> (). materials [5] .SetTexture ("_ MainTex",
    48.    (Texture2D) Instantiate (skybox.GetTexture ("_ BackTex")));
    49.      
    50.    UpdateRotationCubemap (currentFrame.rotationOffset);
    51.    mngBttnInteraction.ContainerNavigation.SetActive (true);
    52. }

    The problem is that by destroying the textures used it will destroy all its references and those that use it, leaving my cubemap without texture. To solve that, I need to intact (clone) the applied texture and then destroy it. But this leads to a notorious FREEZE of the system. Researching found that creating intangibles of objects or textures leads to a decrease in performance. For what I need if you can help me, perhaps to find a new way to do this effect, or to manage my ram memory when I download textures and destroy it.


    Things you should know:
    - Unity 2018.1.0.0f2
    - Visual Studio 2017
    - C#

    Thank you very much
    regards
    Saludos!