using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// Stores a pool of Line. Allows retrieval and initialisation of pooled line. /// public class LineFactory : MonoBehaviour { /// /// The line prefab. /// public GameObject linePrefab; /// /// The number to pool. This is the maximum number of lines that can be retrieve from this factory. /// When a number of lines greater that this number is requested previous lines are overwritten. /// public int maxLines = 50; private Line[] pooledLines; private int currentIndex = 0; void Start () { pooledLines = new Line[maxLines]; for (int i = 0; i < maxLines; i++) { var line = Instantiate (linePrefab); line.SetActive (false); line.transform.SetParent (transform); pooledLines[i] = line.GetComponent (); } } /// /// Gets an initialised and active line. The line is retrieved from the pool and set active. /// /// The active line. /// Start position in world space. /// End position in world space. /// Width of line. /// Color of line. public Line GetLine (Vector2 start, Vector2 end, float width, Color color) { var line = pooledLines [currentIndex]; line.Initialise (start, end, width, color); line.gameObject.SetActive (true); currentIndex = (currentIndex + 1) % pooledLines.Length; return line; } /// /// Returns all active lines. /// /// The active lines. public List GetActive() { var activeLines = new List (); foreach (var line in pooledLines) { if (line.gameObject.activeSelf) { activeLines.Add (line); } } return activeLines; } }