Search Unity

C# Hot reload with VS, hidden feature?

Discussion in 'Editor & General Support' started by artur-leao, Nov 3, 2015.

  1. artur-leao

    artur-leao

    Joined:
    Feb 3, 2015
    Posts:
    66
    Or has this been announced? I was making some changes to my code and forgot to Stop/Play and the code changes did work after saving the file without restarting the scene. It was a very simple change but still... pretty cool.

    I wonder if this is work in progress and still not fully supported hence it hasn't been officially announced.
     
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    It works on simpler projects but on bigger project it sometimes breaks.
    It has been around for a long time.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    I would really, really love to be able to deactivate this feature. I've never seen it work for anything larger than a game jam size project, and on our very large thing, it has a tendency to crash the editor.
     
    fffMalzbier likes this.
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,291
    You could try disabling Auto Refresh whenever you enter play mode. Put this into an editor script:
    Code (csharp):
    1. EditorPrefs.SetBool("kAutoRefresh", false);
    I have not tested it myself so I could be wrong :D
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Thanks a lot!

    It's not quite enough on it's own, as setting the autorefresh to true again when exiting play mode doesn't trigger a refresh if any files are changed. So I'm calling AssetDatabase.Refresh(); as I leave play mode. That seems to have done the trick.

    Relevant parts of my editor script if somebody's interested:

    Code (csharp):
    1. [InitializeOnLoad]
    2. public static class OnSceneLoadScript {
    3.  
    4.     static OnSceneLoadScript() {
    5.         EditorApplication.playmodeStateChanged += ChangePlaymodeCallback;
    6.     }
    7.  
    8.     private static void ChangePlaymodeCallback() {
    9.         //This is only kicks off when you have exited play mode.
    10.         if (!EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying) {
    11.             EditorPrefs.SetBool("kAutoRefresh", true);
    12.             AssetDatabase.Refresh();
    13.         }
    14.        
    15.         //Called just after play mode is entered
    16.         if (EditorApplication.isPlayingOrWillChangePlaymode && EditorApplication.isPlaying) {
    17.             EditorPrefs.SetBool("kAutoRefresh", false);
    18.         }
    19.     }
    20. }

    Edit one and a half year later, as I keep linking people to this. Originally the second if there looked like this:
    //Called before play mode entered
    if (EditorApplication.isPlayingOrWillChangePlaymode && EditorApplication.isPlaying)

    If you changed a script in a way that introduced compiler errors, and then pressed "play" before the code was finished recompiling, the auto refresh would now be set to false, and you'd have to open the preferences menu to turn it back on. That was annoying, so turning auto refresh off after the game had entered play mode prevents that problem.
     
    Last edited: Feb 23, 2017
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    @karl.jones, a coworker just walked by and went "oh, that's neat. Where's the reference for all of the magical strings that does something in EditorPrefs?"

    Is there such a reference other than looking at the registry?
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,291