forked from PostCyberPunk/UnityAutoCompile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLz_AutoCompile.cs
94 lines (83 loc) · 2.68 KB
/
CLz_AutoCompile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using UnityEditor;
using UnityEngine;
using UnityEditor.Compilation;
using System.Net;
namespace Editor
{
[InitializeOnLoad]
public static class CLz_AutoCompile
{
private static HttpListener listener;
private static bool needUpdate;
private static string port = "10245";
static CLz_AutoCompile()
{
needUpdate = false;
CompilationPipeline.compilationStarted += OnCompilationStarted;
EditorApplication.quitting += _closeListener;
EditorApplication.update += onUpdate;
_createListener();
}
private static void _createListener()
{
try
{
_closeListener(); // Ensure previous listener is closed
listener = new HttpListener();
listener.Prefixes.Add("http://127.0.0.1:" + port + "/refresh/");
listener.Start();
listener.BeginGetContext(new AsyncCallback(OnRequest), null);
Debug.Log("自动编译监听器已启动");
}
catch (Exception e)
{
Debug.LogError("自动编译启动失败: " + e);
throw;
}
}
private static void OnRequest(IAsyncResult result)
{
if (listener == null || !listener.IsListening)
return;
HttpListenerContext context = null;
try
{
context = listener.EndGetContext(result);
}
catch (HttpListenerException)
{
return; // Listener closed or disposed
}
catch (ObjectDisposedException)
{
return; // Listener was disposed
}
if (context != null && !EditorApplication.isCompiling)
{
needUpdate = true;
listener.BeginGetContext(new AsyncCallback(OnRequest), null);
}
}
private static void _closeListener()
{
if (listener != null && listener.IsListening)
{
listener.Stop();
listener.Close();
listener = null;
Debug.Log("自动编译监听器已关闭");
}
}
private static void onUpdate()
{
if (!EditorApplication.isCompiling && !EditorApplication.isUpdating && needUpdate)
{
needUpdate = false;
AssetDatabase.Refresh();
Debug.Log("编译完成,刷新资源");
}
}
private static void OnCompilationStarted(object _) => _closeListener();
}
}