hao-il2cpp-unity
Typed Unity wrappers for mods of Unity Il2Cpp games, on top of
hao-il2cpp-runtime.
The runtime crate finds and hooks methods by name and knows nothing about Unity. This crate wraps common Unity functionality the same way for any Unity game:
GameObject(create, find, add/get components, activate, tag, layer),Transform(position, rotation, scale, parent/children, find),RectTransform,Canvas,Cameraand TextMeshPro text;Component::is_enabled/set_enabled- value types
Vector2,Vector3(with arithmetic,dot,magnitude,normalized,distance),Quaternion(Quaternion::euler),Color(named constants), and theKeyCodeenum TextOverlay: a line of text drawn over the game in one callfind_objects_of_type::<T>()/find_object_of_type::<T>()andGameObject::get_component/Component::get_componentquery live objects by type (Object.FindObjectsOfType,GetComponent)- lifecycle:
on_scene_loaded(a scene loads),on_update(each frame), andon_render(on the render thread after each camera renders — where to draw an overlay) gl: immediate-mode drawing (GL.Begin/Vertex3/Color/…) for use insideon_renderinput(keys and mouse),screen(size),time,application, andlog/log_warning/log_error(Debug.Log, to the game'sPlayer.log)
Drawing on top of the game
on_render runs your callback in Unity's render pipeline (it hooks
Camera.FireOnPostRender, which the engine calls from native code per camera render). From there
you can draw with the gl immediate-mode module.
A plain gl draw sits under the game's screen-space UI, which the engine composites after the
cameras. To draw on top of it, use an OverlaySurface: it renders your gl drawing into a
RenderTexture shown by a top-most screen-space-overlay canvas, following the screen size and
surviving scene loads. (OverlayCanvas is the bare top-most canvas, and TextOverlay a line of
text built on it.)
For a full UI, enable the egui feature and call egui::on_egui(|ctx| { … }): it renders an
egui interface onto an OverlaySurface, above the game's own UI
and taking mouse input. See examples/egui_demo.rs.
Each Unity method is looked up the first time it's used and checked against the game's real
signature, so a game built with a different Unity version gives an error instead of a crash.
check_bindings checks them all at once.
use OnceLock;
use ;
static OVERLAY: = new;
static LOAD: extern "C" fn = load;
extern "C"
Unity's API may only be used from its main thread, which hooks of methods the game calls there
(like on_scene_loaded's callbacks) run on.
Examples
These are mods for Megabonk's Linux build. Build one, then add
LD_PRELOAD=/path/to/target/release/examples/lib<name>.so %command% to the game's Steam launch
options. The egui_demo and megabonk mods need the egui feature.
scene_overlay: draws a live overlay over the game with the current scene, scenes loaded and a smoothed FPS, refreshed every frame viaon_update. Works in most Unity games with TextMeshPro.render_overlay: draws a crosshair and a panel over the game withGLimmediate mode from theon_renderhook (using aHidden/Internal-Coloredmaterial), toggled with F1.egui_demo: an egui window over the game (a time-scale slider driven by the liveTime.timeScale, a text field and a scroll area), toggled with F1 — theeguifeature.heal_mod: doubles healing by hooking the game's ownPlayerHealth.Heal, reads the player'shpfield and logs toPlayer.log.megabonk: an egui mod menu with godmode (skipsPlayerHealth.Damage) and item spawning (callsItemInventory.AddItem), capturing the live inventory from its per-frameTick— theeguifeature.
Logging
Errors, debug and trace messages (the game binary being found, il2cpp_init, hooks being
installed and removed, lookups) go through tracing. Nothing
is printed unless the mod installs a subscriber, e.g. with tracing-subscriber:
let filter = try_from_default_env.unwrap_or_else;
let _ = fmt.with_env_filter.with_writer.try_init;
Then RUST_LOG=hao_il2cpp_runtime=debug in the game's launch options shows what hao is doing.
Documentation
The API docs are on docs.rs. The crate page lists the wrapped Unity types; the modules are:
time, screen, input, application |
Unity's static classes |
gl |
immediate-mode drawing with UnityEngine.GL |
egui |
an egui overlay (the egui feature) |
dispatch |
run code on the main thread |
| Feature | |
|---|---|
egui |
the egui overlay |
reflect |
facet reflection roots, as in hao |
Tests
HAO_IL2CPP_GAME=/path/to/game cargo test -p hao-il2cpp-unity starts the game's Il2Cpp runtime
without Unity and checks every wrapped method exists with the expected signature, and (for
Megabonk) that the heal_mod example installs its hook. Without the variable the tests skip.