播放前验证 (Pre-Play Validation)

播放前验证 (Pre-Play Validation) 系统会在仿真启动前自动检测并解决常见的配置问题,从而减少调试时间并确保自动化系统行为的可靠性。

概述

播放前验证 (Pre-Play Validation) 会主动识别场景配置中的潜在问题,并自动修复或提供清晰的解决指导。该验证系统在开发流程的早期阶段捕获问题,防止虚拟调试 (Virtual Commissioning) 期间的运行时故障和异常行为。

主要优势包括:

  • 自动化问题检测:在配置冲突导致运行时问题之前识别它们
  • 智能自动修复:自动解决常见问题,如冲突的组件设置
  • 清晰的诊断信息:提供具体、可操作的反馈以供手动修复
  • 开发工作流集成:在正常开发活动中无缝运行
  • 减少调试时间:在问题演变为复杂的运行时问题之前捕获它们
  • 在 RealvirtualController 设置中启用 Pre-Play Validation
💡 提示 在项目开发早期启用验证,以建立良好的配置实践,并在问题产生时而非最终测试时捕获它们。

快速入门

启用验证(1 分钟)

  1. 在场景中定位 RealvirtualController(通常在 "Controller" GameObject 上)
  2. 在 Inspector 中检查验证设置
    • Validate Before Start:✅ 启用以进行仿真前检查
    • Validation On Components Added:✅ 启用以进行实时组件验证
  3. 测试系统:启动 Play 模式并检查 Unity Console 中的验证消息

理解验证输出

验证消息以以下格式显示在 Unity Console 中:


[Validation] RuleName: Description of issue and action taken
Context: GameObject "ObjectName"Component type

配置选项

RealvirtualController 设置

Validate Before Start 进入 Play 模式时运行全面的场景验证。

  • 类型:布尔值
  • 默认值:true
  • 用例:在仿真启动前捕获配置问题
  • 性能:对启动时间影响极小

Validation On Components Added 在 Editor 模式下添加组件时立即验证。

  • 类型:布尔值
  • 默认值:true
  • 用例:在场景构建过程中防止无效配置
  • 性能:实时验证,无运行时影响

开发工作流集成

不同阶段的推荐设置

早期开发:两种验证均启用

  • 有助于建立正确的配置模式
  • 在组件添加时捕获问题

生产/测试:启用播放前验证,组件验证可选

  • 确保关键测试前的场景完整性
  • 减少快速开发期间的验证中断

最终发布:可禁用验证以提升性能

  • 移除最终构建中的验证开销
  • 此阶段应已解决所有配置问题
Unity Console 中显示的验证警告
ℹ️

最佳实践:在开发期间保持两种验证均启用。该系统设计为非侵入式,同时为场景配置质量提供有价值的反馈。

验证规则参考

验证系统包含针对工业自动化设置中常见配置问题的专用规则。

控制系统验证

BehaviorInterface 点动冲突解决

检测到的问题:同时具有 BehaviorInterface 控制和手动点动 (Jogging) 的 Drive 组件

重要性

  • BehaviorInterface 组件为自动化序列提供程序化驱动控制
  • 手动点动控制 (JogForward/JogBackward) 用于手动测试和调试
  • 两者同时激活会产生冲突的控制信号和不可预测的行为

自动解决

  • 禁用受影响 Drive 组件上的 JogForwardJogBackward 属性
  • 保留 BehaviorInterface 控制以确保正确的自动化操作
  • 维持驱动功能同时消除控制冲突

示例场景


Conveyor System with BehaviorInterfaceConveyor component:
✅ Before: Drive has jogging enabled + active BehaviorInterfaceProblem: Conflicting control signals cause erratic movement
✅ After: Jogging disabled, BehaviorInterface maintains full control

Console 消息


[Validation] BehaviorInterfaceJoggingRule: Disabled jogging on Drive.
Active BehaviorInterface will control drive position and conflicts with manual jogging.
Context: GameObject "ConveyorDrive"Drive component

多个 BehaviorInterface 防止

检测到的问题:具有多个活动 BehaviorInterface 组件的 GameObject

重要性

  • 每个 GameObject 应仅有一个活动的控制接口
  • 多个接口会产生模糊的控制权限
  • 可能导致时序冲突和同步问题

自动解决

  • 保留第一个活动的 BehaviorInterface 组件
  • 禁用额外的 BehaviorInterface 组件
  • 保留主要控制接口功能

示例场景


Robot Arm with multiple control interfaces:
✅ Before: BehaviorInterfaceRobot + BehaviorInterfaceSequence both active
❌ Problem: Conflicting position commands from two controllers
✅ After: Primary interface remains active, secondary disabled

