Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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:
    223
    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?