Skip to content

Instantly share code, notes, and snippets.

@arcv
Last active February 20, 2021 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arcv/d1269e6480629a015dedd6a50621f79f to your computer and use it in GitHub Desktop.
Save arcv/d1269e6480629a015dedd6a50621f79f to your computer and use it in GitHub Desktop.
using System;
using System.Net;
using UnityEditor;
using System.Net.Sockets;
using System.Threading.Tasks;
[InitializeOnLoad]
public class CompileClient
{
public static bool setRefresh;
static TcpListener server = null; // this is the http server
static CompileClient()
{
Task.Run(startServer);
EditorApplication.update += onUpdate;
}
private static void onUpdate()
{
if (!setRefresh) return;
if (EditorApplication.isCompiling) return;
if (EditorApplication.isUpdating) return;
AssetDatabase.Refresh();
setRefresh = false;
}
private static void startServer()
{
server = new TcpListener(IPAddress.Parse("127.0.0.1"), 3001);
server.Start();
Byte[] bytes = new Byte[256];
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
setRefresh = true;
}
client.Close();
}
}
}
# pip install watchgod
# copy this file to project's root
# run python watch.py
from watchgod import watch, AllWatcher, DefaultDirWatcher
import socket
import sys
import time
class CsharpWatcher(DefaultDirWatcher):
def should_watch_file(self, entry: 'DirEntry') -> bool:
return entry.name.endswith(('.cs'))
def notify():
try:
time.sleep(0.2)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 3001))
s.send(b"hi!")
except:
print("error! ", sys.exc_info()[0])
for changes in watch('./Assets/', watcher_cls=CsharpWatcher):
notify()
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment