Hot Reload for Unity (on device)how to build?

How to build Hot Reload For Unity and run it on device

By Chris Handzlik - 20 Feb, 2023

Hope you have already read 'How to build Hot Reload For Unity' - which will allow you to see script changes nearly instantly - skipping the dreaded domain reload bar!

We're going to build on that and take the hot-reload concept even further - to actual device.

Issue at hand

I'm a VR dev, mainly working with Android-based devices. A huge part of why I've built Fast Script Reload was an annoyance when a feature worked fine in Editor but broke on-device.

Some nuances can only be addressed on device. When you hit those - you end up in a loop:
change -> build -> deploy -> start on-device -> test.

It can go on for hours, burning time and your will to live :))

TLDR;

Have a look at github repo. It has a working example which allows you to hot reload script changes in build EXE / APK.

Approach

With changes already being hot reloaded, we could take the concept even further. What if we could take generated assembly, and reload it but on the device?

That'd be an absolutely huge productivity boost. 100% possible and to be honest fairly straightforward now that we got hot reloading part done.

(There's a single 'but' - for detour via MonoMod to work - it needs to run on Mono backend. A lot of apps are built with IL2CPP backend, while I didn't find a solution to that - it's usually a seamless transition to change IL2CPP to Mono, and we'll focus there.)

So, we already have:

  • a compiled assembly with the changes
  • a way to redirect calls from original code to the changed one

The only bit left is getting that assembly to the device. A nice to have would be for that to be fairly seamless. Usually, both devices are in the same network and not all devs are quite familiar with networking in general. With that in mind, auto-discovery functionality would be welcome so it works without fussing around with IPs / settings / etc.

There's a lightweight networking library that will use for that purpose: LiteNetLib

To be completely honest there's nothing difficult in making that leap to on-device hot reload - although feature-wise it's a great improvement for that workflow. One that was quite liked on Reddit when I first floated the idea. So without further ado, have a look at Github repo

Remaining challenges

Most of the challenges will bring us back to compilation/code adjustments to make hot reload functionality more robust:

  • if you cross-reference changed classes you'll get an error. This is down to technically having 2 classes with the same name, if you use them both - the compiler will complain. We'll look at ways to adjust code / or IL representation to make sure your compiled changes do not clash with each other
  • new fields are not visible in the editor and can not be used in a way you're used to

Next Steps

Have a look at github solution, it's a simple approach to show how hot reload on device is done. It's simple on purpose - so critical parts are standing out.

I'll be digging deeper into remaining chalanges and it'd be great to have you on the journey.

Please subscribe to mailing list. I'll send you a message when next piece of puzzle is broken down and described. I'll also keep GitHub repo updated - so you can simply grab that instead :)

Drop your email below and I'll let you know when next post is ready!

* indicates required

Btw: Ready made tool is available on the asset store

If you're after a ready-made solution then have a look at Live Script Reload (on device hot reload) on the asset store

I've made sure to handle some wider networking considerations that you may not want to take care of.

Go Back ×

What You're Struggling With?

We'll be sure to look into that for you!

Go Back ×

We're still working on that blog post!

Why don't You sign up to our Automation Insights and we'll keep you updated!