x


Image files from folder with System.IO into array converted to WWW.texture and applied to Instantiated prefab

Here's what I'm trying to do:

I use http://system.IO to generate a list of files in an image folder. I then take the location of each file and replace some characters to create a well formed url for Unity's WWW class. I then instantiate a simple plane prefab for each file found and using WWW I grab the image file from the folder and assign it as a texture to the instanced plane prefab. What I want is for each prefab plane to have the appropriate image from the WWW request.

The following code almost accomplishes this but what happens is this:

It creates the correct number of prefabs - one for each file found in the folder. But instead of assigning a unique image to each prefab, it assigns the last file found in the parsed folder (say image004 of 4) to each prefab, except for the first prefab, which ends up with a missing material and no image.

Here is my code as it stands (messy but somewhat functional), any help will garner much respect and happiness on my part:

import http://System.IO;

var ImagesLoaded : boolean = false;
private var path : String = "C:/Users/home/Documents/_MarbleMoto/Images/";

var newImageTile = Resources.Load("ImageTile");
var arr = new Array ();

function Start() {
    CreateImageTiles();
}

function CreateImageTiles() {
    if(!ImagesLoaded) {
        var info = new DirectoryInfo(path);
        var fileInfo = info.GetFiles();

        for (file in fileInfo)
        {
        url = file.ToString();
        var myNewURL = "file://" + url.Replace("\\", "/");
        arr.Push (myNewURL);

            ImagesLoaded = true;
        }
    }

    if(ImagesLoaded) {
        for (var i=0;i<arr.length;i++)
            {
                print(arr[i]);
                var www = new WWW(arr[i]); 

                yield www; 

                Instantiate(newImageTile, Vector3(0, i * 3.0, 0), transform.rotation);
                //newImageTile.renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
                //www.LoadImageIntoTexture(newImageTile.renderer.material.mainTexture); 
                newImageTile.renderer.material.mainTexture = www.texture; 

            }
    }
}
more ▼

asked Sep 10 '10 at 01:16 AM

Julian Glenn gravatar image

Julian Glenn
2.6k 5 12 47

No loving at all? This is my "cherry popping" question too...

Sep 10 '10 at 03:55 PM Julian Glenn

Looks like you're trying to play a video with the free version of Unity?

Jan 10 '11 at 06:40 AM yoyo
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I assume you call CreateImageTiles again from somewhere else to get this to actually do something.

Here's what's happening:

for (var i=0;i<arr.length;i++) {
    var www = new WWW(arr[i]); //get the asset or whatever
    yield www;                 //come back when its done
    //instantiate prefab
    Instantiate(newImageTile, Vector3(0, i * 3.0, 0), transform.rotation);
    //change the prefab's material
    newImageTile.renderer.material.mainTexture = www.texture; 
}

You're changing the material on the prefab, not on the instance. Since you change the material on the prefab, all instances should change. Since the it didn't have a material to start with, that may be why the first one has a missing material.

Try something like:

for (var i=0;i<arr.length;i++) {
    var www : WWW = new WWW(arr[i]);
    yield www;
    //not sure what type your prefab is, but this'll probably do
    var tile : GameObject = Instantiate(newImageTile, 
                                        Vector3(0, i * 3.0, 0),
                                        transform.rotation);
    tile.renderer.material.mainTexture = www.texture; 
}
more ▼

answered Sep 10 '10 at 09:33 PM

skovacs1 gravatar image

skovacs1
10k 11 25 92

@Skovacs1 Sir you are a genius. I was totally blinded to the fact that I was changing the Prefabs material. Thanks a million :)

Sep 11 '10 at 01:14 PM Julian Glenn
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1726
x1396
x548
x370

asked: Sep 10 '10 at 01:16 AM

Seen: 1699 times

Last Updated: Sep 10 '10 at 01:16 AM