- Home /
Dragging multiple assets to inspector
Hello all,
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?
Im getting the same issue.
I have discovered it is due to the way the images or objects are imported into unity. If you use an explorer and select 100 items named item01,item02 etc and drag and drop them into unity. They will then appear in random order when dragged into a public array.
If the items are dragged into Unity one at a time e.g. item01 dragged and dropped, then item02 etc. The items will then drag in the correct order to the public array.
Unfortunately this bug removes a lot of the use of being able to drag into a public array.
Hope Unity can fix it soon. Getting sick of filling 200+ arrays one by one.
I guess I should spend the time to make a UI script that handles it.
Answer by JoeStrout · Apr 20, 2015 at 04:55 PM
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.
Answer by saschandroid · Feb 18, 2014 at 01:59 PM
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
.
Thanks saschandroid.
I have not tried out the code yet. But it looks like it does solve the issue.
Will test it out shortly.
Your answer
