A lot of the versions of Unity printed the full path instead of the name of the file when a file watcher was looking at a directory. Which according to the documentation by msdn it should print the file name instead. This was a consistent behaviour (though not correct), but it’s a bit anxiety inducing since I’m not sure if its the same behaviour on linux and osx.
After the mono upgrade in a16, the Name property continued to stay the same, but then the FullPath property started printing two full paths concatenated together. One was backslashed and the other wasnt (I was on windows so the forward slash path should be invalid).
C:\GitHub\project\file.txtC:/GitHub/project/file.txt
@bdovaz also posted a thread in the experimental forum about a specific FileSystemWatcher api missing lately if using IL2CPP with one of the api compatability levels, but ive tested and its missing in both of them at the moment (2021.2.0b1).
Is this specific api managed/observed by Unity in any way? I’d assume “yes” since they have a fork of mono where they can adjust fix this behaviour. Btw the issue tracker has this filed for the Name bug and the FullPath bug as a note, I figure they’re related enough to warrant the same bug ticket even though one existed for a long time and the other only just recently appeared, is this correct?
I had a lot of problems with this API. Registering to its events was very costly after some Unity version and we had to move it to a thread. And after some time the events were firing multiple minutes after the edits were done to the files. I thought that the problems may have something to do with Windows Update and not Unity…
I had the same effect @mahdi_jeddi and it was driving me nuts. Then I checked other assets in my project and realized on new asset had installed its own watcher, completely throwing my watcher off track. I just deleted their watcher and everything was back to normal.
My setup is to take all of the 4 events that the watcher outputs directly into a List<> and process the list in a while loop inside update, its fairly fail safe in my past projects. Minute delays for the events sounds atrocious though, could be something on the OS level.
private List<FileSystemWatcherArgs> fileEvents = new List<FileSystemWatcherArgs>();
private void OnEnable()
{
FileSystemWatcher watcher = new FileSystemWatcher(Application.dataPath)
{
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName,
EnableRaisingEvents = true,
IncludeSubdirectories = true
};
watcher.Changed += (o, e) => fileEvents.Add(e);
watcher.Renamed += (o, e) => fileEvents.Add(e);
watcher.Deleted += (o, e) => fileEvents.Add(e);
watcher.Created += (o, e) => fileEvents.Add(e);
}
private void Update()
{
foreach (FileSystemWatcherArgs fileEvent in fileEvents)
{
//do something
}
fileEvents.Clear();
}
1 Like
How did you do that? Searching for FileSystemWatcher in all script files?
If it’s a OS level problem, why Unity works fine when I add a new file to the assets folder? Do they use some other API for that? Maybe a bug in Mono implementation?
Yes indeed, used Rider to find other usages of the API
Updating this as the bug fix is in review (interestingly for 2022.1 as well)