跳转到内容

蓝图可视化脚本 (Blueprint)

@esengine/blueprint 提供与 ECS 框架深度集成的可视化脚本系统,支持通过节点式编程控制实体行为。

Cocos Creator 蓝图编辑器插件(免费):

下载 Cocos Node Editor v1.2.0

技术交流 QQ 群:481923584 | 官网:esengine.cn

详细使用教程请参考 编辑器使用指南

Terminal window
npm install @esengine/blueprint
  • ECS 深度集成 - 内置 Entity、Component 操作节点
  • 组件自动节点生成 - 使用装饰器标记组件,自动生成 Get/Set/Call 节点
  • 运行时蓝图执行 - 高效的虚拟机执行蓝图逻辑
import { Scene, Core } from '@esengine/ecs-framework';
import { BlueprintSystem } from '@esengine/blueprint';
// 创建场景并添加蓝图系统
const scene = new Scene();
scene.addSystem(new BlueprintSystem());
// 设置场景
Core.setScene(scene);
import { BlueprintComponent } from '@esengine/blueprint';
// 创建实体
const player = scene.createEntity('Player');
// 添加蓝图组件
const blueprint = new BlueprintComponent();
blueprint.blueprintAsset = await loadBlueprintAsset('player.blueprint.json');
blueprint.autoStart = true;
player.addComponent(blueprint);

3. 标记组件(自动生成蓝图节点)

Section titled “3. 标记组件(自动生成蓝图节点)”
import {
BlueprintExpose,
BlueprintProperty,
BlueprintMethod
} from '@esengine/blueprint';
import { Component, ECSComponent } from '@esengine/ecs-framework';
@ECSComponent('Health')
@BlueprintExpose({ displayName: '生命值', category: 'gameplay' })
export class HealthComponent extends Component {
@BlueprintProperty({ displayName: '当前生命值', type: 'float' })
current: number = 100;
@BlueprintProperty({ displayName: '最大生命值', type: 'float' })
max: number = 100;
@BlueprintMethod({
displayName: '治疗',
params: [{ name: 'amount', type: 'float' }]
})
heal(amount: number): void {
this.current = Math.min(this.current + amount, this.max);
}
@BlueprintMethod({ displayName: '受伤' })
takeDamage(amount: number): boolean {
this.current -= amount;
return this.current <= 0;
}
}

标记后,蓝图编辑器中会自动出现以下节点:

  • Get Health - 获取 Health 组件
  • Get 当前生命值 - 获取 current 属性
  • Set 当前生命值 - 设置 current 属性
  • 治疗 - 调用 heal 方法
  • 受伤 - 调用 takeDamage 方法
┌─────────────────────────────────────────────────────────────┐
│ Core.update() │
│ ↓ │
│ Scene.updateSystems() │
│ ↓ │
│ ┌───────────────────────────────────────────────────────┐ │
│ │ BlueprintSystem │ │
│ │ │ │
│ │ Matcher.all(BlueprintComponent) │ │
│ │ ↓ │ │
│ │ process(entities) → blueprint.tick() for each entity │ │
│ │ ↓ │ │
│ │ BlueprintVM.tick(dt) │ │
│ │ ↓ │ │
│ │ Execute Event/ECS/Flow Nodes │ │
│ └───────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
类别说明颜色
event事件节点(BeginPlay, Tick, EndPlay)红色
entityECS 实体操作蓝色
componentECS 组件访问青色
flow流程控制(Branch, Sequence, Loop)灰色
math数学运算绿色
time时间工具(Delay, GetDeltaTime)青色
debug调试工具(Print)灰色

蓝图保存为 .blueprint.json 文件:

interface BlueprintAsset {
version: number;
type: 'blueprint';
metadata: {
name: string;
description?: string;
};
variables: BlueprintVariable[];
nodes: BlueprintNode[];
connections: BlueprintConnection[];
}