JEV Unity 教程:构建实时战术 NPC

围绕 JEV 构建 Unity NPC 架构,具有紧凑状态 可执行动作、决策节奏、执行器、超时处理和可观察的 兜底策略。

Seele Editorial TeamUpdated 2026年9月21日
Unity-style tactical NPC scene with perception, decision, navigation, and fallback layers.

_

这个 Unity 教程使用 JEV 作为有界战术决策层,而 Unity 仍然负责场景状态、物理、导航、动画和游戏权限。它是一种适配器模式,而不是声称一个 SDK 调用可以替换 NPC 控制器。_

Unity 架构

NpcController -> StateProjector -> LegalActionBuilder -> JevClient -> Validator -> Executor or Fallback

NpcController -> StateProjector -> LegalActionBuilder -> JevClient -> Validator -> Executor or Fallback

1。将场景投影到

state = { agentId, role, healthRatio, visibleThreatIds, objectiveUnderThreat, ammo, stateVersion }

state = { agentId, role, healthRatio, visibleThreatIds, objectiveUnderThreat, ammo, stateVersion }

2。构建

动作生成器创建诸如隐藏、攻击可见目标、协助盟友或坚守阵地等命令。每个动作都携带执行者所需的目标或位置标识符。导航仍然是 Unity 关注的问题:JEV 选择意图或目的地候选者,而 NavMesh 计算路径.

3。在渲染循环之外调用

if (!requestInFlight) { requestInFlight = true; result = await client.Decide(snapshot, actions); ApplyIfStillValid(result); }

if (!requestInFlight) { requestInFlight = true; result = await client.Decide(snapshot, actions); ApplyIfStillValid(result); }

4。验证并执行_

将响应的状态版本与当前世界进行比较,重建合法操作集并验证参数。当请求正在执行时,目标可能已经死亡或者掩体点可能无法到达。如果验证成功,则将意图映射到现有命令、行为树、NavMesh 目标或能力系统。

5。设置节奏、超时和

将渲染循环与战术循环分开。设置最后期限,让 NPC 有时间采取行动。超时时,保持当前的安全意图或使用本地策略,例如受伤时寻找掩护、坚守目标或在没有合法攻击时撤退。

6。让决策可见

_在开发版本中,在调试窗口或小控件中公开预计状态、可执行动作、所选操作、状态版本、请求延迟、验证结果和 兜底策略 原因。设计人员应该在不读取网络跟踪的情况下理解决策。

  • Unity 检查表
  • _感知拥有状态投影。不要阻止主线程.执行前重新验证.将运动、物理、动画和权限保留在Unity.船舶确定性兜底策略和从一开始的遥测原型._
  • 最小Unity场景设置
  • 从一个 NPC、一个目标、两个威胁、几个掩护点和一个本地 兜底策略 控制器开始。添加感知组件、状态投影仪、动作构建器、决策客户端和执行器作为单独的 MonoBehaviour 或服务。使 JEV 适配器独立于 NavMeshAgent 和 Animator 详细信息,以便无需完整场景即可对其进行测试。
  • 适配器边界

_适配器将 Unity 类型转换为提供者中立的请求,并将经过验证的意图转换回现有命令。它不应在验证之前调用

SetDestination

、播放动画或施加损坏。这些效果属于执行器和游戏系统。

调整提示前的测试场景_生命值低,有可用掩护。没有弹药,但可以到达撤退处路线.请求在飞行中被摧毁。两个同样有效的掩护点,具有不同的覆盖点距离.JEV活动战斗状态下超时。场景卸载或NPC 在响应前消失。对于每个场景,断言所选操作是合法的,执行程序最多被调用一次,过时的响应被忽略,并且 兜底策略 使 NPC 处于有效状态。在 Unity 分析器中测量什么

跟踪状态投影时间、序列化时间、网络等待、验证时间、执行器启动、操作完成和 兜底策略 计数。模型可能很快,而适配器却很慢,因为它序列化了太多状态或在主线程上安排了工作。单独测量帧影响和端到端决策延迟。

  • 分阶段运输计划
  • _具有本地虚假决策客户端的原型.记录和重播状态快照.在功能标志后面添加远程客户端.Ship 兜底策略-第一个行为和遥测.在使决策层通用之前运行有限的NPC队列._
  • 对于提供商中立合同,请阅读
  • JEV API 用法
  • 。对于动作空间设计,请参见
  • JEV 。一般教程位于JEV教程_.

For each scenario, assert that the selected action is legal, the executor is called at most once, the stale response is ignored, and the 兜底策略 leaves the NPC in a valid state.

What to measure in the Unity Profiler

Track state projection time, serialization time, network wait, validation time, executor start, action completion, and 兜底策略 count. A model can be fast while the adapter is slow because it serializes too much state or schedules work on the main thread. Measure the frame impact separately from end-to-end decision latency.

A staged shipping plan

  1. Prototype with a local fake decision client.
  2. Record and replay state snapshots.
  3. Add the remote client behind a feature flag.
  4. Ship 兜底策略-first behavior and telemetry.
  5. Run a limited NPC cohort before making the decision layer universal.

For the provider-neutral contract, read JEV API usage. For action-space design, see JEV legal actions. The general tutorial is at JEV tutorial.