Hi,
I am making a project which has to load series of images of a system folder (C:/BLA_BLA/Image_Series/).
In total there are about 500 images of 1400x500 aprox.
At first I tried using Resources.Load() and it worked very find, but in the release I can't use it because the images wouldn't be in the folder Resources... I also have tried using WWW but using it the images use a lot of memory, my code using this method is:
Loading the 3 same textures the 2 methods work:
Using Resources.Load() -> 1 MB used textures and 10.3-11.4 MB VRAM usage.
Using WWW -> 24 MB used textures and 10.3-34.4 MB VRAM usage.
Which is the cause of that difference? Do you have some ideas to reduce the used memory using WWW ? There is another, and best, way to load images?
when you import images in unity weather it's in resources folder or any other folder, the image will be compressed to a format (dxt most of the times) and then stored so they take less memory. using www you can only load jpg, png and i think bitmap images. you can compress them by hand yourself to reduce the memory or just compress the textures when you create them at runtime but it takes time so it's not recommended to do at runtime.
to create textures at runtime with the format that you want, you should load the texture with www("file::path") and then create another texture with the format that you want to do some getpixels/setpixels so it will take a huge time for 500 big textures.
the best approach is to compress your png/jpeg images then create a texture2D in DXT11 or DXT5 format then load the newly donwloaded texture into it with WWW.LoadImageIntoTexture.
in this way the texture will be compressed to DXT1 or 5 based on it's format. if you don't need a texture, don't forget to Destroy it.
i found this info in documentation of LoadImageIntoTexture method. it says
This function replaces texture contents with downloaded image data, so texture size and format might change. JPG files are loaded into RGB24 format, PNG files are loaded into ARGB32 format. If texture format before calling LoadImage is DXT1 or DXT5, then the loaded image will be DXT-compressed (into DXT1 for JPG images and DXT5 for PNG images).
the method that you used (using texture property of the www class) don't compress textures. also keep in mind that using none power of two textures will slow down the process but using this method you will be ok.
enjoy the awesome unity in any type of application.
Been beating my head against a wall all day and went through a lot of different posts, this one helped the most (thanks Ashkan for the direction), worked for me. Here's a quick demo-code I put together to test out and make sure the functionality worked.
In my script I manually put in a prefab for t_static_tx and loaded t_static_tx from a file (for comparison):
public Texture2D t_static_tx = null;
public Texture2D t_dynamic_tx = null;
public WWW t_load = null;
void OnGUI(){
GUI.Label(new Rect(100, 200, 64, 64), "TESTING");
GUI.Label(new Rect(164, 200, 64, 64), t_static_tx);
if (t_load == null)
{
Debug.Log("Application data path: " + Application.dataPath);
string targetFile = "file://" + Application.dataPath + "/Resources/Textures/Icons/Chairs_64x64.png";
Debug.Log("Beginning load at time: " + Time.time);
t_load = new WWW(targetFile);
}
else if (t_load.isDone && t_dynamic_tx == null)
{
Debug.Log("File has finished being loaded at " + Time.time);
t_dynamic_tx = new Texture2D(64, 64);
Debug.Log("Preparing to load PNG into Texture");
t_load.LoadImageIntoTexture(t_dynamic_tx);
Debug.Log("Loaded image into texture");
}
else
{
GUI.Label(new Rect(164, 264, 64, 64), t_dynamic_tx);
}
}
As far as I know Unity puts the compression on bitmaps, so that is the reason.
Compression is obviously happening in the process of building, it is obviously too demanding for real-time job.
Have you tried Asset Bundles?
To solve this problem and create a compressed texture I've created it using:
Texture2D t = new Texture2D(2, 2);
www.LoadImageIntoTexture(t);
t.Compress(true);
Another important question is that t.Compress only works if I use textures in resolution 2^x * 2^x (for example 512x512), so I have to scale all the textures on a power of two resolution.
Anybody knows a method to Compress all kind of resolutions?