- Home /
AudioClip Import Order Randomized?
Dragging a number of wave files from assets onto an empty array of AudioClips populates the array in a seemingly random order. Is it possible to have AudioClips in the array be in alphabetical order? With the large number of clips I'm dealing with (well over 1000), it is painful to drag them into the array one by one.... x_X"
using UnityEngine;
using System.Collections;
public class TestAudio2 : MonoBehaviour {
public AudioClip[] auTest;
...
}
dragging onto array: Array populated with clips in random order: Anyone has any idea how to import them in alphabetical order? ^^
Debug.Log(audioClips[0].name);
and
Debug.Log(audioClips[19].name);
for an array of length 20 both write the names properly to the console.
But that also throws the error
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
it's unusual that both names are written before the error is displayed on the console & the error is thrown on the 1st of the 2 Debug.Logs
Answer by boogie77 · Sep 14, 2015 at 10:30 AM
hi @Rostam24 thanks for your help! - it's working now with
System.Array.Sort(auTest,NameSort);
and
int NameSort (AudioClip a, AudioClip b) {
return a.name.CompareTo(b.name);
}
it's from a JavaScript solution for numbers or distances or something that I didn't manage to convert to C# successfully earlier
Ü
Answer by Rostam24 · Sep 12, 2015 at 07:11 PM
I would recommend writing a script that will change the order to whatever you want in the Awake() method.
hi @Rostam24 , had been trying that as well but I'm not sure how to do it on C#. certainly
System.Array.Sort(auTest);
does not work, and I tried other methods too.
I also tried importing AudioSources using
auTest = GameObject.FindObjectsOfType(typeof(AudioSource)) as AudioSource[]
but it populates the array again, unsorted (and it wouldn't import anything below the topmost layer in Hierarchy either).
can you share how you would do it in C#?
cheers
I would probably use a List with Linq (but you could probably use an array with Linq as well):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class YourClass : $$anonymous$$onoBehaviour
{
public List<AudioClip> audioClips;
void Awake()
{
audioClips.OrderBy(x => x.name);
}
}
I'm sorry, I'm not at home and I can't test it out... So let me know if it works! :)
Thanks @Rostam24 Ü That doesn't throw any errors or warnings, but the list still remains unsorted. This is puzzling.