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 drhenry · Aug 17, 2010 at 10:21 PM · loadimages

How to load images from given folder?

Hi, I'd like the application to load all images from a given folder (a subfolder of the installation directory) at the beginning. The reason why I don't use assets is that it should be possible to exchange the images after installation. I've tried this piece of code so far in C#:

public ArrayList imageBuffer = new ArrayList();

private void LoadImages() { //load all Texture2D files in Resources/Images folder Object[] textures = Resources.LoadAll("Images"); for (int i=0; i < textures.Length; i++) imageBuffer.Add(textures[i]); }

Will the resources folder be copied to the installation directory during installation? Or is this not the right way if the contents of the image folder should be exchangeable after installation? How to do it right?

Comment
Add comment · Show 1
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 CarterG81 · Sep 04, 2019 at 11:51 AM 0
Share

I added an answer (probably on 2nd page) if you don't want to use WWW and ins$$anonymous$$d prefer System.IO.

7 Replies

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

Answer by drhenry · Aug 21, 2010 at 11:37 AM

For all those, who'd like to load images (in my example "chessboard001.jpg",..., "chessboard027.jpg" from a given folder):

private void LoadImages() { string pathPrefix = @"file://"; string pathImageAssets = @"C:\HistoryCube_Assets\"; string pathSmall = @"small\"; string filename = @"chessboard"; string fileSuffix = @".jpg";

 //create filename index suffix "001",...,"027" (could be "999" either)
 for (int i=0; i &lt; 27; i++)
 {
     string indexSuffix = "";
     float logIdx = Mathf.Log10(i+1);
     if (logIdx &lt; 1.0)
         indexSuffix += "00";
     else if (logIdx &lt; 2.0)
         indexSuffix += "0";
     indexSuffix += (i+1);

     string fullFilename = pathPrefix + pathImageAssets + pathSmall + filename + indexSuffix + fileSuffix;

     WWW www = new WWW(fullFilename);
     Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
     //LoadImageIntoTexture compresses JPGs by DXT1 and PNGs by DXT5     
     www.LoadImageIntoTexture(texTmp);

     imageBuffer.Add(texTmp);
 }

where imageBuffer is of type ArrayList. This function blocks code execution until all images are loaded.

If you want to continue code execution while images are loading, call

StartCoroutine(LoadImages());

LoadImages must then be declared as:

private IEnumerator LoadImages() { ...

             WWW www = new WWW(fullFilename);
             yield return www;
             Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);
             //LoadImageIntoTexture compresses JPGs by DXT1 and PNGs by DXT5     
             www.LoadImageIntoTexture(texTmp);

             imageBuffer.Add(texTmp);

... }

Hope it helps someone...

Comment
Add comment · Show 7 · 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 · Aug 21, 2010 at 11:43 AM 0
Share

$$anonymous$$ark this answer as accepted, if it works for you, so it's bumped to the top. I just read through the previous two, and went off to investigate before seeing that you'd already solved it! :(

avatar image drhenry · Aug 21, 2010 at 01:34 PM 0
Share

$$anonymous$$arking it as the accepted answer doesn't bump it upwards, sorry. I think it must be uprated by other users to climb upwards...

avatar image horatiu665 · Jan 31, 2014 at 06:26 PM 2
Share

Noob question: Why do you use the character @ at string declarations? I couldn't find anything online due to the nature of the symbol...

I noticed the strings do not use escape characters if I put @ in front. Is that it...?

avatar image TooManySugar horatiu665 · Jun 25, 2018 at 01:55 PM 0
Share

I found the answer, is the scape thing. https://stackoverflow.com/questions/17456686/cs1009-unrecognized-escape-sequence

avatar image zombience · Apr 16, 2014 at 05:21 PM 0
Share

also curious about the @ include

avatar image vintproykt · Jun 19, 2014 at 09:37 AM 0
Share

I think it's "safe" string declaration. You see that nothing is escaped in them. Every normal string instantly must have only usual and escaped characters but this.

Show more comments
avatar image
2

Answer by rdockterjr1 · Oct 02, 2013 at 02:35 PM

This will load all jpeg photos in a single folder as textures This uses the System.IO.Directory.GetFiles in C sharp

Hope this helps

 public class PhotoViewer : MonoBehaviour {
     
     GameObject[] gameObj;
     Texture2D[] textList;
     
     string[] files;
     string pathPreFix; 
     
     // Use this for initialization
     void Start () {
         //Change this to change pictures folder
         string path =    @"C:\Users\Public\Pictures\Sample Pictures\";
         
         pathPreFix = @"file://";
 
         files = System.IO.Directory.GetFiles(path, "*.jpg");
         
         gameObj= GameObject.FindGameObjectsWithTag("Pics");
         
         StartCoroutine(LoadImages());
     }
     
 
     void Update () {
         
     }
     
     private IEnumerator LoadImages(){
         //load all images in default folder as textures and apply dynamically to plane game objects.
         //6 pictures per page
         textList = new Texture2D[files.Length];
         
         int dummy = 0;
         foreach(string tstring in files){
             
                 string pathTemp = pathPreFix + tstring;
             WWW www = new WWW(pathTemp);
                     yield return www;
                     Texture2D texTmp = new Texture2D(1024, 1024, TextureFormat.DXT1, false);  
                     www.LoadImageIntoTexture(texTmp);
      
                     textList[dummy] = texTmp;
             
             gameObj[dummy].renderer.material.SetTexture("_MainTex", texTmp);
             dummy++;
         }
 
     }
 }
 
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 switchfoot · Jun 01, 2019 at 07:54 PM 0
Share

Thanks! Exactly what i was looking for and easy to use!

avatar image
0

Answer by drhenry · Aug 20, 2010 at 03:43 PM

Sorry, I just didn't want people to forget about my issue, so I bumped ;) And this time I need the "code snippet" functionality, which isn't available in comments...

I don't get around this www stuff...and how to wait at program start for the image to be loaded completely...Can anyone explain it using C#?

In Start() I call LoadImages() which shall load all images in a given folder (C:\Assets\Images). But even if I write just

private IEnumerator LoadImages()
{ 
    print("yeah!");
    print("image loaded");
    yield return 0;
}

nothing is ever printed to the console. Why?

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 drhenry · Aug 20, 2010 at 03:59 PM 0
Share

Ah, in Start I have to call StartCoRoutine(LoadImages)... But this will continue code execution although not all images might have been loaded yet. How can I wait in Start until LoadImages is finished?

avatar image matyicsapo · Aug 20, 2010 at 04:00 PM 0
Share

use Debug.Log() ins$$anonymous$$d of print(), also make sure you call the coroutine using StartCoroutine()

avatar image matyicsapo · Aug 20, 2010 at 04:01 PM 2
Share

not making it a coroutine would be the easiest way

avatar image drhenry · Aug 21, 2010 at 11:21 AM 0
Share

sigh sometimes I'm so dumb...of course without the yield statement (as in all Unity examples) I won't need a coroutine anymore. That's it! Thanks.

avatar image
0

Answer by Kalu · Feb 13, 2012 at 11:00 AM

dont know if it could be help someone but in C# =>

 if(cur_image_loaded != null){
     GUI.DrawTexture(new Rect(vignette_pos_x-203, vignette_pos_y-30, 256f, 256f), cur_image_loaded, ScaleMode.ScaleToFit, true);
 }
 StartCoroutine(load_image_preview(image_to_load));
 
 private IEnumerator load_image_preview(string _path)
 {
     WWW www = new WWW(_path);
     yield return www;
     Texture2D texTmp = new Texture2D(256, 256, TextureFormat.RGB24, false);
 
     www.LoadImageIntoTexture(texTmp);
     cur_image_loaded = new Texture2D(256, 256, TextureFormat.RGB24, false);
     cur_image_loaded = texTmp;
 }
Comment
Add comment · 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
0

Answer by Amelia · Apr 25, 2013 at 09:18 AM

Folllowing is my sample code in image loading from a file folder in c#, the code work very well.

 using System;
 using RasterEdge.Imaging.Basic.Core;
 using RasterEdge.Imaging.Basic;
 public static REImage OpenImageFile(string filePath);
 public static void LoadImageFromFileDemo()
         {
             string fileName = "c:/Sample.png";
 
             REImage reImage = REFile.OpenImageFile(fileName);
 
         } 


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 whydoidoit · Apr 25, 2013 at 09:19 AM 1
Share

Um, I guess we'd need to know what RasterEdge was though?

  • 1
  • 2
  • ›

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fatal error after loading image and drawing texture 1 Answer

Error while loading image from Windows Phone gallery 1 Answer

How to load a very large image? 0 Answers

how to load images from hard disk to gui buttons? 1 Answer

how to load images automatically from user's hard disk? 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