Skip to main content

Crate hao_il2cpp_unity

Crate hao_il2cpp_unity 

Source
Expand description

Typed Unity wrappers for mods of Unity Il2Cpp games, on top of hao_il2cpp_runtime.

The runtime crate finds and hooks methods by name. This crate wraps common Unity functionality the same way for any Unity game: game objects and components, UI, scenes, logging and time. Each Unity method is looked up the first time it’s used and checked against the game’s real signature, so a game whose Unity version differs gives an error rather than a crash.

use std::sync::OnceLock;

use hao_il2cpp_unity::{on_scene_loaded, TextOverlay};

static OVERLAY: OnceLock<TextOverlay> = OnceLock::new();

hao_il2cpp_unity::on_init(|_| {
    on_scene_loaded(|scene, _| {
        let overlay = OVERLAY.get_or_init(|| TextOverlay::new("my mod").unwrap());
        overlay.set_text(&format!("scene: {}", scene.name().unwrap())).unwrap();
    })
});

Unity’s API may only be used from its main thread: inside hooks of methods the game calls on it, such as on_scene_loaded’s callbacks.

Re-exports§

pub use hao_il2cpp_runtime as runtime;

Modules§

application
UnityEngine.Application.
egui
egui integration (requires the egui feature). See on_egui.
gl
Immediate-mode drawing with UnityEngine.GL, for use inside an on_render callback (it’s only valid during rendering).
input
UnityEngine.Input (the legacy input manager).
reflect
Reflection roots for the reflect feature. See hao::reflect for the pattern. The overlay and binding handles hold engine pointers and are not reflected; the plain-data types are.
screen
UnityEngine.Screen.
time
Time.

Structs§

Camera
UnityEngine.Camera, passed to on_render callbacks.
Canvas
UnityEngine.Canvas, the root of a UI.
Color
UnityEngine.Color, with components from 0 to 1.
Component
UnityEngine.Component.
FilterMode
UnityEngine.FilterMode, how a texture is sampled. POINT samples the nearest texel exactly (no blending with neighbours).
GameObject
UnityEngine.GameObject.
HideFlags
UnityEngine.HideFlags. HIDE_AND_DONT_SAVE keeps a runtime-created object (e.g. a Material) from being destroyed on scene load.
KeyCode
UnityEngine.KeyCode, for input. A few common keys are named; any other can be made with KeyCode(value).
LoadSceneMode
UnityEngine.SceneManagement.LoadSceneMode.
Material
UnityEngine.Material, e.g. for a gl drawing pass.
OverlayCanvas
A screen-space-overlay Canvas drawn on top of the game’s own UI (the maximum sorting order) and kept across scene loads. Parent UI elements, or a RawImage, under game_object.
OverlaySurface
A full-screen off-screen target shown on top of the game’s UI, for drawing an overlay with the gl module.
Quaternion
UnityEngine.Quaternion, a rotation. Use Quaternion::euler to build one.
RawImage
UnityEngine.UI.RawImage, a UI element that draws a raw Texture2D or RenderTexture.
RectTransform
UnityEngine.RectTransform, the transform of UI elements.
RenderMode
UnityEngine.RenderMode, how a Canvas is drawn.
RenderTexture
UnityEngine.RenderTexture, an off-screen render target.
Scene
UnityEngine.SceneManagement.Scene.
Shader
UnityEngine.Shader.
TextMeshPro
TMPro.TextMeshProUGUI, TextMeshPro text in a UI.
TextOverlay
A line of TextMeshPro text on its own OverlayCanvas, drawn over the game’s UI and kept across scene loads. Positioned from the top left of the screen.
Texture2D
UnityEngine.Texture2D.
TextureFormat
UnityEngine.TextureFormat.
Transform
UnityEngine.Transform: position in the scene and the parent/child hierarchy.
UnityObject
UnityEngine.Object, the base of everything Unity manages.
Vector2
UnityEngine.Vector2.
Vector3
UnityEngine.Vector3.

Traits§

ComponentType
A component type, for GameObject::add_component.

Functions§

check_bindings
Resolves every wrapped Unity method against the running game, returning the ones that are missing or have a different signature.
find_object_of_type
The first active object of type T, or None (Object.FindObjectOfType).
find_objects_of_type
Every active object of type T in the loaded scenes (Object.FindObjectsOfType).
log
Debug.Log: writes to the game’s log (Player.log).
log_error
Debug.LogError.
log_warning
Debug.LogWarning.
on_init
Runs callback once il2cpp_init has finished, or straight away if it already has.
on_render
Runs callback on the render thread after each camera finishes rendering, passing the Camera. This is where to draw an overlay on top of the game with gl, or to drive an immediate-mode UI such as egui.
on_scene_loaded
Runs callback on Unity’s main thread after each scene loads, which is a good time to add objects to the game. Panics are caught and printed.
on_update
Runs callback once per frame on Unity’s main thread, a good place to update an overlay. Panics are caught and printed.