realvirtual.io
  • Building Digital Twins with Unity and realvirtual.io
  • Basics
    • Installation
    • Demo Model
      • Old Demo Model
    • Editor User Interface
      • Hierarchy Window
      • 3D Views
      • Quick Edit
      • Move Pivot Points (Pro)
      • Selection Window (Pro)
      • Material Window (Pro)
      • Measurement (Pro)
      • Kinematic Tool (Pro)
      • Model Checker
    • Runtime UI
      • Group Manager
      • Debug Console
      • Responsiveness
      • Runtime Persistence
    • Importing and exporting
    • Folder structure
    • Tutorial
    • Physics
    • CAD import (Pro)
      • CADLink (Pro)
      • CAD Checker (Pro)
      • CAD Updater (Pro)
    • Reusable Components (Prefabs)
    • Cadenas parts4cad
    • Publishing the Digital Twin
    • Revision management
  • News
  • Login & Download Updates (Pro)
  • Components & Scripts
    • Realvirtual
      • Environment Controller
    • MU, Source and Sink
      • Source
      • Sink
    • Motion and Kinematic
      • Drive
      • Kinematic
      • Group
      • Joint
      • CAM
      • TransportSurface
      • Guided Transport
      • Drive behavior
      • Chain
      • Chain element
      • Motion for developers
    • Sensors
      • Sensor
      • Measure
      • MeasureRaycast
    • Picking and Placing MUs
      • Grip
      • Gripper
      • Fixer
      • Pattern
    • Changing MUs
      • MaterialChanger
      • PartChanger
      • Cutter
    • Scene Interaction
      • 3D Buttons
      • Screen Overlay Buttons
      • Scene Selectables
      • Lamp
      • Interact3D
      • UI components
      • HMI components (Pro)
        • Start your own HMI
        • HMI Tab
        • HMI DropDown
        • HMI Puschbutton
        • HMI Switch
        • HMI Value
        • HMI Text
        • HMI Slider
        • HMI Message
        • HMI Marker
      • ModelZoo (Pro)
    • Interfaces
      • Interface Tools
        • Signal Manager
        • Signal Importer Exporter
        • Signal Catcher
        • OnValueChangedReconnect
      • Signal Importer / Exporter
      • ABB RobotStudio (Pro)
      • Denso Robotics (Pro)
      • EthernetIP (Pro)
      • Fanuc (Pro)
      • FMI
      • Igus Rebel
      • MQTT (Pro)
      • Modbus (Pro)
      • OPCUA (Pro)
      • PLCSIM Advanced (Pro)
      • RoboDK (Pro)
      • RFSuite (Pro)
      • SEW SimInterface (Pro)
      • Siemens Simit Interface (Pro)
      • Simit Shared Memory (Pro)
      • Simulink (Pro)
      • S7 TCP
      • TwinCAT (Pro)
      • TwinCAT HMI (Pro)
      • UDP (Pro)
      • Universal Robots (Pro)
      • Wandelbots Nova (Pro)
      • Websocket (Pro)
      • Windmod Y200 (Pro)
      • Custom Interfaces
    • Performance Tools
      • Combine Meshes (Pro)
      • Create Prefab (Pro)
      • Hierarchy Cleanup (Pro)
      • Mesh Optimizer (Pro)
      • Performance Optimizer (Pro)
    • Defining Logic
      • Unity Scripting
      • Behavior Graph
      • Logicsteps
      • Unity Visual Scripting
      • Recorder
    • Robot Inverse Kinematics (Pro)
    • Volume Tracking (Pro)
  • Multiplayer (Pro)
  • Extensions
    • ModelZoo
      • Beckhoff TwinCAT
      • Siemens S7
      • Denso Cobotta 1300
      • Wandelbots Nova Fanuc CRX
      • Universal Robots UR10
      • Fanuc Roboguide
    • realvirtual.io AIBuilder
      • realvirtual.io AI Builder Overview
      • Generate AI Training Data
      • AI Training
      • Testing AI in a Digital Twin
      • Deploying the AI
    • realvirtual.io Simulation
      • Conveyor Library
      • Path System
        • Path finding
        • Line
        • Curve
        • Workstation
        • Catcher
        • Pathmover
    • realvirtual.io Industrial Metaverse
      • Setup Guide
      • VR Modules
      • AR Modules
      • Multiuser
    • AGX Physics
    • VR Builder
    • CMC ViewR
  • Advanced Topics
    • Render Pipelines
    • Mixed Reality with Meta Quest3
    • Upgrade Guide
      • Upgrade to 2022
    • Open Digital Twin Interface
    • Usefull Addons
    • Improving Performance
    • Supported Platforms
    • Compiler Defines
    • For Developers
      • Important Classes and Interfaces
      • Assembly Definitions
      • Starting your development
      • Class documentation
      • realvirtual Init Sequence
      • realvirtualBehavior Lifetime Methods
      • Testing
    • Newtonsoft JSON
    • Troubleshooting
  • Release Notes
  • AI Digital Twin Assistant (GPT4)
  • License Conditions
