- Home /
preloading many images before displaying scene
I have to load lots of images using WWW to be used as textures for my scene.
I have tried doing it through awake() and start() but everytime the scene displays, the images appear one by one, maybe it is still downloading the images while the scene has already been displayed. What I want to do is to preload all the images, before starting to display the scene.
How do I go about this? Maybe displaying a loading screen before loading the game scene.
Maybe someone can give a sample javascript to do this. thanks!
UPDATE: Adding codes
I have implemented a loading scene to download all images but it still takes awhile before the textures show up in the scene. The "loading scene" has a script that downloads all images to be used in the games. The "game scene" has many groups of panels that each has a LoadImage script attached to it. LoadImage script access static array in ImageHolder that has been loaded before hand.
Loading Scene:
ImageHolder script attached to main camera:
function Start() {
GetInstance();
LoadImages();
}
private function GetInstance() {
if(!Instance) {
Instance = GameObject.FindObjectOfType(typeof(ImageHolder));
}
}
private function LoadImages() {
var panel_www = new WWW("web-service-url-for-all-images");
yield panel_www; //wait to finish
var parsed = JSONParse.JSONParse(panel_www.text);
var store_array = parsed["result"];
for(i=0; i<store_array.length; i++) {
img_url = store_array[i]["image"];
if(Instance.img_array[img_url] == null) { //if image has not been downloaded in cache
www = new WWW (img_url);
// Wait for download to complete
yield www;
Instance.img_array[img_url] = www.texture;
}
}
Application.LoadLevel(2);
}
Game scene:
LoadImage attached to group of planes.
function Start() {
yield StartCoroutine("loadStores");
}
function loadStores() {
//store images
var parts_img = new Array(17); //16 panels + 1 because index 0 is not used
var panel_www = new WWW("web-service-url-for-each-panelgroup");
yield panel_www; //wait to finish
var parsed = JSONParse.JSONParse(panel_www.text);
var store_array = parsed["result"];
for(i=0; i<store_array.length; i++) {
var part_number = parseInt(store_array[i]["part_number"]);
parts_img[part_number] = store_array[i]["image"];
}
//store image parts
var store_panel = transform.Find('Panel');
var name;
for (var child : Transform in store_panel)
{
name = parseInt(child.name);
if(parts_img[name] == null) {
continue;
}
img_url = parts_img[name];
// assign texture
child.renderer.material.mainTexture = ImageHolder.Instance.img_array[img_url];
}
}
The reason why this happens is that WWW returns immediately and does its work in the background. So if you want to wait for it, you'll need a yield for each WWW call/creation, or check .isDone. See http://unity3d.com/support/documentation/ScriptReference/WWW.html and of course Berenger's answer.
Answer by Berenger · Jun 04, 2012 at 09:02 PM
Display a loading screen (OnGUI, GUITexture, whatever), pause the game (Time.timeScale = 0) and then load the pictures. Inside a loop, create the WWW and yield it, until all the pictures are loaded. The hide the loading screen, reset timeScale to 1 and on you go.
I created a loading scene, and downloaded all images to be used while in this loading scene into a static class. Which is great. But, after calling LoadLevel to load the next scene, the textures in the planes (where the downloaded images are supposed to go) show up late. The scene is already show (including all characters) but the texture would only load up after a while. I am setting all textures in Start(). I have also tried moving it to Awake() and still no luck. If i'm not wrong, all Start() and Awake() methods must be finished first before calling the first Update() right?
By the way, the script that assigns the downloaded textures into planes are assigned to multiple objects. So they could all be running at the same time using just one static class that holds reference to the downloaded images.
If the texture are correctly downloaded, the should be displayed the very frame you assign them, or the one after tops. Something is going on here, could you show us some code ?
Your answer
