Latest commit b724a0f
use bevy::{prelude::*, utils::Duration}; /// 插件是 Bevy 的基础 /// 插件是组件、资源和系统的范围集,提供了特定功能(范围越小越好) /// 这个示例用来演示自定义插件如何被创建和注册 fn main() { App::new() .add_plugins(DefaultPlugins) // 注册插件是“构建程序(app building)”过程的一部分 .add_plugin(PrintMessagePlugin { wait_duration: Duration::from_secs(1), message: "这是一个插件示例。".to_string(), }) .run(); } // 这个“打印消息插件”每隔一段时间打印一次消息 pub struct PrintMessagePlugin { // 在这里放置插件配置 wait_duration: Duration, message: String, } impl Plugin for PrintMessagePlugin { // 在这里设置插件 fn build(&self, app: &mut App) { let state = PrintMessageState { message: self.message.clone(), timer: Timer::new(self.wait_duration, true), }; app.insert_resource(state).add_system(打印消息); } } struct PrintMessageState { message: String, timer: Timer, } fn 打印消息(mut state: ResMut<PrintMessageState>, time: Res<Time>) { if state.timer.tick(time.delta_seconds()).finished() { info!("{}", state.message); } }