- Home /
Resources.LoadAll Issue with variable type
private Texture2D[] pages;
// Doesn't work
string bookTitle = BookData.m_BookData[ID].GetTitle();
pages = (Texture2D)Resources.LoadAll("BookPages/" + bookTitle, typeof(Texture2D));
// Work, but can only load one at a time
string textureLocation = "BookPages/" + BookData.m_BookData[0].GetTitle() + "/1";
pages[0] = (Texture2D)Resources.Load(textureLocation, typeof(Texture2D));
I need to load all of the textures in a folder, each folder have different number of files. But LoadAll will only load as GameObject.
I guess bookTitle is the name of only one book. Documentation says: "If path refers to a folder, all assets in the folder will be returned. If path refers to a file, only that asset will be returned". As you specified one single book (title) in Resources.LoadAll
you get only one Texture2D
. You have to use the folder name (and (Texture2D[]) ResourcesLoadAll(...);
)
Answer by whydoidoit · Jun 11, 2012 at 06:17 PM
Your LoadAll loads them all as an array - so you need to cast each element to Texture2D.
var texture = (Texture2D)pages[0];
You could use Linq to get a Texture2D array and do it with:
using System.Linq;
var textures = Resources.LoadAll("Whatever", typeof(Texture2D)).Cast<Texture2D>().ToArray();
I tried to use this for Sprite type, but it did not work...
try using different types, $$anonymous$$aybe GameObject