From 79e73738c74a451e078c19b1bfbbee4c2d3a4f33 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Sat, 19 Oct 2024 23:52:33 +0100 Subject: [PATCH] Optional UI rendering (#8997) # Objective Make UI rendering optional. Quite a few people have been experimenting with using Bevy UI for layout and interaction but replacing the rendering with `bevy_prototype_lyon` or whatever. It's awkward to do though as there is no way to disable the existing UI rendering requiring users to create their own custom node bundles and components. Also, you can't replace the UI's shader and a number of other things. This PR makes the setup and initialization of UI rendering for the RenderApp optional. Then you can do whatever you want by replacing `build_ui_render` with your own function. For instance, one that loads a custom shader. The UI layout and interaction components are still updated as normal. ## Solution Add a field `enable_rendering` to `UiPlugin`. Only call `build_ui_render` and initialize the `UiPipeline` if `enable_rendering` is false. I thought about implementing a "bevy_ui_render" feature but suspect everything is too tightly coupled atm and it would be very fragile. Similar to the struggles with the "bevy_text" feature but worse. --- ## Changelog `UiPlugin` * Added a bool field `enable_rendering`. * Only calls `build_ui_render` and initializes the `UiPipeline` if `enable_rendering` is true. ## Migration Guide `UiPlugin` has a new field `enable_rendering`. If set to false, the UI's rendering systems won't be added to the `RenderApp` and no UI elements will be drawn. The layout and interaction components will still be updated as normal. --- crates/bevy_ui/src/lib.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/bevy_ui/src/lib.rs b/crates/bevy_ui/src/lib.rs index 0e19e475f6..658acc9b48 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -81,8 +81,19 @@ pub use stack::UiStack; use update::{update_clipping_system, update_target_camera_system}; /// The basic plugin for Bevy UI -#[derive(Default)] -pub struct UiPlugin; +pub struct UiPlugin { + /// If set to false, the UI's rendering systems won't be added to the `RenderApp` and no UI elements will be drawn. + /// The layout and interaction components will still be updated as normal. + pub enable_rendering: bool, +} + +impl Default for UiPlugin { + fn default() -> Self { + Self { + enable_rendering: true, + } + } +} /// The label enum labeling the types of systems in the Bevy UI #[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)] @@ -205,16 +216,23 @@ impl Plugin for UiPlugin { .in_set(AmbiguousWithUpdateText2DLayout), ), ); - build_text_interop(app); - build_ui_render(app); - #[cfg(feature = "bevy_ui_picking_backend")] app.add_plugins(picking_backend::UiPickingBackendPlugin); + + if !self.enable_rendering { + return; + } + + build_ui_render(app); } fn finish(&self, app: &mut App) { + if !self.enable_rendering { + return; + } + let Some(render_app) = app.get_sub_app_mut(RenderApp) else { return; };