Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Related-Sloth · Mar 18, 2016 at 12:51 PM · buildfolderwww classwww.loadimageintotexture

www image not loading from folder in Build (works in editor) - Using windows

Hi

Spent the last few hours searching for an answer but nothing has worked so far.

Essentially I am loading all files (jpgs) from a folder as textures which are then converted to a sprites and stored in an array. Then I am loading each sprite into a gameobject.

This is working perfectly fine within the editor however when I build and run I get the following error.

You are trying to load data from a www stream which had the following error when downloading. Couldn't open file /test.jpg

This occurs for every image and nothing loads.

 public class LoadImage : MonoBehaviour {
 
     //   public ArrayList imageBuffer = new ArrayList();
     public List<Texture2D> imageBuffer;
     public List<Sprite> loadedSprites;
     public GameObject textureTest;
     public GameObject testObject;
     string path = @"C:\Users\HWPC\Desktop\Test";
 
     void Start()
     {
         List<Sprite> everyone = new List<Sprite>();
         StartCoroutine("LoadImages");
     }
 
     IEnumerator LoadImages()
     {
 
         var info = new DirectoryInfo(path);
         var fileInfo = info.GetFiles();
         foreach (FileInfo file in fileInfo)
         {
             Debug.Log(file);
 
             string fullFilename = "file:///" + file.ToString();
             WWW www = new WWW(fullFilename);
            yield return www;
 
                 Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
                 try
                 { www.LoadImageIntoTexture(texTmp); }
                 catch { }
   
 
                 imageBuffer.Add(texTmp);
 
                 Sprite image = Sprite.Create(texTmp, new Rect(0, 0, texTmp.width, texTmp.height), new Vector2(0.5f, 0.5f), 100);
                 loadedSprites.Add(image);
                 testObject.GetComponent<Image>().sprite = image;
         }
     }
 }

Advice on possible causes would be greatly appreciated !

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Magius96 · Mar 18, 2016 at 04:50 PM

Why use WWW when your loading them locally?

 using System.IO;
 
 public class LoadImage : MonoBehavior
 {
     private Sprite LoadSpriteImage(string filepath)
     {
         if (File.Exists(filepath))
         {
             var photo = new Texture2D(4, 4);
             byte[] imageData = File.ReadAllBytes(filepath);
             photo.LoadImage(imageData);
             return Sprite.Create(photo, new Rect(0, 0, photo.width, photo.height), new Vector2(0.5f, 0.5f));
         }
         return null;
     }
 }

Just add this method to your class and call on it inside your LoadImages method to get the sprites.

Comment
Add comment · Show 4 · 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 Related-Sloth · Mar 18, 2016 at 10:14 PM 0
Share

Thanks $$anonymous$$agius, that's a much better way to do it. However I am still getting the same issue where the filepath is working in the editor but not in the build (File.Exists(filepath) is failing/false).

avatar image Related-Sloth · Mar 18, 2016 at 10:27 PM 0
Share

I worked out what the issue is. But have no idea why it is occurring.

Essentially in build fullFilename = file.ToString(); is only returning test.jpg, ins$$anonymous$$d of the entire path. So a fix is if I do fullFilename = path + file.ToString();ins$$anonymous$$d. However i'll need to make a workaround so it works in both editor and build.

avatar image Magius96 Related-Sloth · Mar 21, 2016 at 01:48 PM 0
Share

Ins$$anonymous$$d of using file.ToString() use file.FullName

FullName returns a string containing the path and filename.

avatar image saschandroid Related-Sloth · Mar 22, 2016 at 08:15 AM 0
Share

You could also use string[] files = Directory.GetFiles(path); to get the containing files with path as a string array.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Why is cloud build not ignoring hidden assets? 0 Answers

Is my project ruined? 1 Answer

Hide windows build folder 1 Answer

Resource folder doesn't build. Why is it empty? 1 Answer


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