运动系统验证

单 Drive 组件强制

检测到的问题:同一 GameObject 上的多个 Drive 组件

重要性

  • Unity GameObject 仅支持一个主要运动控制器
  • 多个 Drive 组件会产生冲突的变换更新
  • 可能导致抖动运动、位置争用或完全运动失效

立即解决

  • 添加重复 Drive 组件后立即移除
  • 保留原始 Drive 组件配置
  • 在组件添加时立即触发(不仅限于播放前)

预防策略


Hierarchy Design:
✅ Correct: One Drive per GameObject
  ├── ConveyorSystem (Drive component)
  ├── RobotArm (Drive component)
  └── ElevatorPlatform (Drive component)

❌ Incorrect: Multiple Drives on same GameObject
  ├── ComplexMachine (Drive + Drive) ← Second Drive removed

传输表面层级验证

检测到的问题:不当的 Drive-TransportSurface 层级配置

关键物理要求

  • TransportSurface 组件需要 Drive 控制才能正常工作
  • Unity 物理约束阻止某些 Drive-TransportSurface 排列
  • 缺失 Drive 引用会导致传送带系统无法正常工作

验证逻辑

✅ 有效配置

  • TransportSurface 明确分配了 DriveReference
  • TransportSurface 的父层级中恰好有一个 Drive
  • TransportSurface 作为 Drive GameObject 的子对象

❌ 无效配置

  • TransportSurface 没有 Drive 且没有 DriveReference
  • TransportSurface 上方层级中有多个 Drive
  • Drive 组件位于 TransportSurface 上方违反物理规则

实际示例

Correct Hierarchy 1: Direct Reference
Conveyor
├── Belt (TransportSurface + DriveReference to Drive)
└── Motor (Drive component)

✅ Correct Hierarchy 2: Parent-Child Relationship
ConveyorSystem (Drive component)
└── Belt (TransportSurface component)

❌ Problematic Hierarchy: Missing Drive Control
Conveyor
└── Belt (TransportSurface only) ← No drive control

❌ Physics Violation: Drive Above TransportSurface
FactoryFloor (Drive component)
└── ConveyorSection (TransportSurface) ← Physics conflict

Console 消息


[Validation] TransportSurfaceDriveHierarchyRule: TransportSurface requires a Drive component
in the hierarchy to function properly. Please add a Drive component or assign DriveReference.
Context: GameObject "ConveyorBelt"TransportSurface component

验证触发器

播放前验证

进入 Play 模式时自动运行(如果已启用):

  • BehaviorInterface 点动冲突:具有冲突控制方式的 Drive 组件
  • 多个 BehaviorInterface 检测:具有多个活动接口的 GameObject
  • TransportSurface 层级:Drive-TransportSurface 关系验证

组件添加验证

在 Editor 中添加组件时立即运行:

  • 多个 Drive 防止:阻止同一 GameObject 上的重复 Drive 组件

实际使用示例

常见开发场景

场景 1:构建传送带系统

带验证反馈的逐步操作

  1. 创建 Conveyor GameObject

   Hierarchy: ConveyorSystem
  1. 添加 Drive 组件
Drive added successfully
  1. 添加第二个 Drive(错误操作)

   ⚠ [Validation] PreventMultipleDrivesRule: Only one Drive component allowed per GameObject.
      Removing duplicate Drive component.
   ✓ System prevents configuration error
  1. 添加 TransportSurface

   ConveyorSystem (Drive)
   └── Belt (TransportSurface)
   ✓ Hierarchy validated - proper Drive-TransportSurface relationship

场景 2:带自动化控制的机械臂

开发进展

  1. 初始设置

   RobotArm (Drive + manual jogging enabled)
   ✓ Manual testing configuration
  1. 添加自动化控制

   RobotArm (Drive + BehaviorInterfaceRobot + jogging enabled)
   ⚠ [Validation] BehaviorInterfaceJoggingRule: Disabled jogging on Drive.
      Active BehaviorInterface will control drive position.
   ✓ System resolves control conflict automatically
  1. 最终配置

   RobotArm (Drive + BehaviorInterfaceRobot)
   ✓ Ready for automated operation

手动验证控制

运行时控制


// Dynamic validation control during development
public class ValidationController : MonoBehaviour
{
    public RealvirtualController realvirtualController;

    public void EnableValidation()
    {
        realvirtualController.ValidateBeforeStart = true;
        Debug.Log("Validation enabled for next Play mode start");
    }

    public void DisableValidationTemporarily()
    {
        realvirtualController.ValidateBeforeStart = false;
        Debug.Log("Validation temporarily disabled");
    }

