I have a script with a public Textures array attached to an object.
I was happy to see that I can select my 24 texture files, and just drag them onto the Textures field in the inspector. It automatically increased the array size to 24 and applied all textures to the array elements.
However, the result was unusable, since it did not maintain any order.
My textures are named walk0001 to walk0024 but the resulting order was completely random.
Screenshot:
Anything I am missing? Or is it just not possible to drag multiple assets in an organized fashion?
FWIW, in case somebody else stumbles across this question as I did, here’s the solution I have settled on for now. In the script containing an array property, add a bit of code to create a contextual menu, like this:
[ContextMenu ("Sort Frames by Name")]
void DoSortFrames() {
System.Array.Sort(frames, (a,b) => a.name.CompareTo(b.name));
Debug.Log(gameObject.name + ".frames have been sorted alphabetically.");
}
In this example, I have a “public Sprite frames” property; just change all occurrences of “frames” to whatever your own array property is, and it ought to work. (Also, this is C#; changes would be needed for JavaScript.)
This lets the designer decide when they want to sort, by right-clicking the script and choosing the new “Sort” command the above code adds to the contextual menu.
I don’t know if this is still useful for anybody, but I had the same “problem” and fixed it by sorting the Texture2D-array at Start().
// Texture array
public Texture2D[] m_textures;
// 0-based starting position of numbers in image name
public int m_numStart;
// Length of the number part the image name
public int m_numLength;
private void Start ()
{
sortTexturesByNumber(m_textures, m_numStart, m_numLength);
}
private Texture2D[] sortTexturesByNumber(Texture2D[] f_textures, int f_index, int f_length)
{
for (int i = 1; i < f_textures.Length; i++)
{
int j = i;
while (j > 0)
{
string l_nameOne = f_textures[j - 1].name.Substring(f_index, f_length);
string l_nameTwo = f_textures[j].name.Substring(f_index, f_length);
if (int.Parse(l_nameOne) > int.Parse(l_nameTwo))
{
Texture2D l_temp = f_textures[j - 1];
f_textures[j - 1] = f_textures[j];
f_textures[j] = l_temp;
j--;
}
else
{
break;
}
}
}
return f_textures;
}
So if your images have names like “image####.png”, m_numStart = 5 and m_numLength = 4.