bevy/examples/app/plugin_group.rs
François 8cdd977a12 Unique plugin (#6411)
# Objective

- Make it impossible to add a plugin twice
- This is going to be more a risk for plugins with configurations, to avoid things like `App::new().add_plugins(DefaultPlugins).add_plugin(ImagePlugin::default_nearest())`

## Solution

- Panic when a plugin is added twice
- It's still possible to mark a plugin as not unique by overriding `is_unique`
- ~~Simpler version of~~ #3988 (not simpler anymore because of how `PluginGroupBuilder` implements `PluginGroup`)
2022-10-31 16:12:19 +00:00

57 lines
1.6 KiB
Rust

//! Demonstrates the creation and registration of a custom plugin group.
//! [`PluginGroup`]s are a way to group sets of plugins that should be registered together.
use bevy::{app::PluginGroupBuilder, prelude::*};
fn main() {
App::new()
// Two PluginGroups that are included with bevy are DefaultPlugins and MinimalPlugins
.add_plugins(DefaultPlugins)
// Adding a plugin group adds all plugins in the group by default
.add_plugins(HelloWorldPlugins)
// You can also modify a PluginGroup (such as disabling plugins) like this:
// .add_plugins(
// HelloWorldPlugins
// .build()
// .disable::<PrintWorldPlugin>()
// .add_before::<PrintHelloPlugin, _>(
// bevy::diagnostic::LogDiagnosticsPlugin::default(),
// ),
// )
.run();
}
/// A group of plugins that produce the "hello world" behavior
pub struct HelloWorldPlugins;
impl PluginGroup for HelloWorldPlugins {
fn build(self) -> PluginGroupBuilder {
PluginGroupBuilder::start::<Self>()
.add(PrintHelloPlugin)
.add(PrintWorldPlugin)
}
}
pub struct PrintHelloPlugin;
impl Plugin for PrintHelloPlugin {
fn build(&self, app: &mut App) {
app.add_system(print_hello_system);
}
}
fn print_hello_system() {
info!("hello");
}
pub struct PrintWorldPlugin;
impl Plugin for PrintWorldPlugin {
fn build(&self, app: &mut App) {
app.add_system(print_world_system);
}
}
fn print_world_system() {
info!("world");
}