bevy-ecs-expert
它能做什么
这份指南介绍如何在 Rust 语言中,使用 Bevy 引擎面向数据的 ECS 结构开发高性能游戏。你将学习如何组织代码模块(系统)、优化数据读取(查询)、管理全局信息(资源),以及让多个部分并发运行(并行处理)。
适用场景
- ✓当你用 Rust 语言的 Bevy 引擎开发游戏时。
- ✓当你设计需要并行运行的游戏模块时。
- ✓当你想通过高效读取数据来优化游戏性能时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/bevy-ecs-expert/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我想用 Bevy ECS 让角色在屏幕上移动,应该怎么做?”
每周 5 个新技能,1 分钟读完。
工作原理
Bevy ECS 把游戏拆分为“组件”(简单的数据)、“系统”(处理这些数据的函数)和“资源”(全局数据)。它负责管理这些系统之间如何交互和运行,从而实现性能优化。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Bevy ECS Expert
Overview
A guide to building high-performance game logic using Bevy's data-oriented ECS architecture. Learn how to structure systems, optimize queries, manage resources, and leverage parallel execution.
When to Use This Skill
- Use when developing games with the Bevy engine in Rust.
- Use when designing game systems that need to run in parallel.
- Use when optimizing game performance by minimizing cache misses.
- Use when refactoring object-oriented logic into data-oriented ECS patterns.
Step-by-Step Guide
1. Defining Components
Use simple structs for data. Import bevy::prelude::*; add Reflect only when reflection/inspection is needed. The snippets are alternatives and do not duplicate Player/Velocity definitions in one scope.
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct Velocity {
x: f32,
y: f32,
}
#[derive(Component)]
struct Player;
2. Writing Systems
Systems are regular Rust functions that query components.
fn movement_system(
time: Res<Time>,
mut query: Query<(&mut Transform, &Velocity), With<Player>>,
) {
for (mut transform, velocity) in &mut query {
transform.translation.x += velocity.x * time.delta_secs();
transform.translation.y += velocity.y * time.delta_secs();
}
}
3. Managing Resources
Use Resource for global data (score, game state).
#[derive(Resource, Default)]
struct GameState {
score: u32,
}
fn score_system(mut game_state: ResMut<GameState>) {
game_state.score += 10;
}
The toy score_system increments each Update; use gameplay events or an explicit timer for meaningful scoring, rather than implying frame-rate-independent score behavior.
4. Scheduling Systems
Add systems to the App builder, defining execution order if needed.
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.init_resource::<GameState>()
.add_systems(Update, (movement_system, score_system).chain())
.run();
}
Examples
Example 1: Spawning Entities with Require Component
use bevy::prelude::*;
#[derive(Component, Reflect, Default)]
#[require(Velocity, Sprite)]
struct Player;
#[derive(Component, Default)]
struct Velocity {
x: f32,
y: f32,
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
Player,
Velocity { x: 10.0, y: 0.0 },
Sprite::from_image(asset_server.load("player.png")),
));
}
Example 2: Query Filters
Use With and Without to filter entities efficiently.
fn enemy_behavior(
query: Query<&Transform, (With<Enemy>, Without<Dead>)>,
) {
for transform in &query {
// Only active enemies processed here
}
}
Best Practices
- ✅ Do: Use
Queryfilters (With,Without,Changed) to reduce iteration count. - ✅ Do: Prefer
ResoverResMutwhen read-only access is sufficient to allow parallel execution. - ✅ Do: Use
Bundleto spawn complex entities atomically. - ❌ Don't: Store heavy logic inside Components; keep them as pure data.
- ❌ Don't: Use
RefCellor interior mutability inside components; prefer normal ECS borrowing; any interior-mutability design still needs valid Send/Sync and a clear synchronization reason.
Troubleshooting
Problem: System panic with "Conflict" error. Solution: The scheduler normally orders conflicting systems. Bevy B0001 often concerns incompatible queries inside one system; use disjoint With/Without filters or ParamSet as appropriate. Chaining two systems does not repair conflicting parameters inside a system. See Bevy B0001.
Limitations
- Pin the project’s Bevy version and check its migration/API guide. Current examples use delta_secs and required components; older Bevy versions may differ. Enemy/Dead are application-owned component markers and player.png is an application asset, not a bundled file. No engine compilation/performance benchmark is claimed here.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
常见问题
什么是“组件”(Components)?
组件就像一个个小盒子,存放游戏对象的数据,比如位置、速度或生命值。在 Rust 中,它们通过 struct 来定义。
什么是“系统”(Systems)?
系统是让游戏“动起来”的函数。它们读取多个对象的组件并修改数据,比如根据速度移动角色。
什么是“资源”(Resources)?
资源是属于整个游戏的数据,而不是某个特定对象的数据。例如玩家的分数,或当前的游戏状态(比如“暂停”或“进行中”)。
详情
作者的原始描述
Master Bevy's Entity Component System (ECS) in Rust, covering Systems, Queries, Resources, and parallel scheduling.
热度
- GitHub 星标
- 48k
- 排名
- #4029
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。