Answer the question
In order to leave comments, you need to log in
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());
}
}
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;
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question