First post here - I’m quite new to Unity and JavaScript in general.
What I’m trying to do - In short, I’d like create an array of textures (100+), loaded from resources folder, based on file name:
Have a list of image file names in text file, in Assets folder
Load the list of file names into Unity, split it into array of name strings
Have a bunch of images, corresponding to list file names, current plan to have them in Resources folder
Load each image corresponding to name as texture from Resources folder
Put loaded texture into array
Assign these textures one at time to objects
Step 5 is the problem, other steps I already verified by doing Resource.Load for single texture, and assigned it to model OK.
So my problem is:
I can’t seem to be able to instantiate textures to be unique textures, array will only contain last texture of for loop.
Seems like I only have made reference of some sort?
Is it actually possible to do this? There seems to just be GetPixel and create new Texture2D, but it doesn’t seem to have way to add image as source?
Sorry for noobish question, at this point of learning, I’m definitely not sure if this is even usable way to do this, maybe my code has some logic errors too.
I went through pile of Answers/forum links and tried to google answers, but didn’t quite come up with solutions
Is this actually doable? Can I instantiate somehow Texture2D objects?
I tried Instantiate, but it says that read-only texture can’t be instantiated
I have 100+ files to load, so I opted for this alternative instead of manually assigning each file in inspector
I might change to mesh objects, then it would be easier get them into Unity using Resources.Load, I think
Any help appreciated, thanks!
Here’s my code:
#pragma strict
import System.IO;
var iconMesh: GameObject;
var iconTexArr:Array;
function Start ()
{
// Start
iconTexArr = new Array ();
// Get textfile contents
var sr : StreamReader = new StreamReader(Application.dataPath + "/icons.txt");
var fileContents = sr.ReadToEnd();
sr.Close();
// Split each row (file name) to create array of names
var fileNames:Array = fileContents.Split("\n"[0]);
// Variables
var nameStr:String;
for (var i:int = 0 ; i < fileNames.length ; i++)
{
// name
nameStr = (fileNames[i] as String);
Debug.Log ("name:" + fileNames[i]);
// Make it Texture2D *** NOT WORKING ***
var loadedMap:Texture2D = UnityEngine.Resources.Load(nameStr,Texture2D);
// Add to array
iconTexArr.Push(loadedMap);
}
// Only last one is in array
Debug.Log ("Arr:" + iconTexArr);
}
Not actually, but so far I was under impression that there can be only one Resources folder. But now I read that “Multiple “Resources” folders” can exist. That is why I wanted to have some sort of list to filter out only certain files, but maybe I could just sort them in sub-folders and use LoadAll.
From manual:
var textures : Object[] = Resources.LoadAll("Textures", Texture2D);
So I guess I’ll try this first. Seems like loading images as Texture2D is bit complicated at least for me. I also tried www, got some problems with it too.
Texture2D[] textures = Resources.LoadAll("Textures") as Texture2D[];
or in javascript (might not work as my javascript is really rusty)
var textures:Texture2D[] = Resources.LoadAll("Textures") as Texture2D[];
this way you get access to the properties from texture2D and arent limited to object inheritations
(also easier to understand for other programmers what you are doing)
Thank you for help! I got texture loading working.
However, I couldn’t get Texture2D[ ] working, I had to use Object[ ]. So This code works, but if I try to just change Object to Texture2D, it won’t.
What comes to arrays as side note, I’m bit too noob with all kinds of Unity supported lists and arrays, I take it these will be fixed-size generic arrays.
yes, it has to be object[ ]
you have to foreach/for and cast to texture2d
using system.collections.generic;
List textureList = new List;
foreach (object o in Resources.LoadAll(“Textures”, Texture2D)) {
textureList.Add(o as Texture2D);
}
I suspected that type needs to be object, thank you for clarification. Your foreach loop looks very elegant, I’ll definitely have to find a JavaScript book, I had no idea that return value of Resources.LoadAll could be iterated like that.
Like I have all the images in the Resources folder and I have a list of image names( not all the image names are there on this list)? How can I load all those images (whose names are on the list) to an array?