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

Exclude loading a sprite before run-time, how to load sprites/textures on the fly?

Discussion in 'UGUI & TextMesh Pro' started by yakm, Sep 25, 2014.

  1. yakm

    yakm

    Joined:
    Sep 12, 2014
    Posts:
    24
    Are there any ways to load sprites/textures on the fly? Without loading the sprite/texture before run-time using the inspector and using Texture Type -> Sprite(2D and UI) -> Apply?

    Or is there a way to unload/load sprites/textures from memory during run-time?

    We don’t want all the UI stuff in memory during the gameplay. Especially the huge background images.

    Is there any information on how unity 4.6 handles the memory management of sprites/textures?
     
    Last edited: Sep 25, 2014
  2. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Unity only loads the textures needed to display the current game objects, and swaps textures in and out as needed, so just have then in your scene but hide them, they will get loaded when they are needed (they'll be in ram but not in GPU memory, if you don't want them to even be there, you'll have to script loading them)
     
  3. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    I believe you need to use script

    Resources.Load<Sprite>()

    to load from Resources folder
     
  4. yakm

    yakm

    Joined:
    Sep 12, 2014
    Posts:
    24

    So in unity 4.6, by adding images/sprites/textures using the system in the inspector -> Texture Type -> Sprite(2D and UI) -> Apply. Unity automatically unloads and loads these assets as needed during runtime?

    For instance, I have a main menu with a bunch of UI elements such as text, buttons, and panels only for the main menu.

    The main menu assets should be unloaded during Game play because they are only used on the main menu, so that they are not floating around in memory during game play. Does unity automatically do this? If not, how would i unload the assets that are not needed during game play.

    Note : our game is being built in entirely one scene.
     
  5. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    Sort of, they would exist in ram and would be referenced, but would not exist in GPU memory, which is generally where it counts as this is what really impacts performance. If you don't even want them in ram, use the resources and destroy them after you don't need them, but I'd guess, unless you graphics are huge fullscreen images, you'd be fine just turning them off.
     
  6. adhdchris

    adhdchris

    Joined:
    Nov 13, 2013
    Posts:
    45
    If your game uses a lot of uncompressed sprites and you're targeting lowe-end mobile devices, just use the Resources.Load() to load your UI and Resources.UnloadUnusedAssets() once you destroy it. Just make sure you don't have any hard references to the sprites in your scene otherwise this will achieve nothing.
     
  7. yakm

    yakm

    Joined:
    Sep 12, 2014
    Posts:
    24
    F

    We have several huge background images, how would i remove these from memory? Looks like the Sprite atlas has to be setup before runtime... Maybe using asset bundles for these background images?
     
  8. Jonny-Roy

    Jonny-Roy

    Joined:
    May 29, 2013
    Posts:
    666
    For the huge background images if they are just on the meun pages, put them in the resources and load / unload them, as lilboylost suggested, although, it sounds like you have pro, so I'd not worry yet and profile it, it's only worth worrying if it'll cause you a problem.
     
  9. yakm

    yakm

    Joined:
    Sep 12, 2014
    Posts:
    24
    For future reference i got this to work. In the inspector "1_boot_screen_partners" Texture type is : Texture

    Code (CSharp):
    1. //load in the texture and have reference
    2. Texture texture = Resources.Load<Texture>("Textures/UI/Logos/1_boot_screen_partners");
    3.  
    4. //create gameobject to hold rawimage which displays texture
    5. testTexture = new GameObject("test Texture");
    6.  
    7. //use rawimage to display texture
    8. RawImage rawTexture = testTexture.AddComponent<RawImage>();
    9.  
    10. //set loaded in texture to the rawimage
    11. rawTexture.texture = texture;
    12.  
    13. //parent and scale and position
    14. testTexture.transform.parent = mainCanvas.transform;
    15. testTexture.transform.localScale = new Vector3(1, 1, 1);
    16. testTexture.transform.localPosition = new Vector3(0, 0, 0);
    17. rawTexture.SetNativeSize();
     
    Last edited: Oct 2, 2014