How to Load Images Dynamically From A Particular Folder In unity C#

Hi All
I have facing a problem while loading images dynamically from a particular folder in unity
Following is a code which I am trying:-

EDIT: Code Modified

using UnityEngine; using System.Collections;

public class GenrateImages : MonoBehaviour { // This is the Path where my images are stored.I am Fetching the images from external folder. // I my project i have stored images in "D:UnityPractiseExamplesDynamicScrollBarAssetsResources". //public string imagePath = @"C:UsersPublicPicturesSample Pictures"; public string imagePath = "D:/UnityPractiseExamples/DynamicScrollBar/Assets/Resources/"; //public string imagePath; Texture2D[] typedTextures; int displayed = 0;

 // Use this for initialization
 void Start () {
     Object[] textures = Resources.LoadAll(imagePath, typeof(Texture2D));
     typedTextures = new Texture2D[textures.Length];
     for (int i = 0; i < textures.Length; i++){
         typedTextures _= (Texture2D)textures*;*_

}
Debug.Log("Textures Loaded: " + typedTextures.Length);
Debug.Log ("Image Path: " + imagePath);
}

// Update is called once per frame
void Update () {
if (typedTextures != null || typedTextures.Length > 0){
displayed++;
if (displayed >= typedTextures.Length){
displayed = 0;
}
}
}
void OnGUI(){
if (typedTextures != null && typedTextures.Length > 0){
//GUI.DrawTexture(new Rect(0,0,Screen.width, Screen.height), _typedTextures[_displayed], ScaleMode.StretchToFill, true);
GUI.DrawTexture(new Rect(20,20,200,200), typedTextures[displayed], ScaleMode.StretchToFill, true);
}
}

}
I above code doesn’t give any error but not running also
Please help asap.
Thanks You
Regards

The thing you have to beware of with Resources.Load is that does not in fact load images, or any other assets, from the original files. It loads them from a single file called resources.assets into which all assets are packed when your project is built. Unity will package all assets located in any folder called “Resources” into this file, but it will not load assets from anywhere else. If you want something to be loadable with Resources.Load, then the asset MUST be stored in a folder called Resources, or a subfolder thereof. See this page for more information:

This behaviour can be a little confusing at first because Resources.Load works with paths and filenames when you load data from it. So at a glance, it seems intuitive to expect it to be able to load resources from any path you give it. But it’s important to remember that the data is not loaded from files, and you do not have to copy the original source files into the folder when you build.

If you find you can’t load any resources, it’s probably because of any of these common mistakes:

  • The file is not stored in a folder called “Resources” in the editor.
  • You got the path wrong in the string you pass to Resources.Load. The path is relative to any Resources folder
  • You added extensions to the filenames you’re trying to load.
  • You used backslashes in the path. Resources.Load works with forward slashes ONLY.

If you want to load actual files outside Unity’s Resources package system, e.g. because you need to ship extra assets after building the app, then you need to look into AssetBundles, which provide that option.

apparently the problem could be found only here:

Object[] textures = Resources.LoadAll(_imagePath, typeof(Texture2D));

could you tell us precisely the value passed in _imagePath?

ok, just to be very clear what you’re trying to do is unusual - Unity expects all the assets used in your game to belong to the Assets folder you already used to know in your Unity projects, and if you want to load a texture dynamically as you wish, better be in a folder named Resources. I attached a demo scene to show you that this way it works painlessly but ,if you terribly need to load a texture outside your game assets, then you have to keep in mind that you can’t use the Resource object to do that but rather use some other technique - if any exists - but there I’m unable to help you.