R
R
ReWire_922017-08-29 09:48:14
Unity
ReWire_92, 2017-08-29 09:48:14

Do I need to unload resources loaded by Resources.Load()? Is it a costly operation?

There is a canvas, on it Image. Sprites from the Resources folder are loaded into it from the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SpriteController : MonoBehaviour {

  public int level = 0;
  public Image Picture; 

  void Start () {
    Picture.sprite = Resources.Load<Sprite>(level.ToString());
  }

  void NextLevel(){
    level++;
    Picture.sprite = Resources.Load<Sprite>(level.ToString());
  }
}

There are about 50 sprites in the folder. The NextLevel function is called quite often. Do I understand correctly that when moving to the next level (level ++) and loading the next sprites, all previously loaded ones are not deleted and continue to hang in memory, taking up space?
If so, should they be unloaded every time a new sprite is loaded, given that the previous sprites in the game will definitely no longer be used? (Sprites - images weighing 1.5 MB each, 800x600) Is the Resources.UnloadUnusedAssets() method not suitable for unloading? As I understand it, it unloads only assets? How then correctly to unload my sprites? Thus?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SpriteController : MonoBehaviour {

  public int level = 0;
  public Image Picture;
  Sprite mySprite;

  void Start () {
    mySprite = Resources.Load<Sprite>(level.ToString());
    Picture.sprite = mySprite;
  }

  void NextLevel(){
    level++;
    Destroy (mySprite);
    mySprite = Resources.Load<Sprite>(level.ToString());
    Picture.sprite = mySprite;
  }
}

Am I understanding everything correctly or is there something wrong / can it be implemented better?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Basmanov, 2017-08-29
@ReWire_92

Loaded sprites are not deleted if you are sitting on the same scene, if you change the scene, then everything should be unloaded automatically, provided that you do not have a link left in any singleton. UnloadUnusedAssets can help in a situation where you have loaded a bunch of different resources, used them, then nulled all references and want to clean everything up. If these are single large images, then it is better to unload them more explicitly via Resources.UnloadAsset . What you have loaded from resources should be passed to this method, for example, if you loaded a texture and created a sprite from it using Sprite.Create, then you need to transfer the texture, not the sprite. Destroy needs to be handled carefully, it can also free up memory, but in some situations it can delete an object so that you can’t reload it without restarting the application. I also strongly recommend testing such code in isolation on a separate scene on the target device; you cannot check the correctness of unloading in the editor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question