Skip to content

Commit 1cf9a0a

Browse files
committed
Added FileWatcher.
1 parent 098f2aa commit 1cf9a0a

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.
2+
3+
#if UNITY_EDITOR
4+
using System.IO;
5+
using UnityEditor;
6+
using UnityEngine;
7+
8+
namespace CandyCoded.GitStatus
9+
{
10+
11+
[InitializeOnLoad]
12+
public static class FileWatcher
13+
{
14+
15+
public delegate void EventHandler();
16+
17+
public static event EventHandler UpdateEvent;
18+
19+
private static FileSystemWatcher _fileSystemWatcher;
20+
21+
private static bool _changeRegistered;
22+
23+
static FileWatcher()
24+
{
25+
26+
if (_fileSystemWatcher != null)
27+
{
28+
29+
return;
30+
31+
}
32+
33+
_fileSystemWatcher = new FileSystemWatcher(Application.dataPath);
34+
35+
_fileSystemWatcher.Changed += OnChanged;
36+
37+
_fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
38+
_fileSystemWatcher.Filter = "*.*";
39+
_fileSystemWatcher.IncludeSubdirectories = true;
40+
_fileSystemWatcher.EnableRaisingEvents = true;
41+
42+
}
43+
44+
private static void OnChanged(object sender, FileSystemEventArgs e)
45+
{
46+
47+
if (e.FullPath.EndsWith(".meta"))
48+
{
49+
50+
return;
51+
52+
}
53+
54+
if (!_changeRegistered)
55+
{
56+
57+
EditorApplication.update += OnAllChanged;
58+
59+
_changeRegistered = true;
60+
61+
}
62+
63+
}
64+
65+
private static void OnAllChanged()
66+
{
67+
68+
UpdateEvent?.Invoke();
69+
70+
EditorApplication.update -= OnAllChanged;
71+
72+
_changeRegistered = false;
73+
74+
}
75+
76+
}
77+
78+
}
79+
#endif

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/FileWatcher.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)