17个稳定版本
3.3.0 | 2024年7月7日 |
---|---|
3.2.1 | 2024年5月2日 |
3.2.0 | 2024年3月21日 |
3.1.0 | 2023年11月16日 |
1.3.0 | 2023年5月10日 |
#244 in 游戏开发
142 每月下载量
42KB
426 行
bevy_health_bar3d
Bevy 3D的生命条插件。尽管其名称如此,此插件具有通用性。它可以用于渲染任何可以表示为百分比的值的条。可以自由调整大小,支持水平和垂直方向,自定义前景和背景颜色,以及可选的边框,可配置厚度和颜色。与分屏或分层相机无缝配合。
Bevy兼容性
Bevy版本 | 包版本 |
---|---|
0.14 |
>= 3.3.0 |
0.13 |
3.2.0 |
0.12 |
2.0.0 |
0.11 |
1.2.0 |
0.10 |
1.1.0 |
0.9 |
1.0.0 |
使用方法
为您要跟踪的组件实现Percentage
特质,并在实例化插件时传递组件的类型。
use bevy_health_bar3d::prelude::{HealthBarPlugin, Percentage};
#[derive(Component, Reflect)]
struct Health {
max: f32,
current: f32,
}
impl Percentage for Health {
fn value(&self) -> f32 {
self.current / self.max
}
}
fn main() {
App::new()
// add multiple times to track further component types
.add_plugins((HealthBarPlugin::<Health>::default(), HealthBarPlugin::<Mana>::default()))
// set a different color for the Mana bar
.insert_resource(ColorScheme::<Mana>::new().foreground_color(ForegroundColor::Static(Color::BLUE)))
.run();
}
创建一个网格、要跟踪的组件和一个BarSettings
组件来配置条的外观和感觉。
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
PbrBundle {
mesh: meshes.add(Sphere { radius }),
// ...
},
Health {
max: 10.,
current: 2.,
},
BarSettings::<Health> {
width: 5.,
offset: 2.,
orientation: BarOrientation::Vertical, // default is horizontal
..default()
},
));
}
注意BarBundle
的泛型参数。它用于将配置与跟踪的组件关联起来,并且对于每个实体支持多个条是必要的。
就这样!组件值的更新将自动传播到条上。
示例
示例可以在这里找到。要运行Web上的示例,首先安装cargo-make (cargo install cargo-make
),然后调用cargo make web <示例名称
,例如cargo make web dinosaurs
依赖项
~37–74MB
~1.5M SLoC