Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Destroying Textures/Texture2D in the right way!

Discussion in 'Scripting' started by Martin_Gonzalez, Jan 23, 2015.

  1. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    Hi everybody, today in my work i have an issue where the app we are developing crashes. The application is a drawing app where lots of textures are created, cloning etc.
    The problem is that when i generate random screens (with objects to paint) the memory starts growing in the device (Android-iOS).
    I've been reading a lot about how to destroy textures but i have differents behaviours, so it would be nice to have the point of another developer.

    I read that we have to do some of these things:
    Texture.Destroy(myTexture);
    Resources.UnloadUnusedAssets();
    Destroy(renderer.material)

    And what i've tried was doing the Texture.Destroy of each variable when i will not using it again and after that calling Resources.UnloadUnusedAssets();
    Is expensive calling Resources.UnloadUnusedAssets()?
    Should i call it after the destruction of each texture?

    I would like to clean up the memory and not leave any texture living in memory.

    Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Just call it once when you're done destroying everything.

    --Eric
     
  3. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    You mean, i have to destroy the textures and in the end of destroying all i call resources?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Yes.

    --Eric
     
  5. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    Eric, do you have any book or blog or anything relative with RAM and Unity (how it works), i mean, if i do a new() what happen, and when you do a destroy, etc etc.?

    And thanks for the answer