This project contains C# bindings for egui. egui (pronounced "e-gooey") is a simple, fast, and highly portable immediate mode GUI library written in Rust.
Egui.NET aims to provide safe, idiomatic C# bindings that mirror the Rust API as closely as possible.
Egui.NET can be used anywhere you can draw textured triangles, which means you can easily integrate it into your game engine of choice.
For further information, please see the primary egui README.
ui.Heading("My egui Application");
ui.Horizontal(ui =>
{
ui.Label("Your name:");
ui.TextEditSingleline(ref name);
});
ui.Add(new Slider<int>(ref age, 0, 120).Text("age"));
if (ui.Button("Increment").Clicked)
{
age += 1;
}
ui.Label($"Hello '{name}', age {age}");
ui.Image(EguiHelpers.IncludeImageResource("csharp.png"));Egui.NET is available as a Nuget package. Use the following command to add it to your project:
dotnet add package Egui Then, create a Context and begin drawing UI as described in the egui README.
To use Egui.NET, the consumer is responsible for handling user input and rendering. To make this easy, Egui.NET comes with several integration libraries:
egui_net_ffi: support for passing Egui.NET data to Rust, for use with any Rusteguiintegration (likeegui-winit)Egui.Silk.NET.OpenGL: support for rendering toSilk.NETwindows via OpenGL (thanks to@sjoerdev)- The
SilkOpenGlexample demonstrates how to incorporate Egui.NET using this backend
- The
Need to add Egui.NET somewhere else? Adding a new egui integration is easy.
Egui.NET aims to expose the entirety of egui's functionality. Although 97% of bindings are complete, there are currently a few things missing:
- Functions requiring
'staticcallbacks such asStyleModifierorGrid::with_row_color. These functions are difficult to bind because Rust-side code must own a C# closure - Custom
BytesLoaders,ImageLoaders, andTextureLoaders - Persistence (the ability to save a
Contextto disk and load it later) - Accessibility and screen reader support with
accesskit
Egui.NET is designed so that:
- The bindings are completely safe; they cannot cause undefined behavior
- The bindings closely mirror the Rust API, while taking advantage of C# features like properties
- The bindings are easy to update for new
eguireleases
To this end, Egui.NET uses an autobinder to generate approximately 75% of the packaged code. The remaining bindings are written by hand, to deal with corner cases and make the library cleaner. Using a json file produced by rustdoc, the autobinder generates a list of all functions exposed by ecolor/egui/emath/epaint. The autobinder generates Rust-side and C#-side code for as many of these functions as possible. In addition, the autobinder uses serde-generate to emit C# type definitions for serializable egui types. Because the egui API is largely functional (with many plain-old-data types) this covers a large swath of the library.
To ensure safe communication between C# and Rust, FFI is performed using bincode serialization. When C# makes an egui function call at runtime, the arguments and return value are serialized across the language boundary.
The project is organized in the following way:
- docs - contains files for customizing the DocFX output
- Egui - contains the C# project definition and manually-written C# bindings
- egui_net - contains the runtime
eguicrate that is compiled to adyliband loaded by C#.- progress_report.txt - describes what percentage of the API is complete, and identifies functions that have yet to be bound. Generated every time
cargo testis run
- progress_report.txt - describes what percentage of the API is complete, and identifies functions that have yet to be bound. Generated every time
- egui_net_bindgen - helper crate that contains the autobinder. Leverages
rustdocandserde-generateto bind most of egui automatically. The bindings are written totarget/bindingswhenevercargo buildis run - examples - example programs demonstrating how to use the library in C#
- media - images and other media used in documentation
- serde-generate - a fork of the
serde-generatecrate, with some customizations for the Egui.NET API
The project also compiles a fork of egui. The main purpose of the fork is to mark additional egui types as serializable, so that they may be shared with C# easily.
aarch64-pc-windows-msvcx86_64-pc-windows-msvcaarch64-apple-darwinx86_64-apple-darwinaarch64-unknown-linux-gnux86_64-unknown-linux-gnu
To build from source, the following dependencies are required:
.NET 9.0 SDKRustcross-rs(for cross-compiling the Nuget package)Docker(for cross-compiling the Nuget package)
Once installed, the following commands should be available:
cargo build- generate the Rust library for the current platformcargo test- build and update theprogress_report.txtdotnet build Egui- build the Egui.NET library for the current platformdotnet build Egui -c Release- build the Egui.NET library in release mode for all platformsdotnet pack- generate a Nuget package for all platforms