Search Unity

Feedback [True] Hot Reload - Possible Someday?

Discussion in 'Editor & General Support' started by joshcamas, Aug 1, 2019.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,278
    Hello!

    I know that a form of "Hot Reload" already exists in Unity - you can technically recompile code while a game is running. However, this comes with a lot of caveats - specifically with strange OnEnable/OnDisable triggers happening, and serialization. Thus, the current implementation hot reloading is simply too limited for any large projects (Hence most people just disable compiling in play mode entirely).

    I know that you can make compiling faster by using Assemblies, but I'm not actually complaining about compilation speed. Even if compilation speed was instant, it's just not as powerful as being able to have your world running and being able to change some code. For me, it takes around 40 seconds to compile and start the game up, not including going back to the state I was in beforehand. Maybe this is a good speed, and I'm just too picky? But I do know being able to have instant feedback is *amazing* - I have a scripting engine in my game and using it is a **dream** because of how easy it is to debug and test things. It's truly shown me what I was missing.

    Now, maybe this is just... impossible. I know UE4 does it, but of course UE4 is entirely different, so comparing isn't exactly useful.

    But if it is possible - are there any possible plans for this in the far future? I'm sure this would be a huge change, but still I figured I'd ask. Even if there are no promises, is this a possibility of some kind someday?

    Thank you!
     
    davidrochin likes this.
  2. unity_7PljJ4sqTncHsQ

    unity_7PljJ4sqTncHsQ

    Joined:
    Aug 7, 2019
    Posts:
    2
    There are plugins that allow you to do this!

    You can use a runtime compiler to hot reload in runtime like you would for lua.
    https://assetstore.unity.com/packages/tools/integration/roslyn-c-runtime-compiler-142753

    The only problem is that it isn't magic. So it won't just hot reload your entire project automatically and it works. You have to be slightly more intentional with what you recompile.

    Like you mentioned it would be great if we could get native support, but I don't think Unity will do that before DOTS is finished because the whole monobehaviour API is going to get deprecated in the future so they probably want to make something that works with DOTS. (All speculation though)
     
  3. DanVioletSagmiller

    DanVioletSagmiller

    Joined:
    Aug 26, 2010
    Posts:
    204
    I'm running a series on Hot Reload, AKA Recompile and Continue Playing. There are many features of C# it doesn't support by default, such as statics. Honestly, speed has been fine for me. However, on most projects, I start splitting the code into different assembly definitions to reduce the amount to recompile, and the speed at which a recompile can continue. I'm covering my findings on going over this here: Unity Compile & Continue - YouTube
     
  4. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,144
    In your video about singletons you found a hacky way to preserve a reference to a static Singleton by using the ISerializationCallbackReceiver:
    https://github.com/DanVioletSagmill...sets/Testing/CSingletons/ScoreManagerC.cs#L11
    However, I believe this method is very dangerous and can have unpredictable consequences. The ISerializationCallbackReceiver is designed for handling serialization but in the snippet above it's used for a totally different purpose.

    I found an awesome article about hot reloading, it explains a lot of details. Also, it proposes a more straightforward way of handling singleton hot reloading:
    https://gist.github.com/cobbpg/a74c8a5359554eb3daa5#supporting-static-fields
     
    Last edited: May 7, 2022
    Rewaken and nirvanajie like this.
  5. PencilBow

    PencilBow

    Joined:
    Jun 23, 2017
    Posts:
    132
    When we were developing our game, we ran into the same issues as you - except in many cases, it would take even longer (2 minutes to compile, and another 30-60 seconds to get back to the prior game state).

    In the end, we built a C# Hot Reload extension for Unity which we just recently made public. It works pretty much as you’d expect Unity’s default “hot reload” functionality to work, but with lightning fast compiling and no domain reload (so your variables stay intact). Basically we made a C# compiler which only compiles the specific method that changed (which is very fast, milliseconds), and then we swap just that function in playmode. Now we have < 1 second iteration times in our project rather than 2-3 minutes.

    Feel free to check it out on our website hotreload.net or on our Unity Forum post.