Unity - Texture2D.Loadimage resulting in MemoryWarning - unity3d

I currently work on a 2D project on Unity (for mobiles/tablets), where I download a few heavy pictures by using UnityWebRequestTexture.GetTexture(url), which I then store using File.WriteAllBytes(filePath, webRequest.downloadHandler.data).
Later in the project, I need to show the downloaded picture on a RawImage element. To avoid blocking the app I read the bytes via FileStream on a separate Thread, which works well.
But when I call (from the main thread) ((Texture2D) myRawImage.texture).LoadImage(loadedBytes), my Profiler shows a huge peak, and if most devices handle it correctly, on iPad Mini I often get a MemoryWarning at this very point. Which, when repeated, leads to the app being forced close by iOS.
For info, the downloaded image's size vary depending on the device asking it. We initially intended to display 2048x1536 pictures that way on iPad Mini, but we were getting MemoryWarning on each display. We're now using 750x1334, but we still get MemoryWarning from time to time.
My question then is : is there a better way to do the "LoadImage" step ? I believe that the conversion from byte[] to Texture2D is where my memory problem lies, but I didn't find any workaround to this.
Or eventually: Is there a better way to download/store the image? If the image was in the app's resources from the beginning, I don't seem to have any trouble displaying it on the same device. Maybe there is a way to download the texture while applying it the same compression algorithm that assets receive when building an app ?

Using Texture2D.Compress() after loading the texture compresses the texture by at least 90%.
Reference

Related

Handle too many textures Unity

At present one of my quiz game require, many images of flowers based on questions asked.
I can say total 250+ images with resolution of 512x435 each.
Plus other games textures get loaded at a time.
So when game screen get opened which showing all these images, its on the spot get crashed.
I am testing this on iPhone devices. So how to handle these many textures? I was stuck in this point.
Here is overview of flowers textures.
I was displaying all these in grid view so in one scrollable screen all get displayed.
Now I hope, you understand my side point of view.
You need to reduce the amount of these images that are loaded at once, try keeping most of those images rendering components disabled using some sort of managing script when you are not seeing them, rather than simply putting them off camera and try to moderate the amount that are rendered at once. If this does not work the problem might be that the total size of the images is bigger then the amount of ram your device has. Even the IPhone X has only 3GB of ram, check if your images in total are bigger than or close to the 1GB-3GB range, unity remote 5 is probably storing these images in ram or temporary memory. It is always a good idea to try to compress your images when working with a mobile device anyway, try to put them into a texture atlas or lower the quality of them until the iphone can handle it, you should never rely on mobile devices to render tons of images at once.
Wow. That's a lot of flowers....
Okay if you need all of those textures, and you're only displaying them in the editor, I'd group them into folders.
Even if you sort the blue from the red..... etc just human readable directories it will help out a lot as you won't load ALL of your images at once.
If you are loading all of those textures at runtime, which I don't think you would as unity optimizes your built executable, I would recommend against loading all of the images at once because you will use a lot of RAM, especially if those images are 512px or larger.
With in the application you could split them into 25 at a time on the screen and you could swipe to the right to go to the next 25 or swipe back to the previous. This is how i solved a similar problem in unity a while back.

Animations Lagging When Publishing from Flash CS6 To iPhone 4

I recently created an app in flash cs6 to be used on my iPhone 4.
The app doesnt need to work through the app store its just a tech demo but when i put the app on my device all the animations become really slow/choppy.
My iPhones up to date and im using air3.2 (i did try updating to air 3.7 but then my application just became a white screen)
I have also tried cacheing all the movie clips as bitmaps bit it doesnt seem to make a difference.
(the app works fine inside the flash simulator)
please help?!
There could be a few reasons why this is happening,as I am taking a punt here as you havent gave much information on what your doing.
but your frame rate may have effect.. how are you creating your tweens?timeline animation or tween scripts.I found tween scripting works better as its distance over time opposed to distance over frames.
Also depending on your animation, images sizes, event listeners etc..
You need to take inconsideration your device itself and the resources it has available, free memory or actual storage available.
The reason it will work freely on your PC would be due to the fact of less limitations, your pc has more to computes and resources to throw at your application .
Add this line to your code to check your memory:
import flash.filesystem.File;
this.addEventListener(Event.ENTER_FRAME,performMemTest);
function performMemTest(e:Event):void {
trace(System.totalMemory);
}
I have read anywhere around 14MB++you may experience problems.

Images showing in simulator but not on the iPhone device

