Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BlackRockShoota · Dec 14, 2019 at 02:15 PM · editor-scriptingeditor extensioneditorscript

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?

     [InitializeOnLoadMethod]
     static void InitializeOnLoadMethod()
     {
         if (m_Server == null)
         {
             EditorApplication.update += Update;
             Debug.Log("starting");
             IPAddress ip = IPAddress.Parse(ipAdress);
             m_Server = new TcpListener(ip, port);
             m_Server.Start();
             Debug.Log("Sever started");
             Debug.Log(ip);
             Debug.Log(port);
 
 
             //Wait for async client connection 
             m_Server.BeginAcceptTcpClient(ClientConnected, null);
             OnServerStarted?.Invoke();
         }



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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Dec 14, 2019 at 04:22 PM

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to get all editable fields of a component in a script? 2 Answers

Run method of editor script when application is playing 1 Answer

Play Unity Editor from VSCode? 0 Answers

Change Unity Project Settings through Script 1 Answer

Dynamic content in custom EditorWindow 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges