Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion FileSystemWatcher runs too slow on Unity Editor

Discussion in 'Editor & General Support' started by NGC6543, Jul 14, 2022.

  1. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    228
    Hi, I'm trying to create my own file change monitoring system using
    FileSystemWatcher
    .
    But the watcher performance is too bad for practical use.

    Background
    • I have additional codegen / datagen apps outside the Unity Editor.
    • When they run, they write a lot of files inside Unity Assets folder.
    • If Unity 'Auto Refresh' option is enabled, the two processes can interfere and unwanted result may occur.
      • So I need to disable the 'Auto Refresh' option, and refresh Asset import manually.(
        AssetDatabase.Refresh()
        )
    • I want to notify users with GUI when file change is detected.
    • I decided to make a watcher using
      FileSystemWatcher
      • A class with
        InitializeOnLoadAttribute 
        creates and holds the watcher instance.
    Status
    • The watcher class runs fine when built as a console app.
      • File change events are picked up immediately.
    • The same class runs extremely slow when run in Unity Editor.
      • It took more than 5 seconds to get the change event.
    Debug
    • Create Watcher instance on a background thread(
      Task.Factory.StartNew()
      )
      • Still too slow.
      • According to the sourcecode, FileSystemWatcher uses Threadpool internally. So the watcher is running on a background thread already.
    Question
    • I want to know why the watcher run slow when in Unity Editor. What makes it differ from console?
    • What is the proper way to implement my own file watcher in Unity?
     
  2. ChrisHandzlik

    ChrisHandzlik

    Joined:
    Dec 2, 2016
    Posts:
    203
    got very similar issue. file system watcher on big directories (with recursion) - is quite slow and causes performance spikes