- Home /
UnloadUnusedAssets
I have a script that load a 2048px texture from a Resources folder follow an index (FrameINT):
#pragma strict
var OBJ : GameObject;
var TextName : String = "";
var FrameINT : int ;
var FrameOLD : int;
var Tempo : float = 0.0;
var velocity : float = 0.05;
function Awake () {
OBJ.renderer.material.SetTexture("_MainTex", Resources.Load(TextName + Manager.SceneNumber.ToString()+ "_" + Manager.Frames.ToString(), Texture));
}
function Start () {
}
function Update () {
FrameOLD = FrameINT;
FrameINT = Manager.Frames;
if ( !Manager.FadeON && FrameOLD != FrameINT){
OBJ.renderer.material.SetTexture("_MainTex", Resources.Load(TextName + Manager.SceneNumber.ToString()+ "_" + FrameINT.ToString(), Texture));
}
if (Manager.FadeON){
AssignTexture();
}
}
function AssignTexture () {
OBJ.renderer.material.SetTexture("_MainTexture2", Resources.Load(TextName + Manager.SceneNumber.ToString() + "_" + FrameINT.ToString(), Texture));
if (Tempo < 1.0){
Tempo = Tempo + velocity;
OBJ.renderer.material.SetFloat("_Blend", Tempo);
}
else if (Tempo >= 1.0){
OBJ.renderer.material.SetTexture("_MainTex", Resources.Load(TextName + Manager.SceneNumber.ToString() + "_" + FrameINT.ToString(), Texture));
Tempo = 0.0;
OBJ.renderer.material.SetFloat("_Blend", Tempo);
Resources.UnloadUnusedAssets();
Manager.FadeON = false;
Manager.TouchON = true;
}
}
This system work great and give me possibility to load a large number of big size texture. At first, the application works fine, but with each new load gets too slow. I try to use :
Resources.UnloadUnusedAssets();
to unload unused assets but seem not to work. There is a right way to unload unused texture and speed up my application?
I have to ask why you are loading such a large texture for what seems to be an animation...? Are you sure you couldn't fake it another way?
I have a system like 360° viewer where there is a sequence of image that I control with swipe gesture. I'm a newbie programmer and seem the best solution...
Sorry for late reply. I thought maybe you were doing a fullscreen animation with it or something. You might be able to increase the throughput by using lower resolution "previews" while moving then swapping for a higher-resolution version when the camera "slows", or breaking them down into smaller images. Although it all depends on your (desired) implementation.
If you are using the free version of Unity, I think you will have to block either way though, since asynchronous loading/asset bundles is a pro feature.
Note that if a file is already loaded, Resources.Load should not take very long at all since the load is skipped and its just a lookup.
Note that depending on your target hardware/the total size of the view, it might be feasible to cache all the images while showing a loading screen.
Answer by Sisso · Sep 20, 2013 at 01:22 AM
The UnloadUnusedAssets and UnloadAsset is the right way to unload assets. For it work, you have to be sure that there is no references.
You said that it become slow. I don't think that could be the use of memory, use of memory don't make a game slow until you fill it and the SO start to paginate.
In your code there is many Resource.Load, and even inside a Update function. With "big size texture" it could really get things slow. The Resouces.Load is a very slow operation, and wich I know, there is no cache optimization from unity3d.
Try to profiling deep and see what is really slowing down. This could help:
Thanks for answer. I now that LoadResource is slow but for my project is perfect because i have a lot of highres texture. I am a newbie and would like to understand what you mean by references. $$anonymous$$y script is attached to plane and texture load every time i need. This mean I must delete the plane before unload resource? To conclude I think that the problems are elsewhere...
Reference is everything that point to your assets. If your plane is using a texture, it is a reference. For example, before clean up a texture a could remove the reference from my plane:
plane.GetComponent($$anonymous$$eshRenderer).shared$$anonymous$$aterials = []; Resources.UnloadUnusedAssets();
In your case you replace the texture before unloadall, so, the old texture should be cleanup.
Resources.UnloadUnusedAssets is also very slow. If you call each frame, you could expect slow downs.
You really need to profiling your code to know how many times and how much time the Resorces.Load and UnloadAssets are taking each frame.
Unity3d pro has some tools, you could give a try in 30 days trial.
What is the tools that unity Pro has to solve the problem? Thnaks @Sisso
That was in 2013, I was talking about the Profiler that was only available in Unity Pro.