So when I run the application from the device the pictures show up and everything works great. However, when I move to the device I run in about 10 out of 38 pictures that don't show up. I am pulling the names for the images from an sqlite database and I already checked and the names are correct, case and everything. I checked the bundle and the images are correctly in there.
Does memory play into effect in this? I am not really sure what else could cause this to happen??
Thanks!
Solution:
The files somehow were not saved properly and were unable to be opened by say photoshop or paint even. So with the files not being able to be open they weren't showing up... Thanks for the help everyone!
First thing to check is the case of the strings you're using to refer to the resources. The iPhone is case sensitive, but the simulator is not.
Oops, just seen that you checked the case. Better log all your UIImage creation calls then!
The simulator accepts a wider range of image formats. Sometimes you had a specific file type that the device cannot display. Double check the 10 files looking for some difference from the others ones.
Yes, memory absolutely plays into this. Check your UIImage creation calls and make sure they dont return empty. Also, check to see if youre getting memory warnings in the console window.
Generally, if in-memory size was the culprit, your application would crash with a low memory error on the device. However, it seems like the images just don't display.
One other thing that can be going wrong is the size of the images. UIViews on the iPhone can only have dimensions smaller than the maximum texture size supported by the GPU. Apple states that this is 1024 x 1024, but I've found it to be more like 2048 x 2048 on even the original model iPhone. You may be creating a view for some of these images that exceeds this size in one dimension, but your Mac has a larger maximum texture size on its GPU and so it displays fine in the Simulator.
Apparently the iPhone caches the launch screens. So in my case, I had to reboot my iPhone before being able to see the image.

Big loading times in iphone app

I'm making an openGL game for iPod/iPhone.
At start I load at once all the textures I need. At first loading times where small, but as I kept developing and adding new textures the loading times have been increasing, to the point of taking many seconds before the game start.
Recently a new problem appeared, when I build the game in the device It takes too long and the game quits. At the app is installed correctly and i can test It, but never while being connected to xcode. Sometimes even the app quits, when too many elements are dran on screen.
Right now I use 6 files , with about 2 Mbs of size in total.
Is there a form to create a loading screenor the such ?
What other meassures can I take so solve this issues ?
If you're decoding PNG files at startup using Core Graphics, I would suggest using PVRTexTool to create PVR data files instead. The contents of PVR files can be uploaded directly to OpenGL; no need to use Core Graphics to decode them.
PVRTexTool can also do neat stuff like generate mipmaps (another thing you might want to avoid at startup time) and encode to compressed formats (reducing your texture size will help too).
Besides encoding your textures as PVR-textures there are a few more solutions.
One is to defer texture loading to a later point. Let your app bring up its user interface and maybe show a progress bar to the user while you're loading your textures. This will stop iPhoneOS from killing your app.
You'll probably also need to look into what kind of textures you are creating. Some formats are much more expensive than others to create from you png:s.
As a last resort you could save your textures as uncompressed raw textures. This will make your app larger but cut down loading time.

iPhone OpenGLES: Textures are consuming too much memory and the program crashes with signal "0"

I am not sure what the problem is. My app is running fine on the simulator but when I try to run it on the iPhone it crashes during debugging or without debugging with signal "0". I am using the Texture2D.m and OpenGLES2DView.m from the examples provided by Apple. I profiled the app on the iPhone with Instruments using the Memory tracer from the Library and when the app died the final memory consumed was about 60Mb real and 90+Mb virtual. Is there some other problem or is the iPhone just killing the application because it has consumed too much memory? If you need any information please state it and I will try to provide it. I am creating thousands of textures at load time which is why the memory consumption is so high. Really cant do anything about reducing the number of pics being loaded. I was running before on just UIImage but it was giving me really low frame rates. I read on this site that I should use OpenGLES for higher frame rates.
Also sub question is there any way not to use UIImage to load the png file and then use the Texture class provided to create the texture for OpenGLES functions to use it for drawing? Is there some function in OpenGLES which will create a texture straight from a png file?
thousands of textures? really? how many of them are on the screen at one time? perhaps you can only load some of them at a time, or if they're small, you should combine them into fewer larger textures.
the general guideline I've heard is that you are limited to 24MB of texture memory.
there's nothing built into OpenGLES that loads from disk, but you can use a file parser like stb_image to do it yourself.
I tried load as ten texture pieces of 2048x2048 pixels.
Texture memory exceed 24MB, but iPhone3GS is able to loaded and rendered it.
I also recommend stb_image or SOIL texture loader.
(stb_image library is used SOIL library.)