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*);*
*}*
*```*
*<p>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?</p>*

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 < 27; i++)
    {
        string indexSuffix = "";
        float logIdx = Mathf.Log10(i+1);
        if (logIdx < 1.0)
            indexSuffix += "00";
        else if (logIdx < 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...

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?

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

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

        } 

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

	}
}

From Assets\Resources folder

    public static ArrayList textures = new ArrayList();
    for (int i = 0; i <= data.contours.Count; i++)
            {
                textures.Add(Resources.Load(""+i, typeof(Texture2D)));
            }

If you don’t want to use the WWW www from Unity, you can just use C#'s System.IO

public Dictionary<string, Sprite> AllSprites = new Dictionary<string, Sprite>(); //Store all Sprites created into this dictionary container

string artPath = "C:\\GameFolder\\Art\\";  //All the PNG's are in my "Art" folder and subfolders.

using System.IO; //This is what we use instead of Unity's WWW. Unity also has this stuff in UnityEngine.Windows.

void LoadAllSpritesFromPngFilesInFolderAndAllSubFolders()
{
    //Get all files PNG in "ART" directory
    string[] allFilePaths = Directory.GetFiles(artPath, "*.png", SearchOption.AllDirectories);
    //SearchOptions.AllDirectories is what gets you all subfolders too. Change this to not use subfolders

    //Loop through allFilePaths 
    foreach (string filePath in allFilePaths)
    {
        //Ready the PNG file from the harddrive
        byte[] newPngFileData;
        newPngFileData = File.ReadAllBytes(filePath); //Read the PNG file's bytes. This loads the PNG file into memory.

        //Create a Unity TEXTURE from PNG file
        Texture2D newTexture2D = new Texture2D(2, 2); //Create a new Texture. Size doesn't matter!
        newTexture2D.LoadImage(newPngFileData); //Load the PNG file into a Texture2D.       PngData ---> Texture2D

        //Create a Unity SPRITE from Texture
        Sprite newSprite = Sprite.Create(newTexture2D, new Rect(0, 0, newTexture2D.width, newTexture2D.height), new Vector2(0,0), 1);

        string spriteName = Path.GetFileNameWithoutExtension(filePath); //Get the filename of the png image             //example: "C:/GameFolder/Art/heroCharacter.png" ---> "heroCharacter"

        AllSprites.Add(spriteName, newSprite); //Add the Sprite to the AllSprites Dictionary.  Key is the filenameWithoutExtension. Value is the Sprite.
        //So if you later want to access the sprite, then you can simply write...
        //AllSprites[heroCharacter]; //This will give you the heroCharacter.png sprite!
    }

    Debug.Log("Finished Loading Sprites! Total Sprites Loaded: " + AllSprites.Count);
}

I also suggest this function for getting the Sprite from the dictionary.

public Sprite GetSprite(string imageName)
{
    //Debug.Log("Getting Sprite:'" + imageName + "'");

    if (!AllSprites.ContainsKey(imageName))
    {
        //Trim excess Space
        imageName = imageName.Trim();
        //Try Again
        if (!AllSprites.ContainsKey(imageName))
        {
            Debug.LogError("SPRITE NOT FOUND in AllSprites:[" + imageName + "]");
            return AllSprites["Error"];
        }
    }
    return AllSprites[imageName];
}