Display pictures in assets folder in random order
Hello,
new to Unity, so I probably made some stupid mistakes, but I'd still appreciate the help.
I want to display all pictures in the assets/resources folder in random order with one second delays. So I thought I could just load their filenames into a hash table with numerical keys and generate a list with numbers equal to the keys at the same time. Then I randomise the list and use the contents to access the hash table, instantiating the pictures as sprites. (The pictures should later be partially occluded, so that's why I'm guessing I can't just work with GUITexture.)
If anyone can point me to an easier way to do this, I'd be grateful, but here's what I have so far (which doesn't do anything - no error, no debug text, nothing). If you could give it a look and tell me what I'm doing wrong, that would be great. Thanks!
public class SocDistance : MonoBehaviour {
Hashtable picHash = new Hashtable();
List<int> picList = new List<int>();
public void PicAssign () {
int i = 0;
DirectoryInfo dir = new DirectoryInfo("myPath"); // resources path
FileInfo[] fileamount = dir.GetFiles("*.*");
foreach (FileInfo fi in fileamount)
{
if (fi.Extension.Contains(".JPG"))
{
i++;
picHash[i] = fi.Name;
Debug.Log (fi.Name);
picList.Add(i);
}
}
}
public void PicRandom () {
for (int n = 0; n < picList.Count; n++) {
int temp = picList[n];
int randomIndex = UnityEngine.Random.Range(n, picList.Count);
picList[n] = picList[randomIndex];
picList[randomIndex] = temp;
}
}
IEnumerator Stimulus () {
int z = picList.Count;
int y = picList[1];
while (z > 0)
{
string pic = (string)picHash[y];
string picNoext = pic.TrimEnd ('.','J','P','G');
GameObject stimulus = new GameObject();
SpriteRenderer sr = stimulus.AddComponent<SpriteRenderer>();
sr.sprite = Resources.Load(picNoext, typeof(Sprite)) as Sprite;
Debug.Log(picList[1]);
picList.RemoveAt(1);
z--;
yield return new WaitForSeconds(1f);
}
}
void Start () {
PicAssign();
PicRandom();
StartCoroutine(Stimulus());
}
void Update () {
}
}
look up Resources.LoadAll, which might be easier to use for loading all images from a particular folder