Powered by GitBook
On this page
  • Overview of Methods
  • Typical Flow
  • Example Script
  • When to Use These Methods
  • Debugging with DebugMode
Edit on GitHub
  1. Advanced Topics
  2. For Developers

realvirtualBehavior Lifetime Methods

When you inherit from realvirtualBehavior in your scripts, the realvirtualController automatically invokes several core lifecycle methods at key moments. This mechanism ensures a consistent flow for starting, stopping, and updating your simulation logic.


Overview of Methods

  1. AwakeAlsoDeactivated()

    • Called by realvirtualController in its own Awake() method.

    • Runs even if the realvirtualBehavior component (or its GameObject) is not active.

    • Ideal for any early setup logic that normally would run in Awake(), but needs to occur even if disabled.

  2. PreStartSim()

    • Called once realvirtualController is ready to start the simulation.

    • Use this to finalize any pre-simulation configuration, such as resetting positions or clearing caches.

  3. StartSim()

    • Immediately after PreStartSim().

    • Begin your runtime logic here—start animations, open connections, spawn objects, etc.

  4. StopSim()

    • Called when the simulation is being paused or stopped (for instance, via the pause button).

    • Ideal for halting coroutines, stopping movements, and closing any active connections.


Typical Flow

  1. realvirtualController.Awake()

    • Calls AwakeAlsoDeactivated() on every realvirtualBehavior.

  2. realvirtualController.Start()

    • Eventually calls StartSim() (after running PreStartSim()).

  3. realvirtualController.Pause() / StopSim()

    • Invokes StopSim() on every realvirtualBehavior.

Here’s a simplified timeline:

Unity Loads Scene
└──> realvirtualController: Awake()
     ├──> Calls AwakeAlsoDeactivated() on all realvirtualBehavior scripts
     └──> Additional Initialization Steps

Later, realvirtualController: Start()
├──> PreStartSim() on all realvirtualBehavior
└──> StartSim() on all realvirtualBehavior

When Pausing or Stopping:
└──> StopSim() on all realvirtualBehavior

Example Script

Below is an example of how you might extend realvirtualBehavior to respond to these calls:

using UnityEngine;

namespace realvirtual
{
    // Inherit from realvirtualBehavior
    public class MyMachine : realvirtualBehavior
    {
        // Called by the controller in Awake(), even if 'MyMachine' is not active
        public override void AwakeAlsoDeactivated()
        {
            base.AwakeAlsoDeactivated();
            Debug.Log("MyMachine: AwakeAlsoDeactivated - Basic setup done.");
        }

        // Called just before the simulation actually starts
        public override void PreStartSim()
        {
            Debug.Log("MyMachine: PreStartSim - Final checks before sim begins.");
        }

        // Called immediately after PreStartSim
        public override void StartSim()
        {
            Debug.Log("MyMachine: StartSim - Simulation logic begins.");
        }

        // Called when the simulation is being paused or stopped
        public override void StopSim()
        {
            Debug.Log("MyMachine: StopSim - Cleaning up and halting movements.");
        }
    }
}

When to Use These Methods

  • AwakeAlsoDeactivated()

    • Use for setting references or configuration that must exist regardless of the GameObject’s active state.

    • Useful for bridging dependencies or preparing data needed by other scripts.

  • PreStartSim()

    • Use for last-minute preparation just before the simulation becomes “live.”

    • E.g., ensuring internal caches or object pools are ready, resetting the environment, etc.

  • StartSim()

    • Your “go” signal for the main logic—starting animations, enabling physics-based behaviors, or beginning timers.

  • StopSim()

    • Graceful shutdown: stopping animations, saving states, cleaning up.

    • Called when the user pauses, or the simulation is otherwise halted by realvirtualController.


Debugging with DebugMode

By enabling DebugMode on the realvirtualController, you will see logs in the Unity console showing precisely when each of these methods is invoked. This is an excellent way to confirm:

  • Whether your scripts are receiving these calls in the expected order.

  • If certain scripts remain disabled and thus never receive the calls.

Previousrealvirtual Init SequenceNextTesting

Last updated 2 months ago