Can Unity Editor enter PlayMode without reloading a script

I am writing an Unity editor script that creates TCP server accepts requests from other application, but I found out that “clicking” play mode reloads my editor scripts which will interrupts the connection by re-initializing my server. Is there any way Unity Editor can stop reloading this particular script and makes it up running at all time?

    static void InitializeOnLoadMethod()
        if (m_Server == null)
            EditorApplication.update += Update;
            IPAddress ip = IPAddress.Parse(ipAdress);
            m_Server = new TcpListener(ip, port);
            Debug.Log("Sever started");

            //Wait for async client connection 
            m_Server.BeginAcceptTcpClient(ClientConnected, null);

In other words, is there a way to keep all my static variables and my editor coroutines after PlayMode is invoked? I did some search and I think domain reloading has caused it

No there’s no way. Keep in mind that Mono / C# is just the scripting environment used by Unity. Unity itself is mainly written in C++. Whenever you enter playmode or whenever unity recompiles your scripts the whole appdomain and scripting environment is shutdown and reloaded. Nothing on the managed really remains. Unity just restores everything it can serialize after the reload. However this does not include any objects or classes it can not serialize or classes which might wrap native resources (like sockets or threads).

You could probably implement this in a native C++ plugin which as far as I know are not reloaded since they do not belong to the managed land. However there could be all sorts of issues with such an approach. If you really want to implement a TCP server, I highly recommend to implement it as a seperate application and have the Unity editor just connect to it as a client. Unity itself does something like that with the shader compiler. It’s a hidden process running in the background and you can connect to it through TCP. Also you should be careful when starting threads during edit time. Managed threads build on top of native threads. All sorts of things can go wrong when the editor does an assembly reload. See here for example.

I would generally advice to stay away from threads (and some other native wrappers) inside the Unity editor. Of course you can use threads at runtime but you should ensure a graceful shutdown of the threads when you exit playmode. If you want to use threads during edit mode that is possible but extra care should be taken when and how you end those threads. In most cases you shouldn’t need to use any threads during edit mode. Even Unity actually blocks the main thread for most operations and displays either a ProgressBar or a CancelableProgressBar while you perform your task. Keep in mind that the Untiy editor is mainly an authoring tool for game development and not meant as an operating system. Yes, I know, it’s fun to implement all sort of things into the editor, but you should ask yourself if that’s really a feature relevant to game development?