Skip to content

Game.Simulation.DiversitySystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: GameSystemBase, IPostDeserialize

Summary:
DiversitySystem is an ECS system responsible for ensuring that essential "diversity" singletons (Atmosphere and Biome) exist after deserialization and for applying preset prefabs to those singletons. It also contains a small post-deserialization fix that repairs invalid EditorContainer entities placed at the world origin by copying a valid owner Transform when possible. The system implements IPostDeserialize and runs its PostDeserialize logic during the deserialization phase to populate or correct components required by rendering/environment subsystems.


Fields

  • private EntityQuery m_AtmosphereQuery
    Query for existing AtmosphereData singleton entities. Used to check whether an AtmosphereData component already exists in the world and to read/update its singleton.

  • private EntityQuery m_AtmospherePrefabQuery
    Query to find Atmosphere prefab entities (AtmospherePrefabData). Used to pick a default prefab when no AtmosphereData singleton exists.

  • private EntityQuery m_BiomeQuery
    Query for existing BiomeData singleton entities. Used similarly to m_AtmosphereQuery but for biome.

  • private EntityQuery m_BiomePrefabQuery
    Query to find Biome prefab entities (BiomePrefabData). Used to pick a default biome prefab when no BiomeData singleton exists.

  • private EntityQuery m_EditorContainerQuery
    Query to find editor container entities. This query includes Game.Tools.EditorContainer, Game.Objects.Transform and Owner component types and is used by PostDeserialize to locate editor containers that have invalid (zero) transforms.

Properties

  • None. (This system exposes no public properties.)

Constructors

  • public DiversitySystem()
    Default constructor. Marked with [Preserve] to prevent code stripping; typical Unity ECS system construction behavior is used (no extra initialization happens here — initialization occurs in OnCreate).

Methods

  • public void ApplyAtmospherePreset(Entity atmospherePrefab)
    Sets the AtmosphereData singleton's m_AtmospherePrefab to the provided atmospherePrefab. Internally gets the current AtmosphereData singleton, updates its m_AtmospherePrefab field, and writes it back with SetSingleton.

  • public void ApplyBiomePreset(Entity biomePrefab)
    Sets the BiomeData singleton's m_BiomePrefab to the provided biomePrefab in the same manner as ApplyAtmospherePreset.

  • public void PostDeserialize(Context context)
    Implementation of IPostDeserialize. Called after entities/components are deserialized. Behavior:

  • Queries for available AtmospherePrefabData and BiomePrefabData entities.
  • Logs a warning and returns early if no prefabs of either type are found.
  • If there is no AtmosphereData singleton present, creates a new entity and adds AtmosphereData using the first found Atmosphere prefab.
  • If there is no BiomeData singleton present, creates a new entity and adds BiomeData using the first found Biome prefab.
  • Scans EditorContainer entities; for any editor container whose Transform is exactly float3.zero, logs the occurrence and, if the container has an Owner whose transform exists, copies the owner's Transform component onto the editor container (fixing invalid placement).
  • Disposes any NativeArray instances it allocates.

  • protected override void OnCreate()
    System initialization. Sets up the EntityQuery fields:

  • m_AtmosphereQuery => ComponentType.ReadOnly()
  • m_AtmospherePrefabQuery => ComponentType.ReadOnly()
  • m_BiomeQuery => ComponentType.ReadOnly()
  • m_BiomePrefabQuery => ComponentType.ReadOnly()
  • m_EditorContainerQuery => ReadOnly(), ReadOnly(), ReadOnly() Marked with [Preserve] to avoid stripping.

  • protected override void OnUpdate()
    Empty override. This system performs its work during deserialization (PostDeserialize) rather than per-frame updates.

Usage Example

// Acquire the system from the default world and apply a preset prefab entity:
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var diversitySystem = world.GetExistingSystem<Game.Simulation.DiversitySystem>();

// Suppose you have an Entity representing an Atmosphere prefab (prefabEntity):
if (diversitySystem != null)
{
    diversitySystem.ApplyAtmospherePreset(prefabEntity);
}

// During deserialization, the game's serialization pipeline will call:
 // diversitySystem.PostDeserialize(context);
 // which will create missing AtmosphereData/BiomeData singletons
 // and attempt to fix invalid EditorContainer transforms.

Notes: - Methods and the constructor are marked with [Preserve] to avoid being stripped by Unity's managed code stripping. - This system expects to run in the mod/game deserialization phase; directly invoking PostDeserialize is normally handled by the engine's deserialization pipeline via the IPostDeserialize interface.