- Home /
Load textures from external path, without change their names.
I've read some post about load texture using WWW(url), like this one:
http://answers.unity3d.com/questions/813495/can-i-load-textures-outside-the-resources-folder.html.
I was wondering if it is possible to load the textures/images without change their names(the post suggest to use a sequencial names) ? If there any function that can read how many images are in the external path and then create an array or list?
Thanks all
Answer by Pawscal · Nov 21, 2014 at 05:44 PM
There is a coupple of solutions availaible to you if you want to load images dynamically whatever the name of the images are.
We have done this on many of our projects.
The easiest way would be to use something like this:
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System.Collections;
public class MyClass : MonoBehaviour {
public string _imageFolderPath = @"C:\My\Image\Path";
List<Texture2D> _projectImages = new List<Texture2D>();
IEnumerator BatchLoadImages()
{
if(Directory.Exists(_imageFolderPath))
{
// Make sure your list is empty your list
_projectImages.Clear();
//get path for all .jpg files in directory and subDirectories.
string[] dirPaths = Directory.GetFiles(_imageFolderPath,"*.jpg" ,SearchOption.AllDirectories);
if(dirPaths.Length>0)
{
//load each images
foreach (string path in dirPaths)
{
string url = "";
// look if the path is an url or a local path
if(path.Contains("http:"))
{
url = path;
}
else
{
url = "file://" + path;
}
// Create an empty texture
Texture2D newTexture = new Texture2D (512,512,TextureFormat.ARGB32,true);
// loads the image
WWW www = new WWW (url);
yield return www;
// Load it into a Texture2D
www.LoadImageIntoTexture (newTexture);
//add texture to the list.
_projectImages.Add(newTexture);
}
}
}
else
{
Debug.LogWarning("The Image loading path does not exist");
}
}
}
this would return an array of texture2D you could then use to suits your needs.
If you need something more robust with more infos that just an image path, you can use a data base that generates an XML file that will hold all the info you need about each images you want to load , parse it in Unity and use the data to load your images.
Hope it helps, tell me if you need more details
Your answer
Follow this Question
Related Questions
Exception: not implemented? 2 Answers
Assigning Texture using WWW into Texture Array? 1 Answer
Image loaded with LoadImageIntoTexture gets inverted 0 Answers
Host Image/Texture using Unity server, then request it using WWW on client 0 Answers
Using WWW in public method of a GameObject in Unity C# 1 Answer