Skip to content

Game.Prefabs.PrefabID

Assembly:
Namespace: Game.Prefabs

Type: struct

Base: IEquatable, ISerializable

Summary:
Represents a lightweight identifier for a prefab using its runtime type name and instance name. Used to uniquely (within typical usage) identify prefabs across save/serialization boundaries and to support backward-compatible deserialization of older save formats. Implements custom serialization/deserialization that handles legacy representations and performs a small type migration for an old prefab type name.


Fields

  • private string m_Type
    Stores the prefab's type name (prefab.GetType().Name). Used as the first part of the identifier.

  • private string m_Name
    Stores the prefab's instance name (prefab.name). Used as the second part of the identifier and as the basis for GetHashCode().

Properties

  • (none)
    This struct exposes no public properties; the identifier is accessed via methods (GetName, ToString) and equality operations.

Constructors

  • public PrefabID(PrefabBase prefab)
    Creates a PrefabID from a PrefabBase instance. Initializes m_Type with the prefab's runtime type name and m_Name with the prefab's name.

  • public PrefabID(string type, string name)
    Creates a PrefabID from explicit type and name strings.

Methods

  • public bool Equals(PrefabID other)
    Compares this PrefabID to another by checking equality of m_Type and m_Name. Returns true only if both match.

  • public override int GetHashCode()
    Returns the hash code of m_Name. Note: only the name is used for hashing, so different types with the same name will collide in hash-based collections.

  • public override string ToString()
    Returns a string in the form "Type:Name" (interpolated from m_Type and m_Name).

  • public string GetName()
    Returns the stored prefab name (m_Name).

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the type and name (as two strings) to the provided writer. The ordering is type then name.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads the prefab id information from the reader. Behavior:

  • If reader.context.version < Version.newPrefabID, it expects a legacy single string in the format "Type:Name"; it reads that string and splits on ':' to populate m_Type and m_Name.
  • Otherwise it reads m_Type and m_Name as separate string values.
  • Additionally, for very old versions (reader.context.version < Version.staticObjectPrefab) if the type read is "ObjectGeometryPrefab", it remaps that type to "StaticObjectPrefab" to preserve compatibility with renamed prefab types.

Usage Example

// create from a PrefabBase instance
PrefabID idFromPrefab = new PrefabID(somePrefab);

// create from explicit strings
PrefabID id = new PrefabID("BuildingPrefab", "SmallHouse");

// print
Console.WriteLine(id.ToString()); // "BuildingPrefab:SmallHouse"

// serialization example (pseudo-code; depends on writer/reader implementations)
writer.Write(id); // via IWriter extension that handles structs implementing ISerializable

// deserialization (reader will handle legacy formats internally)
reader.Read(out PrefabID loadedId);