Class SpiralLayoutContainer
- Namespace
- WitShells.DesignPatterns
A MonoBehaviour that procedurally arranges ISpiralNode GameObjects in a spiral pattern using an ObjectPool<T> for efficient instantiation and reuse.
public class SpiralLayoutContainer : MonoBehaviour
- Inheritance
-
SpiralLayoutContainer
Remarks
Attach this component to a parent GameObject in your UI or world space.
Assign a spiralNodePrefab that implements ISpiralNode.
Call GenerateSpiralLayout(int) at runtime to build (or rebuild) the spiral.
The spiral can operate in either world space or canvas (RectTransform) space
depending on the canvasSpace setting.
Fields
canvasSpace
protected bool canvasSpace
Field Value
head
protected ISpiralNode head
Field Value
maxCount
protected int maxCount
Field Value
nodes
protected List<ISpiralNode> nodes
Field Value
spacing
protected float spacing
Field Value
spiralDirection
protected SpiralDirection spiralDirection
Field Value
spiralGrowthRate
protected float spiralGrowthRate
Field Value
spiralNodePrefab
protected GameObject spiralNodePrefab
Field Value
- GameObject
tail
protected ISpiralNode tail
Field Value
Properties
Head
The first node in the spiral chain (closest to the centre).
public ISpiralNode Head { get; }
Property Value
NodePool
public ObjectPool<GameObject> NodePool { get; }
Property Value
- ObjectPool<GameObject>
Tail
The last node added to the spiral chain.
public ISpiralNode Tail { get; }
Property Value
Methods
AddNodeToSpiral()
protected virtual void AddNodeToSpiral()
Awake()
protected virtual void Awake()
CalculateSpiralPosition(int)
protected virtual Vector3 CalculateSpiralPosition(int index)
Parameters
indexint
Returns
- Vector3
ClearLayout()
protected virtual void ClearLayout()
ConnectNodes()
protected virtual void ConnectNodes()
CreateFirstNode()
protected virtual void CreateFirstNode()
GenerateSpiral(Vector2Int, int)
public IEnumerable<Vector2Int> GenerateSpiral(Vector2Int center, int totalTiles)
Parameters
centerVector2InttotalTilesint
Returns
- IEnumerable<Vector2Int>
GenerateSpiralLayout(int)
Clears any existing spiral and generates a new one with count nodes.
Node GameObjects are obtained from NodePool and placed according to
a square spiral algorithm. Clamped to maxCount.
public virtual void GenerateSpiralLayout(int count)
Parameters
countintDesired number of nodes. Clamped to
maxCount.
SetHeadCoordinate(Vector2Int)
Moves the head node to coordinate and propagates the offset
to all subsequent nodes in the chain.
public void SetHeadCoordinate(Vector2Int coordinate)
Parameters
coordinateVector2IntNew 2D grid coordinate for the head node.