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.
This commit is contained in:
ickshonpe 2024-10-19 23:52:33 +01:00 committed by GitHub
parent 8f31c09e60
commit 79e73738c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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;
};