Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Julian-Glenn · Sep 10, 2010 at 01:16 AM · instantiatearraytexture2dwww

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 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 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; 

         }
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julian-Glenn · Sep 10, 2010 at 03:55 PM 0
Share

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

avatar image yoyo · Jan 10, 2011 at 06:40 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by skovacs1 · Sep 10, 2010 at 09:33 PM

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; 
}
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julian-Glenn · Sep 11, 2010 at 01:14 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

What is the best way to convert a string list to a class list? 1 Answer

Spawn from array 2 Answers

How do you download image to UI.Image? 4 Answers

Texture2D Array Appears Grainy 0 Answers

Retrieve Array of Texture2Ds dropped in EditorWindow ObjectField 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges