Can anyone tell me if there is a async version of LoadImage() method? I found about Resources.LoadAsync, however the method doesnt work.
Thank you
Can anyone tell me if there is a async version of LoadImage() method? I found about Resources.LoadAsync, however the method doesnt work.
Thank you
You're on the right track in the comments above when you ask whether the WWW class could help. This is how I've solved the problem in the past for asynchronously loading local image files as Texture2D data:
Texture2D _content;
Material _material;
// Call this with StartCoroutine(LoadTexture(path));
IEnumerator LoadTexture(string path)
{
// Start the asynchronous load.
var www = new WWW("file://" + path);
// Let the game continue running until the texture has been loaded.
yield return www;
// Replace current content with loaded texture.
if (_content != null)
Destroy(_content);
_content = www.texture;
// Instantiate a copy of the material,
// and hang onto it so we can clean up when finished with it
if (_material == null)
_material = GetComponent<Renderer>().material;
_material.mainTexture = www.texture;
}
void OnDestroy()
{
// Clean up created instances
// (these aren't immediately garbage-collected otherwise)
if(_material != null)
Destroy(_material);
if(_content != null)
Destroy(_content);
}
I don't know how much load-balancing Unity does on its own, but it might be good to avoid kicking off all 6GB of load requests at once. ;) Try pacing them out, so you only have a few requests running at a time, to avoid any major hitches.
www.texture
is 100% synchronous and blocks the main thread for a significant length of time with large textures (enough to cause framerate stutter). However, I was using www for web requests, not local requests.
\$\endgroup\$
Commented
May 24, 2016 at 17:53
You can fire off a C# thread to load the image from a file, and get as far as turning it into a color array, with which you can quickly set a texture. So assuming you have some class AsyncImage
with a function loadMyImage()
which sets a Color[] pixels
member variable, you could do something like:
Thread t = new Thread(new ThreadStart(asyncImage.LoadMyImage));
t.Start();
Now in the update method of your script, or some coroutine that runs every few milliseconds:
if (asyncImage.pixels != null) {
myTexture2d.SetPixels(asyncImage.pixels);
}
You have probably heard that Unity can't do threading, but it is only the case that most Unity objects will not function outside of the main thread. You can still fire off as many threads as you want and do whatever things you want in them, as long as you don't use those forbidden unity methods/objects. This may mean you have to roll your own image loading code, or find some open source code. It is possible the standard mono libraries will have what you need. You can see a similar example in my source where I am generating an image procedurally in the background here: https://github.com/jackmott/solescape/blob/master/Assets/src/MainMenu/MainMenuManager.cs