1个不稳定版本
0.1.0 | 2023年8月29日 |
---|
#1762 在 游戏开发
17KB
76 行
bevy_cleanup
为在Bevy中使用清理设计模式提供辅助工具,其中实体在状态转换后自动移除,取决于它们拥有的任何Cleanup
标记组件。
use bevy::prelude::*;
use bevy_cleanup::{Cleanup, AddStateCleanup};
// Set up your States enum and keep your Cleanup component types close by
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, States)]
enum AppState {
#[default]
Menu,
Game,
}
#[derive(Component, Cleanup)]
struct CleanupMenu;
#[derive(Component, Cleanup)]
struct CleanupGame;
// Set up your App
fn main() {
App::new()
.add_state::<AppState>()
.add_state_cleanup::<_, CleanupMenu>(AppState::Menu)
.add_state_cleanup::<_, CleanupGame>(AppState::Game)
.add_systems(OnEnter(AppState::Menu), setup_menu)
.run();
}
fn setup_menu(mut commands: Commands) {
// When spawning an entity, give it one of your `Cleanup` component types
// Typically, you put the Name and Cleanup types at the top of the component tuple
commands.spawn((
Name::new("Menu entity"),
CleanupMenu,
// everything else...
));
}
// When you want to switch from Menu to Game...
fn switch_to_game(mut next_state: ResMut<NextState<AppState>>) {
// After this, any entities with the cleanup component of the current state (`CleanupMenu`)
// will be automatically recursively despawned.
next_state.set(AppState::Game);
}
许可证
许可方式为以下之一
- Apache许可证2.0版本 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任选其一。
除非您明确声明,否则您有意提交以包含在工作中的任何贡献,根据Apache-2.0许可证的定义,应按上述方式双许可,不附加任何额外的条款或条件。
依赖
~20–29MB
~425K SLoC