Skip to content

Speed up preloads using unity-scene-repacker#157

Merged
fifty-six merged 20 commits into
hk-modding:masterfrom
jakobhellermann:repack-preloads
Oct 7, 2025
Merged

Speed up preloads using unity-scene-repacker#157
fifty-six merged 20 commits into
hk-modding:masterfrom
jakobhellermann:repack-preloads

Conversation

@jakobhellermann

@jakobhellermann jakobhellermann commented Jun 30, 2025

Copy link
Copy Markdown
Contributor

Currently preloading loads the entire scene at startup, which is usually the slowest part of startup.

unity-scene-repacker lets us repack the game's scenes into AssetBundles which can be loaded instead.

This PR introduces a PreloadMode globalsetting, with the following modes:

  • full-scene: the current behaviour
  • repack-scene: repacks the scenes into an AssetBundle containing the exact referenced scenes, but filtered to only relevant objects. The preloading code stays pretty much the same
  • repack-assets: repacks the scenes into an AssetBundle that contains the game objects separately, which can then be spawned using `bundle.LoadAsset("SceneName/path/to/object.prefab"). For compatibility with the current API, every object still gets spawned in the beginning and handed off to the mods.

Some rough benchmarking with a few mods enabled:

  • 20s full-scene
  • 9s repack-scene
  • 5s repack-assets
    However, only full-scene and repack-scene modes support sceneHooks.

In this PR:

  • new preloader strategies 7be2e86 a81730f
    • this include a new unityscenerepacker.dll (or libunityscenerepacker.{so,dylib} on linux/macOS)
  • remove $(SolutionDir) from csproj f9f50d0 this enabled dotnet build Assembly-CSharp --runtime win-x64 which is required to copy the native dependencies.
  • fix smoothing of progress bar f3f77be
    • previously, when progress was made too fast, it would never actually catch up
  • add <CopyDir> csproj variable which you can set to your game to automatically copy files on build, simplifying the build -> run -> edit cycle (I can move this to another PR or delete this if y'all don't like this)

@jakobhellermann
jakobhellermann marked this pull request as draft June 30, 2025 20:03
@jakobhellermann

Copy link
Copy Markdown
Contributor Author

Draft because the version of https://github.com/jakobhellermann/unity-scene-repacker used is not yet released and currently contains some hardcoded data for Hollow Knight I want to remove first.

@jakobhellermann

Copy link
Copy Markdown
Contributor Author

Test build: MAPI-windows.zip

@BadMagic100

Copy link
Copy Markdown
Contributor

Practically speaking, I think having this as a mapi global setting is bad since the majority of users will see no benefits from the feature that way. I would propose the following approach instead:

  • Possibility of different strategies per mod
  • By default, all mods use repack-scene for a modest performance improvement
  • Mods which don't implement PreloadSceneHooks can opt themselves in to repack-assets for even better performance. Perhaps also worth considering, since this is opt-in anyway, making the API break that was discussed in discord to just be handed the assetbundle for even better performance
  • Optional: add a legacyPreloads global setting (default false) to allow players to revert to the current behavior instead of repack-scene by default. Would allow players to work around any incompatibilities

Comment thread Assembly-CSharp/ModHooksGlobalSettings.cs Outdated
@PrashantMohta

Copy link
Copy Markdown
Contributor

I think having an opt out ( setting it to enabled by default) will be good enough for players wanting to kill switch it.

for mods wanting to use scene hooks, they should be able to indicate somehow that they care to hook into some scenes and only those should be loaded in that case.

@jakobhellermann

Copy link
Copy Markdown
Contributor Author

Practically speaking, I think having this as a mapi global setting is bad since the majority of users will see no benefits from the feature that way. I would propose the following approach instead:

Yes, it makes sense to have this enabled my default. I mostly did it this way first in case we wanted to do some testing before rolling it out, in case there are any issues that come up. E.g., I haven't tested this on macOS, and I wonder if the native dependency immediately works on apple silicon.

Mods which don't implement PreloadSceneHooks can opt themselves in to repack-assets for even better performance.

Since we know what mods request scene hooks, we could also do that automatically. Use repack-scene for those that need it, and repack-assets otherwise.

Comment thread Assembly-CSharp/Preloader.cs Outdated
Comment thread Assembly-CSharp/Preloader.cs Outdated
By extracting `DoPreloadRepackedScenes` into a separate method, that
just passes the scenePrefix to `DoPreloadScenes`.
previously this was hardcoded in the unityscenerepacker dll
@jakobhellermann
jakobhellermann marked this pull request as ready for review July 2, 2025 13:15
Comment thread Assembly-CSharp/Assembly-CSharp.csproj
@fifty-six

Copy link
Copy Markdown
Member

Have other people tried this much? The pr itself looks fine to me, though if you could get UnitySceneRepacker.cs using the typical C# style that'd be nice. Sorry for taking a while, slipped my mind. There's a few very minor nits I'd like to mess with if you don't mind me doing that as well.

@SFGrenade

Copy link
Copy Markdown
Member

Have other people tried this much? The pr itself looks fine to me, though if you could get UnitySceneRepacker.cs using the typical C# style that'd be nice. Sorry for taking a while, slipped my mind. There's a few very minor nits I'd like to mess with if you don't mind me doing that as well.

i'm using this api version and so far i've not seen anything wrong in the RepackAssets mode.

@jakobhellermann

Copy link
Copy Markdown
Contributor Author

Sorry for taking a while, slipped my mind. There's a few very minor nits I'd like to mess with if you don't mind me doing that as well.

No worries, and I don't mind. I don't have a lot of time to work on this anyways for the next month, so I'm in no rush to get this merged.

@fifty-six
fifty-six merged commit aa20a17 into hk-modding:master Oct 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants