bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.64k stars 3.52k forks source link

UiPlugin cannot be used without rendering #3815

Open alice-i-cecile opened 2 years ago

alice-i-cecile commented 2 years ago

Bevy version

0.6

What you did

use bevy::prelude::*;
use bevy::ui::UiPlugin;

fn main() {
    App::new()
        .add_plugins(MinimalPlugins)
        .add_plugin(UiPlugin)
        .run();
}

What actually happened

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', C:\Users\Alice\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_ui-0.6.0\src\render\mod.rs:62:70

This panic is in https://github.com/bevyengine/bevy/blob/458cb7a9e93dab3127bb99ce7bf8cfc3af18851d/crates/bevy_ui/src/render/mod.rs#L61.

Additional information

Related to #3155.

This is essential for headless integration testing of UI logic.

Wcubed commented 2 years ago

For headless testing of UI, see the example in this pull request for adding headless windows: #3835

YourAlly commented 2 years ago

Just started learning Bevy and Rust, where should I start for this issue?

alice-i-cecile commented 2 years ago

Just started learning Bevy and Rust, where should I start for this issue?

Thanks @YourAlly :) So, the steps I would take are:

  1. Reproduce the problem locally.
  2. Identify why this is happening (it appears that the resource is not being added).
  3. Modify the plugin such that this system is not added or compiled if the rendering feature is disabled.
  4. Verify that the problem is fixed.
  5. Open a PR with your changes.

https://doc.rust-lang.org/cargo/reference/features.html is a useful learning resource here; you can see how to use feature flags to conditionally enable compilation of certain code segments.

Feel free to check in with me or report back on this thread if you get stuck.

kirusfg commented 2 years ago

With my PR, the example provided simply compiles. I am not sure what UI functionalities must work in a headless mode, hence provided no tests. Disregard, what I did was horribly wrong.

kirusfg commented 2 years ago

The simple UI test provided in #3835 (check if button is centered in a window without a window) passes with rendering disabled by preventing

from loading when UIPlugin is added. These systems heavily depend on textures: Res<Assets<Image>>; that is the reason TextPlugin will not work without rendering, too.

I bet those Res<Assets<Image>> can be Options to allow for "imaginary" UI text and images that still can be tested for correct positioning, but simply are not rendered. If so, is this the correct way of solving the issue?

alice-i-cecile commented 2 years ago

I bet those Res<Assets> can be Options to allow for "imaginary" UI text and images that still can be tested for correct positioning, but simply are not rendered. If so, is this the correct way of solving the issue?

Yep, that seems promising!

JMS55 commented 1 month ago

Not just without rendering, but it would be great to be able to use UI with a custom non-wgpu based renderer.

That way projects can use bevy_ui and bevy_ui-based crates without any logic, styling, event handling, etc changes, and without needing to write their own UI and layout system, but still being able to use their own renderer and just write a simple system to query the list of finalized nodes to build draw calls for.