Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Image files from folder System.IO into texture array etc...

Discussion in 'Scripting' started by Julian-Glenn, Sep 10, 2010.

  1. Julian-Glenn

    Julian-Glenn

    Joined:
    Jul 23, 2010
    Posts:
    97
    Here's what I'm trying to do:

    I use 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:


    Code (csharp):
    1. import System.IO;
    2.    
    3.     var ImagesLoaded : boolean = false;
    4.     private var path : String = "C:/Users/home/Documents/_MarbleMoto/Images/";
    5.    
    6.     var newImageTile = Resources.Load("ImageTile");
    7.     var arr = new Array ();
    8.    
    9.     function Start() {
    10.         CreateImageTiles();
    11.     }
    12.    
    13.     function CreateImageTiles() {
    14.         if(!ImagesLoaded) {
    15.             var info = new DirectoryInfo(path);
    16.             var fileInfo = info.GetFiles();
    17.    
    18.             for (file in fileInfo)
    19.             {
    20.             url = file.ToString();
    21.             var myNewURL = "file://" + url.Replace("\\", "/");
    22.             arr.Push (myNewURL);
    23.    
    24.                 ImagesLoaded = true;
    25.             }
    26.         }
    27.    
    28.         if(ImagesLoaded) {
    29.             for (var i=0;i<arr.length;i++)
    30.                 {
    31.                     print(arr[i]);
    32.                     var www = new WWW(arr[i]);
    33.                    
    34.                     yield www;
    35.                    
    36.                     Instantiate(newImageTile, Vector3(0, i * 3.0, 0), transform.rotation);
    37.                     //newImageTile.renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
    38.                     //www.LoadImageIntoTexture(newImageTile.renderer.material.mainTexture);
    39.                     newImageTile.renderer.material.mainTexture = [url]www.texture;[/url]
    40.    
    41.                 }
    42.         }
    43.     }
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Does it work if you take out the "yield WWW"?
     
  3. Julian-Glenn

    Julian-Glenn

    Joined:
    Jul 23, 2010
    Posts:
    97
    Nope, does the same thing - all prefabs have the same texture/image except one which is blank.