I have a function to get images from folder and display each on RawImage for 5 seconds, then start again.
I have this problem after first image is displayed when the looper() -function calls the medialogic() -function. It gives the error in title.
How could I solve this or why does this happen? I'm new with unity and C#.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
using System.Linq;
using System.Text;
public class Test2 : MonoBehaviour {
// Use this for initialization
//Publics
private string path = "file:///";
public string folder = "C:/medias/";
int interval = 7000;
int arrLength;
int i = 0;
string source;
string str;
WWW www;
string[] extensions = new[] { ".jpg", ".JPG", ".jpeg", ".JPEG", ".png", ".PNG", ".ogg", ".OGG" };
FileInfo[] info;
DirectoryInfo dir;
void Start() {
dir = new DirectoryInfo(@folder);
info = dir.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
arrLength = info.Length;
looper ();
}
// Update is called once per frame
void Update () {
}
void looper() {
medialogic ();
EasyTimer.SetInterval(() =>
{
if (i == arrLength-1) {
info = dir.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
arrLength = info.Length;
i = 0;
} else {
i++;
}
medialogic();
}, interval);
}
void medialogic() {
source = info [i].ToString();
str = path + source;
www = new WWW(str);
string[] extType = source.Split('.');
int pos = Array.IndexOf(extensions, "."+extType[1]);
if (pos > -1) {
GetComponent<RawImage> ().texture = www.texture;
Debug.Log (extType[1]);
} else {
//videos here
Debug.Log (extType[1]);
}
}
public static class EasyTimer
{
public static IDisposable SetInterval(Action method, int delayInMilliseconds)
{
System.Timers.Timer timer = new System.Timers.Timer(delayInMilliseconds);
timer.Elapsed += (source, e) =>
{
method();
};
timer.Enabled = true;
timer.Start();
// Returns a stop handle which can be used for stopping
// the timer, if required
return timer as IDisposable;
}
public static IDisposable SetTimeout(Action method, int delayInMilliseconds)
{
System.Timers.Timer timer = new System.Timers.Timer(delayInMilliseconds);
timer.Elapsed += (source, e) =>
{
method();
};
timer.AutoReset = false;
timer.Enabled = true;
timer.Start();
// Returns a stop handle which can be used for stopping
// the timer, if required
return timer as IDisposable;
}
}
}
Edit: User Programmer below has the correct answer.