Skip to content

Game.Prefabs.Modes.UnlockAtStartMode

Assembly: Assembly-CSharp
Namespace: Game.Prefabs.Modes

Type: class

Base: LocalModePrefab

Summary:
A prefab mode component that configures certain prefabs to be unlocked at game start. You assign one or more target prefabs (m_Prefabs) and optionally a requirement prefab (m_Requirement). When applied, the mode clears any existing UnlockRequirement entries on each target and, if a requirement is provided, adds a single UnlockRequirement pointing to that requirement using UnlockFlags.RequireAll. This is typically used to ensure specific assets are available/unlocked at the beginning of a game or scenario.


Fields

  • public PrefabBase[] m_Prefabs
    An array of target prefabs that this mode will affect. Each entry will be resolved to an Entity via PrefabSystem and have its unlock requirements modified.

  • public PrefabBase m_Requirement
    Optional single prefab that serves as a requirement for unlocking the m_Prefabs. If null, the mode will clear the unlock requirements on the targets (effectively removing requirements).

Properties

  • This class does not define public properties of its own.

Constructors

  • public UnlockAtStartMode()
    Default constructor. The class is a Unity/Editor component (see ComponentMenu attribute) and is typically instantiated/serialized by Unity rather than constructed manually at runtime.

Methods

  • public override void RecordChanges(EntityManager entityManager, PrefabSystem prefabSystem)
    Collects entity references for the prefabs in m_Prefabs so the PrefabSystem records them as changed/used. For each PrefabBase in m_Prefabs it calls prefabSystem.GetEntity(prefabBase) to ensure the entity is tracked.

  • public override void ApplyModeData(EntityManager entityManager, PrefabSystem prefabSystem)
    Applies the unlock-at-start behavior:

  • Resolves the optional requirement prefab to an Entity (if m_Requirement is not null).
  • Iterates over each prefab in m_Prefabs, resolves its Entity, then looks for a DynamicBuffer on that entity.
  • If the buffer exists, it is cleared, and if a requirement Entity was resolved, it adds a single UnlockRequirement(entity, UnlockFlags.RequireAll).
  • Finally, the entity is marked with the Updated component to indicate the prefab's data changed.

  • public override void RestoreDefaultData(EntityManager entityManager, PrefabSystem prefabSystem)
    Restores default unlock data for prefabs in m_Prefabs:

  • For each PrefabBase, attempts to get its Unlockable component (using TryGetExactly).
  • If present, resolves the prefab to an Entity and collects its dependencies into a List.
  • Calls component.LateInitialize(entityManager, entity, dependencies) to reinitialize the unlockable data from its prefab defaults (restores original unlock requirements).

Usage Example

// Example usage in code (e.g. editor or initialization script)
var mode = new UnlockAtStartMode
{
    m_Prefabs = new PrefabBase[] { somePrefabA, somePrefabB },
    m_Requirement = someRequirementPrefab  // optional; set to null to clear requirements
};

// Apply immediately (in real usage, the PrefabSystem/EntityManager come from the game's context)
mode.ApplyModeData(entityManager, prefabSystem);

// To restore defaults later:
mode.RestoreDefaultData(entityManager, prefabSystem);

Additional notes: - This component is decorated with [ComponentMenu("Modes/Mode Component/", new Type[] { })], so it can be added/configured in the Unity inspector where supported. - The code relies on Unity.Entities (ECS) types such as Entity, EntityManager, DynamicBuffer, and JobHandle interactions handled elsewhere. - When no m_Requirement is provided, ApplyModeData clears existing unlock requirements; when a requirement is provided, it enforces that requirement using UnlockFlags.RequireAll.