Game.Prefabs.AttachedObject
Assembly: Assembly-CSharp
Namespace: Game.Prefabs
Type: class
Base: ComponentBase
Summary:
AttachedObject is a prefab component used to mark placeable objects with an attachment type and offset. During entity initialization it modifies the PlaceableObjectData component on the entity to set placement flags (OnGround, Wall, Hanging) and the placement offset (typically Y for vertical offset, Z for wall depth). This component is intended for use with StaticObjectPrefab and MarkerObjectPrefab (see ComponentMenu attribute).
Fields
-
public AttachedObjectType m_AttachType
Specifies how the object is attached. Common enum values are Ground, Wall, Hanging (as used in Initialize). Determines which PlacementFlags are set/cleared and which axis of m_PlacementOffset is used. -
public float m_AttachOffset
The offset applied to the PlaceableObjectData placement offset. For Ground and Hanging the value is applied to placementOffset.y (vertical). For Wall the value is applied to placementOffset.z (depth).
Properties
- None (this component exposes no public properties).
Constructors
public AttachedObject()
Implicit default constructor. The component is typically created/serialized by Unity when placed on a prefab. No special constructor logic is defined in the class.
Methods
-
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the required entity component types to the prefab conversion. This implementation adds PlaceableObjectData as a ReadWrite component to ensure Initialize can read and write it. -
public override void GetArchetypeComponents(HashSet<ComponentType> components)
No archetype components are added by this component (method intentionally empty). -
public override void Initialize(EntityManager entityManager, Entity entity)
Reads PlaceableObjectData from the entity, updates placement flags and placement offset according to m_AttachType and m_AttachOffset, then writes the modified PlaceableObjectData back to the entity.
Behavior per AttachedObjectType: - Ground: sets PlacementFlags.OnGround and writes m_AttachOffset to placementOffset.y - Wall: clears OnGround, sets PlacementFlags.Wall and writes m_AttachOffset to placementOffset.z - Hanging: clears OnGround, sets PlacementFlags.Hanging and writes m_AttachOffset to placementOffset.y
Notes: - This method depends on PlaceableObjectData being present on the entity. GetPrefabComponents ensures that component is added during conversion. - Uses EntityManager.GetComponentData and SetComponentData (Unity.Entities).
Usage Example
// Example: set up the AttachedObject in code (normally set in Inspector on a prefab)
var attached = gameObject.AddComponent<Game.Prefabs.AttachedObject>();
attached.m_AttachType = AttachedObjectType.Wall;
attached.m_AttachOffset = 0.5f;
// When the prefab is converted to an entity, AttachedObject.GetPrefabComponents ensures
// PlaceableObjectData exists, and AttachedObject.Initialize will adjust flags/offsets accordingly.
Additional notes: - PlacementFlags (OnGround, Wall, Hanging) and PlaceableObjectData are part of the placement system; changing these affects where and how the object can be placed in the editor/game. - Coordinate conventions: placementOffset.y is vertical (up/down). placementOffset.z is used for depth offset when attaching to walls. Ensure offsets are appropriate for your art/physics scale.