- Home /
Are Texture2Ds in a Resources folder always loaded in memory?
Hey Unity people,
I used this small script to print out all loaded Texture2D in my game and I noticed that some of them are loaded even if I'm 90% sure they shouldn't be. Here is the script:
var textures = Resources.FindObjectsOfTypeAll(typeof(Texture2D));
int totalBytes = 0;
foreach (Texture2D t in textures)
{
int memorySize = Profiler.GetRuntimeMemorySize(t);
if (memorySize > 0.4f)
Debug.Log("Texture object " + t.name + "(" + t.width + "x" + t.height + "|" + t.format + ") using: " + memorySize + "Bytes", t);
totalBytes += memorySize;
}
Debug.Log("Total bytes: " + totalBytes);
I tried Resources.UnloadUnusedAssets to unload those assets but that doesn't seem to make any difference.
My main question is: do Texture2Ds automatically load into memory when they are placed in a Resources folder whether you use them or not?
Side questions: How can I tell why is that Texture loaded in memory? Is somebody referencing it? How can I search the scene for the game object that references it?
Thanks in advance.
Answer by whydoidoit · May 09, 2013 at 06:12 PM
No textures in Resources are not always loaded into memory (they are always included in the build). However as they are always accessible to a Resources.FindObjectsOfTypeAll then they will be loaded into memory when that code runs.
You can tell what uses something in the editor by running a scene, finding the texture in the project, right clicking and use Find Scene References.
Erm..... So basically, by just running that code, I load all textures in memory? Haha! That's good to know. I did run the Find Scene References but wasn't finding anything referencing what my code was saying. But I guess now it makes sense why. Thanks!
Your answer
Follow this Question
Related Questions
Texture and Texture2D for GUI (simple question) 1 Answer
Texture Doubled in size in RAM Profiler 3 Answers
Releasing assets loaded from asset bundle 1 Answer
Quality level and cubemaps 1 Answer