Search Unity

How to restart a ECS scene with physics

Discussion in 'Physics for ECS' started by gmentat, Oct 19, 2020.

  1. gmentat

    gmentat

    Joined:
    Nov 12, 2012
    Posts:
    15
    I'm trying to implement a Restart button for a user in a ECS scene with physics (Havok).

    The world is mostly setup in a scene from the start. So I want to reset everything in the scene and run it again. Here's what I came up with:

    Code (CSharp):
    1. var entityManager = Unity.Entities.World.DefaultGameObjectInjectionWorld.EntityManager;
    2.  
    3. entityManager.DestroyEntity(entityManager.UniversalQuery);
    4. Unity.Entities.World.DisposeAllWorlds();
    5. DefaultWorldInitialization.Initialize("Default World", false);
    6.  
    7. SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);
    I launch it from a coroutine as some people suggested in the forums.

    It kinda works but the simulation is accelerated after the restart in the editor for a second and runs at about 5 FPS on an Android device (60 at start).

    What am I doing wrong and how to properly implement a restart-scene button in ECS?
     
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    You don't really need to dispose all worlds and initialize again, just destroying all entities should give the desired results
     
    petarmHavok likes this.
  3. gmentat

    gmentat

    Joined:
    Nov 12, 2012
    Posts:
    15
    Thanks brunocoimbra. Removing the world disposal seems to be doing the trick.

    Code (CSharp):
    1. var entityManager = Unity.Entities.World.DefaultGameObjectInjectionWorld.EntityManager;
    2. entityManager.DestroyEntity(entityManager.UniversalQuery);
    3. SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);