Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Issues With Multiple Worlds and Loading Scenes

Discussion in 'Entity Component System' started by spectre1989, Oct 30, 2019.

  1. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Hi all,

    I'm trying to create multiple worlds, and in the scene mark up which stuff goes in world A and/or world B. I've been taking an approach similar to what I saw in the Unity multiplayer github repo, which is:

    Create an ICustomBootstrap class, which creates an additional world in the Initialize() function:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using Unity.Entities;
    4.  
    5. public class WorldManager : ICustomBootstrap
    6. {
    7.     public static World[] worlds;
    8.  
    9.     public List<Type> Initialize(List<Type> systems)
    10.     {
    11.         if (worlds != null)
    12.         {
    13.             return systems;
    14.         }
    15.  
    16.         worlds = new World[2];
    17.         worlds[0] = World.Active;
    18.  
    19.         World world = new World("Second World");
    20.         worlds[1] = world;
    21.         // second worlds systems will be created here
    22.  
    23.         return systems;
    24.     }
    25. }
    26.  
    Then I create a "WorldA" subscene, a "WorldB" subscene, and a "Common" subscene, unticking auto load scene. I attach the following script to each subscene:
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Entities;
    3. using Unity.Scenes;
    4.  
    5. public class SubSceneWorld : MonoBehaviour
    6. {
    7.     public int[] worlds;
    8.  
    9.     private void OnEnable()
    10.     {
    11.         SubScene subScene = GetComponent<SubScene>();
    12.  
    13.         subScene.AutoLoadScene = true;
    14.         World previouslyActiveWorld = World.Active;
    15.  
    16.         foreach (int world in worlds)
    17.         {
    18.             World.Active = WorldManager.worlds[world];
    19.             subScene.UpdateSceneEntities();
    20.         }
    21.  
    22.         subScene.AutoLoadScene = false;
    23.         World.Active = previouslyActiveWorld;
    24.     }
    25. }
    26.  
    Is this the correct way to load a scene into a specific world?

    I can see the stuff in the WorldA subscene (which is for world[0], the default world), but WorldB and Common I can't see. I understand why I can't see the stuff in WorldB because I haven't added any systems to it yet, but why can't I see the Common subscene objects?

    Cheers
    J
     
    Last edited: Nov 1, 2019
    florianhanke likes this.
  2. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Updated post with most recent progress, hopefully someone has an idea what I'm getting wrong.
     
  3. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    I've been trying to figure this out for days now, no Unity people who can help me even?
     
  4. HappyStoner

    HappyStoner

    Joined:
    May 31, 2018
    Posts:
    9
    What do you mean with 'see'?
     
  5. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    I can see them in the game viewport
     
  6. HappyStoner

    HappyStoner

    Joined:
    May 31, 2018
    Posts:
    9
    You can or you can't? If you can't, it's because they're not in the main world.
     
  7. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    As I said in the original post, I have 3 subscenes and 2 worlds. I'm trying to load scene 1 into world 1, scene 2 to world 2, and scene 3 to worlds 1&2. I can see the scene 1 contents but not scene 3. I can't see scene 2 but I don't expect to yet (it doesn't have any systems). This is an approach used in the unity multiplayer GitHub repo but I can't get it to work.
     
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,631
    What you're doing does not work.

    When you call UpdateSceneEntities() it destroys all existing entities that were created previously before creating them again.

    Your approach works on loading to a specific world but it does not allow loading to multiple worlds at same time. The SubScene script by default only allows loading into a single world. You would need multiple SubScene scripts.
     
  9. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Thank you!
     
  10. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    So I guess the stuff in the unity multiplayer doesn't work then, or maybe requires a future release of the entities package
     
  11. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    For reference, this https://github.com/Unity-Technologi...ets/NetCode/Authoring/ClientServerSubScene.cs
     
  12. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,631
    The multiplayer package is very out of date, it uses Unity 0.0.12-preview.33

    There is meant to be an update coming soon~
     
  13. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Is there a recommended way to load a scene or subscene to specific (and potentially multiple) worlds?
     
  14. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,631
    I think this is the recommended way to loading it to a specific world.

    As for multiple worlds you could potentially have more than 1 subscene component pointing to the same scene or maybe better clone it per world at runtime.

    I'd probably just load it into into a single world (probably a staging world) then clone the entities it into different worlds.

    But I've never tried or really looked into it so no idea if it'd work.
     
    spectre1989 likes this.
  15. saarg

    saarg

    Joined:
    Mar 9, 2017
    Posts:
    31
    To load a subsene in a specific world you can look at this but I don't think it works with the current entities version so you'll have to wait a bit: https://forum.unity.com/threads/loa...1572877018-2065661543.1556050628#post-5026634

    If you want to then split object between multiple worlds you'll need to use this: https://docs.unity3d.com/Packages/c...oveEntitiesFrom_Unity_Entities_EntityManager_

    Note that I managed to have multiple worlds with renderer working but static RenderMesh won't show after the move. The frozen render mesh update starts by checking GetComponentOrderVersion<FrozenRenderSceneTag> wich is odd because FrozenRenderSceneTag is a shared component and should not be covered by GetComponentOrderVersion
     
    spectre1989 likes this.
  16. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Yeah cloning the entities sounds like a good shout, thanks
     
  17. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Loading into a staging world had some issues for me - basically I couldn't figure out when the scene has finished loading because the StreamingState component used by the SubSceneStreamingSystem is inaccessible. I guess I'll try multiple subscene scripts unless anyone knows a way around that?
     
  18. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Ah, and multiple instances of the same subscene script doesn't work because there's a static list of subscenes in SubScene.cs so it detects that I'm trying to do this and logs a warning
     
  19. Jawsarn

    Jawsarn

    Joined:
    Jan 12, 2017
    Posts:
    215
    Trying the same thing with mixed subscene for client and server, and bumped into same problem^^ Hopefully next release will fix it.
     
    spectre1989 likes this.