/// <summary> /// Orders the lawn elements so that they're drawn correctly. /// </summary> private void OrderLawn() { List <RectTransform> transforms = new List <RectTransform>(this.lawn.Capacity); foreach (RectTransform transform in this.rectTransform) { if (LawnController.IsWeed(transform.gameObject)) { transforms.Add(transform); } } // Sort the items by y so that the layering is correct and then move them // to be the last sibling to enforce the draw order. transforms.Sort((a, b) => b.anchoredPosition.y.CompareTo(a.anchoredPosition.y)); foreach (RectTransform transform in transforms) { transform.SetAsLastSibling(); } }
/// <inheritdoc /> void Start() { // Clear existing weeds. for (int i = 0; i < this.rectTransform.childCount; ++i) { GameObject child = this.rectTransform.GetChild(i).gameObject; if (LawnController.IsWeed(child)) { Destroy(child); } } // Add new weeds. for (int weedIndex = 0; weedIndex < this.lawn.Capacity; ++weedIndex) { PortableItem weed = this.lawn[weedIndex]; if (weed == null) { continue; } Vector2 position; if (layout.FindPosition(this.rectTransform, out position)) { PortableItemController obj = GameObject.Instantiate(this.prefab, this.rectTransform); obj.Initialize(weed); // Set the anchor to the center because rect.min, rect.max, and the // collider are all relative to the center. RectTransform itemTransform = obj.transform as RectTransform; itemTransform.anchorMin.Set(0.5f, 0.5f); itemTransform.anchorMax.Set(0.5f, 0.5f); itemTransform.anchoredPosition = position; } else { // Couldn't place it after 100 attempts; clear it from the inventory so // we don't count it in the score. this.lawn[weedIndex] = null; } } this.OrderLawn(); }