    public void RunManualValidation()
    {
        if (realvirtualController != null)
        {
            realvirtualController.RunValidation();
        }
    }
}

Editor 集成


// Custom editor integration for validation
[CustomEditor(typeof(RealvirtualController))]
public class RealvirtualControllerEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Validation Tools", EditorStyles.boldLabel);

        if (GUILayout.Button("Run Validation Now"))
        {
            ((RealvirtualController)target).RunValidation();
        }

        if (GUILayout.Button("Reset Validation Settings"))
        {
            ((RealvirtualController)target).ValidateBeforeStart = true;
            ((RealvirtualController)target).ValidationOnComponentsAdded = true;
        }
    }
}

自定义验证规则

扩展验证系统

验证系统支持针对项目特定需求的自定义规则:


// Custom validation rule example
public class CustomComponentValidationRule : PrePlayRule<CustomComponent>
{
    public override string RuleName => "CustomComponentValidationRule";

    protected override bool IsValid(CustomComponent component)
    {
        // Custom validation logic
        return component.RequiredReference != null;
    }

    protected override void FixComponent(CustomComponent component)
    {
        // Automatic fix if possible
        if (component.RequiredReference == null)
        {
            component.RequiredReference = component.GetComponent<RequiredType>();
        }
    }

    protected override string GetWarningMessage(CustomComponent component)
    {
        return "CustomComponent requires a valid reference for proper operation.";
    }
}

故障排除

验证未运行

症状:启动 Play 模式或添加组件时,Unity Console 中未出现验证消息

诊断步骤

  1. 验证 RealvirtualController 配置

   // Check controller setup
   var controller = FindObjectOfType<RealvirtualController>();
   if (controller == null)
   {
       Debug.LogError("RealvirtualController not found in scene");
   }
   else
   {
       Debug.Log($"Validation enabled: {controller.ValidateBeforeStart}");
   }
  1. 检查 Console 过滤设置
    • 确保 Unity Console 显示 "Info" 级别消息
    • 查找控制台消息中的 "\[Validation]" 前缀
    • 检查 Console 折叠设置是否隐藏了验证消息
  2. 验证组件目标
    • 验证仅在具有活动验证规则的组件上运行
    • 使用已知验证场景测试(添加第二个 Drive 组件)

常见解决方案

  • 在 RealvirtualController Inspector 中启用 "Validate Before Start"
  • 确保 RealvirtualController GameObject 在场景中处于活动状态
  • 检查验证目标组件是否存在于场景中

理解验证消息

消息格式分析


[Validation] RuleName: Action taken and reason
Context: GameObject "Name"Component Type

示例解读


[Validation] BehaviorInterfaceJoggingRule: Disabled jogging on Drive.
Active BehaviorInterface will control drive position and conflicts with manual jogging.
Context: GameObject "ConveyorDrive"Drive component

Translation:
- Rule: BehaviorInterface jogging conflict detection
- Action: Automatically disabled jogging properties
- Reason: Prevent control conflicts
- Location: ConveyorDrive GameObject's Drive component

验证冲突和误报

当验证似乎有误时

  1. 理解规则逻辑
    • 查看规则文档以理解触发原因
    • 考虑您的配置是否遵循 realvirtual 最佳实践
    • 检查特殊用例是否需要不同的方法
  2. 合理的特殊情况

   // Temporary validation disable for special configurations
   public class SpecialCaseController : MonoBehaviour
   {
       void Start()
       {
           var controller = FindObjectOfType<RealvirtualController>();
           if (controller != null)
           {
               // Disable for specific scenario
               controller.ValidateBeforeStart = false;
               Debug.Log("Validation disabled for special case scenario");
           }
       }
   }
  1. 配置审查
    • 验证组件层级是否符合 realvirtual 模式
    • 检查组件引用和依赖关系
    • 考虑重构为标准配置

性能和集成问题

Editor 性能影响

症状:添加组件或启动 Play 模式时出现明显延迟

优化方法

  • 在快速原型开发期间禁用组件添加验证
  • 仅在最终测试阶段使用播放前验证
  • 考虑场景复杂性和验证目标数量

多场景验证问题

症状:不同场景间的验证不一致

解决方案

  • 确保每个场景有自己的 RealvirtualController
  • 验证场景特定的组件配置
  • 使用预制件 (Prefab) 在不同场景间保持一致的验证设置

另请参见


© 2025 realvirtual GmbH https://realvirtual.io - 版权所有。未经出版商事先书面许可,不得以任何形式或任何方式(包括打印、保存、复印、录制或其他电子或机械方法)复制、分发或传播本出版物的任何部分。