LoadAll order issue

Hi,

I used a for-loop to display ascending textures in the gui.
But the strange thing is that the order line is completely different than the name order in my resource map.

for(var n = 41; n > -1; n--)
	{
	    var textures : Object[] = Resources.LoadAll("GUI", Texture2D);
		guiTexture.texture = textures[n];
		yield(WaitForSeconds(1));
	}

If I use Debug.log(n) then I get nicely the countdown from 41 to 0, but when i do Debug.log(textures[n]) I get 9,8,7,6,5,41,40,4,39,38 on so forth. The names of my images are from the same as the n values (0 to 41) and in the GUI map which is in the Resources map.

So the for loop works well, but the loadall does strange, does someone knows what’s happening and knows how to fix it?

Thanks!

(And sorry for my bad english)

I see that you had solved your problem but sounds like you don’t understand why. Resources.Load returns the results alphabetically (it’s the same order that will see in the project tab), so it compares the first char for each file name and it will only compares the second one if the first one are different.

The second problem is that you are loading all textures 41 times. The third problems is that you hardcoded that you have 41 textures, so if you delete or add one texture you have to come back and fix you code. So, what I recommend is:

var textures : Object[] = Resources.LoadAll("GUI", Texture2D);
for (var n = textures.Lenght - 1; n >= 0; n--) {
       guiTexture.texture = textures[n];
       yield(WaitForSeconds(1));
}

public static int CompareToUnity(this string strA, string strB)
{
if (strA.IsNullOrEmpty() || strB.IsNullOrEmpty())
{
return strA.CompareTo(strB);
}

    int minLength = strA.Length < strB.Length ? strA.Length : strB.Length;

    for (int i = 0; i < minLength; ++i)
    {
        if (char.IsDigit(strA_) && char.IsDigit(strB*))*_

{
string numberA = “”;
string numberB = “”;
for (int j = i; j < strA.Length; ++j)
{
if (char.IsDigit(strA[j]))
{
numberA += strA[j];
}
else
break;
}
for (int j = i; j < strB.Length; ++j)
{
if (char.IsDigit(strB[j]))
{
numberB += strB[j];
}
else
break;
}
int compare = int.Parse(numberA).CompareTo(int.Parse(numberB));
if (compare != 0)
return compare;
else
i += numberA.Length - 1;
}
else
{
int compare = ((int)strA_).CompareTo(strB*);
if (compare != 0)
return compare;
}
}
if (strA.Length < strB.Length) return -1;
else if (strA.Length > strB.Length) return 1;
else return 0;
}*_

I know this is an old thread, but in case others get annoyed by the order issue, the fastest would be to just number your textures flowerOpacityMap_0, flowerOpacityMap_1, flowerOpacityMap_2… etc. and load in via a for loop using the “Resources.Load”.

Make sure they are all placed in a folder you name whatever, inside a folder called “Resources”.
Eg. Assets/Resources/PurpleFlower_opacity_map/flowerOpacityMap_1.

I can’t think of a use case for .LoadAll at all really.

This code preps an array with opacity map textures, that can be switched in your shader.
Using .Load you’d just do:

public string pathOfResources = "PurpleFlower_opacity_map/flowerOpacityMap_";

void Start() { 
        m_AlphaArray = new Texture[arraySize];
        m_Renderer = GetComponent<Renderer>();    
        
        for (int i = 0; i <= arraySize; i++)
        {
            string tempPath = pathOfResources + i.ToString();
            var texture = Resources.Load<Texture2D>(tempPath);
            m_AlphaArray *= texture;*

}
}