Search Unity

Proper way of pausing gameplay and physics

Discussion in 'Entity Component System' started by nicolasgramlich, Feb 23, 2020.

  1. nicolasgramlich

    nicolasgramlich

    Joined:
    Sep 21, 2017
    Posts:
    231
    I was wondering what the "proper/recommended" way of pausing an ECS based game (with physics) is. Currently I'm just fetching my own
    SimulationSystem(Group)s
    and the
    StepPhysicsWorld
    and toggle their
    Enabled
    flags (as well as the good old
    timeScale = 0
    ). It works like I want to, but I was wondering if I should instead pause the entirety of all SimulationGroups?

    Code (CSharp):
    1. public void OnPauseButtonClicked() { // A button on a canvas
    2.    var gameSimulationSystemGroup = World.DefaultGameObjectInjectionWorld.GetExistingSystem<GameSimulationSystemGroup>();
    3.    var lateGameSimulationSystemGroup = World.DefaultGameObjectInjectionWorld.GetExistingSystem<LateGameSimulationSystemGroup>();
    4.    var stepPhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<StepPhysicsWorld>();
    5.  
    6.    var enabledPrevious = gameSimulationSystemGroup.Enabled;
    7.    var enabledNow = !enabledPrevious;
    8.  
    9.    gameSimulationSystemGroup.Enabled = enabledNow;
    10.    lateGameSimulationSystemGroup.Enabled = enabledNow;
    11.    stepPhysicsWorld.Enabled = enabledNow;
    12.    Time.timeScale = enabledNow ? 1 : 0;
    13. }
     
    kerjemanov and tarkhon like this.