- Home /
How do i load entire folders into arrays?
I've seen this already asked but i've not seen any useful or working answer to my problem so i've gotta ask it in a little bit specific way.
I've got several folders, loaded with images. By now all those images in one of them have the same name and a number. the usual way now would be run through their numbers, but i've been asked to do it counting with they could have different names.
anyway, by now, this is my folder
From my script, i need to load an array with those images for each folder, which means i need to have 3 "catX" arrays with their respective images, which means i need to check what's inside "ImagenesGaleria", inside "Editor"
and i can't do it. I've tried with "Resources.loadAll" and nothing happened.
Answer by Rabwin · Dec 20, 2011 at 10:33 AM
why do you have your images stored under the folder Editor? I believe that is usually used by Unity for other purposes.
If you want to use Resources.Load, I believe you have to create a folder called Resources and place your images under that directory, for example "Resources/ImagenesGaleria/Catx"
Then you would use Resources.Loadall with that particular directory. OR If you know the names of the files, I guess you can use Resources.Load and manually add them to the array with a simple loop structure.
Edit: Making a change to add some code according to comments to try and explain how I would separate the "folders" for your system. This code is in C#. Showing you the struct I define, and the start function using the method I describe in the comments.
public struct NumAndImages
{
uint x;
Texture2D[] texArray;
public uint X
{
get
{
return x;
}
set
{
x = value;
}
}
public Texture2D[] aTexture
{
get
{
return texArray;
}
set
{
texArray = value;
}
}
}
void Start()
{
ArrayList textureArrays = new ArrayList();
bool noImage = false;
uint counter = 0;
while(!noImage)
{
NumAndImages NAI;
NAI.X = counter;
NAI.aTexture = Resources.LoadAll("ImagenesGaleria/Cat" + counter);
if(NAI.aTexture == null)
{
noImage = true;
print("Found up to Cat" + counter-1 + ". No more images to load");
}
else
{
textureArrays.Add(NAI);
}
}
//how many catx do I have
int howmany = textureArrays.Count;
//Load an image from the first catx
NumAndImages NAI = textureArrays[0];
Texture2D image = NAI.aTexture[0];
//load the last image in the last catx
NumAndImages NAI = textureArrays[textureArrays.Count];
Texture2D image = NAI.aTexture[NAI.aTexture.Length];
}
I've used Editor because my last try was with "AssetDatabase.LoadAllAssetsAtPath". I've just thought... i tried with "resources" yesterday... but i think i hadn't writen Shift r in "Resources"... let me try again :s i'll tell you shortly
allright, i'm such an ass... it was that f....ng "R" i wrote yesterday "r". so Resources.Loadall("ImagenesGaleria") really works. is there a way to separate all pictures from different subdirectories?
Is there any way of getting the path where those images were once you've loaded them?
I'm not sure how to do that, other than hard coding it in which is fine because when you build the executable, Unity will take care of that stuff. But the problem is that it seems like you have many files you want to store in arrays.
The reason I believe unity doesn't give you a directory for loaded images, is because when you make a build, all your resources (scripts, models, textures, etc) will get placed into a single file database. Have a look for yourself, I think it's called resources.assets.
Why would you need the directory anyway? If you're trying to get another object to load those textures/images, just pass the image you cached in the current object holding the array, it's much faster.
this project is some kind of image viewer mixed with a catalog using TUIO. Each folder "CatX" stores different images; it's a subcatalogue. several buttons are created according to folder number the main idea is that you hit the button created for Cat1 [which shows the first image in its array] and you have it loaded in the viewer. the viewer has next and previous buttons and you change through elements in the array. those images are shown in a pane [or thin cube] and you can use touchable surface to make it bigger and smaller.
Why not hardcode the directories to those resources then? Resources.Load doesn't throw an error if you try to load something that doens't exist, the object simply remains null. You can use a looping structure to check if an image was loaded in catx.
If you only have up to cat3 and tried to load an image from cat4(who doesn't exist), the texture array would have nothing loaded and would remain null, so you can use an if statement to check if the array is null, then ter$$anonymous$$ate the loop since theres no more images to grab.
Sorry, I'm finding it a bit hard to understand exactly what you are trying to do.
Edit: Okay I might understand now. $$anonymous$$aybe you can use a custom struct which contains 1. a string 2. a Texture2D[] (an array of Texture2D) Using a looping structure, you check which catx you are up to and create a new struct and set the string to whichever x you're up to (on second thought, it might be better to use an unsigned short to store the directory ins$$anonymous$$d, which uses less memory) This way, your struct contains an array of images from catx, and it also knows which catx those images came from. You will store all these structs in an ArrayList.
If this is what you wanted, let me know and i'll update my answer with some example code.
Answer by jahroy · Dec 20, 2011 at 05:23 PM
Check out both of these:
hey thanks a lot for your answer, I'm gonna try it now if it works, i'll let you know asap.
Your answer
Follow this Question
Related Questions
non-dynamic asset load? 1 Answer
load images in sequence into a gui? 2 Answers
Best way to speed up loading texture from file 0 Answers
Loading image for inventory from calculated path 0 Answers
Loading with ArrayPrefs 1 Answer