Skip to content

Game.Prefabs.Modes.ModePrefab

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

Type: abstract class

Base: PrefabBase

Summary:
ModePrefab is an abstract base class used for game mode prefabs. It extends PrefabBase and provides support for registering required components for mode entities and for recording in-editor / development debug information about mode-related component values (used by debug UI). The debug logging APIs are compiled only in UNITY_EDITOR or DEVELOPMENT_BUILD builds and are no-ops otherwise.


Fields

  • public class ModeDebugUILogInfo
    ModeDebugUILogInfo is a nested helper class used to store a single debug log entry for a particular Type. It contains three public fields used by the logging system:

  • public Type m_Key
    The Type of the logged value (used to identify entries in per-entity lists).

  • public object m_ValueBefore
    The value captured as the "before" snapshot (or initial capture).

  • public object m_ValueAfter
    The value captured as the "after" snapshot (if any).

There are no other explicit private fields declared in ModePrefab. The class exposes debug state via the property described below.

Properties

  • public Dictionary<Entity, List<ModeDebugUILogInfo>> modeDebugUILogs { get; private set; }
    A dictionary that maps an Entity to a list of ModeDebugUILogInfo entries. This property holds the debug log snapshots for entities and is only populated when RecordLog is invoked (and only in editor/development builds due to Conditional attributes). The setter is private — typical usage from derived classes is to call RecordLog / ClearLog rather than mutating this dictionary directly.

Constructors

  • public ModePrefab()
    No explicit constructor is declared in the source; the compiler-provided default constructor is used. Implementations can rely on the default construction semantics and initialize any instance state in derived classes if needed.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Adds component types required by the prefab to the provided set. ModePrefab overrides PrefabBase.GetPrefabComponents and ensures that GameModeComponent is included as a ReadWrite component:
  • Adds ComponentType.ReadWrite().
  • Call base.GetPrefabComponents(components) first so base behavior is preserved. This method runs in normal builds.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    An override that is currently empty in ModePrefab. Derived classes can override to add more archetype components if required. No behavior is implemented in the base class.

  • protected void RecordLog<T>(Entity entity, ref T value) where T : unmanaged
    (Compiled only when UNITY_EDITOR or DEVELOPMENT_BUILD are defined due to the Conditional attributes.) Records a snapshot of an unmanaged value for the specified entity into modeDebugUILogs. Behavior:

  • Lazily initializes modeDebugUILogs if null.
  • Ensures a list exists for the given entity.
  • Locates an existing ModeDebugUILogInfo for the Type of value; if none exists it adds one with m_ValueBefore set to the captured value and m_ValueAfter null.
  • If an entry exists, updates m_ValueAfter with the new captured value. Notes:
  • T must be unmanaged.
  • The method stores boxed objects (object) for before/after values; be mindful of allocations in hot code paths.
  • This method is intended for editor/development debugging and will be stripped out in production builds.

  • protected void RecordLog<T>(Entity entity, ref DynamicBuffer<T> value) where T : unmanaged
    (Compiled only when UNITY_EDITOR or DEVELOPMENT_BUILD are defined due to the Conditional attributes.) Records a snapshot of a DynamicBuffer by copying its contents into a managed array and storing that array in modeDebugUILogs. Behavior:

  • Lazily initializes modeDebugUILogs if null.
  • Ensures a list exists for the given entity.
  • Copies the contents of the DynamicBuffer to a T[] array.
  • Looks for an existing ModeDebugUILogInfo for the DynamicBuffer Type; if none exists it adds one with m_ValueBefore set to the copied array and m_ValueAfter null.
  • If an entry exists, updates m_ValueAfter with the copied array. Notes:
  • This performs a copy of the buffer (allocation). Use sparingly in performance-sensitive paths; intended for debugging in editor/dev builds only.
  • Type used as the key is value.GetType() — that will represent the runtime type of the buffer.

  • public void ClearLog()
    (Compiled only when UNITY_EDITOR or DEVELOPMENT_BUILD are defined due to the Conditional attributes.) Clears the modeDebugUILogs dictionary if it has been allocated. Use this to reset debug state between frames or mode changes in editor/dev builds.

Usage Example

// Example usage inside a derived ModePrefab class.
// These calls have effect only in UNITY_EDITOR or DEVELOPMENT_BUILD builds.

public class MyModePrefab : ModePrefab
{
    // Example: record a change to an unmanaged component field
    protected void UpdateModeValue(Entity e, ref SomeUnmanagedComponent comp)
    {
        // capture "before" (first call will set m_ValueBefore)
        RecordLog(e, ref comp.someValue);

        // modify component value...
        comp.someValue += 1;

        // capture "after" (will set m_ValueAfter)
        RecordLog(e, ref comp.someValue);
    }

    // Example: record a DynamicBuffer snapshot
    protected void UpdateBuffer(Entity e, ref DynamicBuffer<MyBufferElement> buffer)
    {
        // Copies buffer contents into an object array entry in the debug log.
        RecordLog(e, ref buffer);
    }

    // Clear stored debug logs (editor/dev builds)
    public void ResetDebugLogs()
    {
        ClearLog();
    }

    public override void GetPrefabComponents(HashSet<ComponentType> components)
    {
        base.GetPrefabComponents(components);
        // Add further components required by this mode prefab...
    }
}

Notes and considerations: - RecordLog overloads are decorated with [Conditional("UNITY_EDITOR")] and [Conditional("DEVELOPMENT_BUILD")], so calls to them are compiled out in release builds without those symbols. This prevents runtime overhead in shipping builds. - The logging stores snapshots as boxed objects (for primitive/unmanaged values) and T[] arrays (for buffer copies). If large numbers of entities are logged, memory allocations can increase significantly — intended for debugging only. - GetArchetypeComponents is left empty in this base class; derived mode prefabs should override it if they need specific archetype component sets.