KBVE game logic to Unity viauniti
A Rust C FFI library that exposes KBVE game logic — flow-field pathfinding, slot inventory, a SQLite-backed world store and an empire snapshot ticker — to a Unity runtime as a native plugin.
One native plugin
uniti builds as a cdylib, a staticlib and an rlib, bundling the game-logic crates plus SQLite and zstd so Unity needs nothing on the host.
- Logic — bevy_pathfinder and bevy_inventory, built without Bevy.
- Helpers — bundled rusqlite, zstd, and a shared tokio runtime.
What it gives you
Features
Pathfinding and inventory
Grid flow-field navigation from bevy_pathfinder and slot-based inventory from bevy_inventory, behind opaque handles.
Persistent world store
Ghost-chunk state for hexes, units and buildings that diverged from world-gen, ticked on a background thread and flushed to SQLite every 30 seconds.
Empire ticker
Unity publishes an EmpireSnapshot; a background tick decodes it with prost, drifts each city's mood and status, and hands the bytes back.
No host dependencies
Statically bundles rusqlite (bundled feature) and zstd, so Unity loads the library without host libraries.
The bridge
Overview
uniti is the C FFI bridge that exposes KBVE game logic to a Unity runtime, written for the Unity build of RareIcon. It compiles as a cdylib (desktop DllImport), a staticlib (iOS static link) and an rlib for its integration tests.
Every export is a pub unsafe extern "C" fn over opaque handles returned by a matching uniti_*_new / uniti_*_open constructor; a handle must still be live and must not be used from two threads at once.
Modules & build
Modules, build & bindings
Modules
| Module | Purpose |
|---|---|
ffi_pathfinding | Grid + flow-field navigation over bevy_pathfinder |
ffi_inventory | Slot-based inventory over bevy_inventory |
ffi_world | Persistent ghost-chunk world store, ticked in the background and flushed to SQLite |
ffi_empire | Empire snapshot publish / tick / take, decoded with prost from kbve-proto |
bevy_battle and bevy_skills are linked as dependencies but have no FFI surface yet. Two runtime helpers are compiled in so Unity does not need them on the host:
rusqlitewith thebundledfeature — world-store persistence, no systemlibsqlite3dep.zstdwith default features disabled — compression for save blobs.
Build and test
moon run uniti:buildmoon run uniti:testmoon run uniti:lintThe integration tests under tests/ cover world lifecycle, migrations, concurrency, safety and property tests, and benches/ffi_world_bench.rs measures the world store.
Bindings
The crate documents csbindgen as the generator of the Unity-side Uniti.g.cs, but there is no build.rs or csbindgen dependency in the crate today, and the Unity project it wrote into is no longer in the monorepo.
License
MIT
Questions
Frequently asked
What is the uniti crate?
uniti is the C FFI bridge that exposes KBVE game logic to a Unity runtime, written for the Unity build of RareIcon. It compiles as a cdylib for desktop DllImport, a staticlib for iOS static linking, and an rlib for its own integration tests. Its exports cover flow-field pathfinding (bevy_pathfinder), slot inventory (bevy_inventory), a persistent world store and an empire snapshot ticker.
How are the C# bindings produced?
Every export is a pub unsafe extern "C" fn over opaque handles from a matching uniti_*_new or uniti_*_open constructor. The crate documents csbindgen as the generator of the Unity-side Uniti.g.cs, but this crate has no build.rs and no csbindgen dependency today, and the Unity project is no longer in the monorepo, so nothing here generates the C# side.
Does uniti need system libraries on the host?
No. It statically bundles rusqlite with the bundled feature (so no system libsqlite3 is required) and zstd with default features disabled, and pulls the bevy crates with default-features off so no Bevy app stack is compiled in.
Discuss on the forum
Questions, fixes and notes on this page live in the KBVE forum.