- Home /
Resources.Load ... async ? For large images.
Note .. for 2016+ Unity have indeed recently added LoadResourceAsync which can be relevant.
Essentially, is there a Resources.LoadAsync ... or has anyone come up with a similar solution?
When loading a large texture (a PNG) using Resources.Load, a game will hiccup for 1/2 second.
is there a solution?
I'm thinking, one might be able to use the Application.LoadLevelAdditiveAsync command in Unity,
load a small fake "level" that contains nothing other than the texture in question.
Any experience with this?
The only other idea I had was, cut the image into tiny pieces, small enough that unity does not stutter on a Resources.Load. Load each one with a gap of a couple frames between each, and paste together.
Any thoughts on loading large images on the fly with no stutter? Cheers
intriguing ... you can indeed stream it in with WWW using the Strea$$anonymous$$gAssets folder.
var thePath:String = System.IO.Path.Combine(
Application.strea$$anonymous$$gAssetsPath, "large.png");
Debug.Log("thePath is "+thePath);
var w:WWW;
w = new WWW( "file://" + thePath);
yield w;
tempTexture = w.texture; .. etc
BUT ... I have found that IT STILL HICCUPS. (For large images.)
I believe the problem is creating the openGL texture. No matter how well it streams in the PNG, you'll get a major hiccup when it makes the texture. There seems to be no solution for this.
Thanks as you guys exactly answered the specific question stated .. it's WWW to stream stuff in, use file:// and StreamignAssets folder.
Do you use w.texture directly, or do you create a Texture 2D from that? In case of the latter, you can try an uncompressed format (TextureFormat.ARGB32), they are much faster to generate than compressed textures. Not sure whether this is also true for iOS, though.
Wolf, I did try using w.texture directly, no difference.
I did use uncompressed formats and tried many.
It's really a shame that I can't find a way to simply stream a largish image from "disk" during gameplay.
After all, strea$$anonymous$$g audio (even enormous sounds) for sound effects etc during gameplay works perfectly in Unity, with no hiccup.
Indeed note that I tried it with images as small as 250.250 and it still hiccups.
A bit of a mystery / problem .....
thanks for your input!
Answer by Wolfram · Feb 04, 2013 at 11:30 AM
You will have better luck using the WWW class. It can load in the background, yield until it's finished, and then you have your image available, without, or maybe very little lag.
you can use file:// to retrieve local, as a matter of fact there was in bug in 4.0, fixed in 4.0.1 for doing just this.
It accepts any URL, therefore also local objects. In addition, it does not restrict you to your project folder (except maybe on $$anonymous$$obiles). We use it for example to load tons of videos on-demand from the User's Documents folder. This also reduces your build size, but you'll have to ship the image/video data separately.
Just don't use File:/// on your $$anonymous$$ac, it will crash the application ;-) (sorry, can't seem to find an English article about that).
Your answer
Follow this Question
Related Questions
When generating prefab, i cant load images from either resource or assetbundle 2 Answers
Displaying an image from a resource folder. 2 Answers
Resources does load up in the editor but not in build 0 Answers
Sphere with texture using C# scripting 1 Answer
Why does the behavour of my script change when it is built? 0 Answers