我正在从文件夹中加载图像并应用于RawImage
。然后,借助FileSystemWatcher
类,我正在检查文件夹中是否有新图像。
public static Dictionary<string, Object> textures = new Dictionary<string, Object>();
private void Start()
{
//Path
try
{
string[] configMessage = ReadConfig.GetLines("Path");
path = configMessage[0];
}
catch
{
path = "C:\\xampp\\htdocs\\folder\\images";
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
GetAssetList();
for (int i = 0; i < textures.Count; ++i)
{
AddImageToMosaic((Texture)textures.ElementAt(i).Value);
}
}
private void GetAssetList()
{
try
{
string[] fileEntries = Directory.GetFiles(path);
foreach (string fileName in fileEntries)
{
byte[] fileData;
Texture2D tex = null;
if (File.Exists(fileName))
{
fileData = File.ReadAllBytes(fileName);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
textures.Add(fileName, tex);
}
}
}
catch (System.Exception e)
{
print(e.ToString());
}
}
private void OnChanged(object source, FileSystemEventArgs e)
{
print("OnChanged " + e.FullPath);
Populate(e.FullPath);
}
然后在FileSystemWatcher
事件中,我致电Populate()
:
static Populate(string imageName)
{
string fileName = imageName;
byte[] fileData;
if (File.Exists(fileName))
{
fileData = File.ReadAllBytes(fileName);
Texture2D tex = new Texture2D(128, 128);
tex.LoadImage(fileData);
if (!textures.ContainsKey(fileName))
{
textures.Add(fileName, tex);
}
}
}
代码在Texture2D tex = new Texture2D(128, 128);
之后不执行。我已经在以后的一些日志中证实了这一点。