public static void SetNextOrder(this SpriteRenderer spriteRenderer, string sortingLayer) { if (spriteRenderer != null) { var nextOrder = SortingOrder.GetNext(sortingLayer); spriteRenderer.sortingLayerName = sortingLayer; spriteRenderer.sortingOrder = nextOrder; } }
private void Awake() { var qtyObjects = GameRandom.Get(MinObjects, MaxObjects); var objectsPositions = ObjectsPositions.GetRandomValuesWithoutRepeat(qtyObjects); foreach (var position in objectsPositions.OrderByDescending(o => o.y)) { var gameObject = Instantiate(Objects.GetRandomValues(1).First()); gameObject.transform.parent = this.transform; gameObject.transform.localPosition = position; var spriteRenderer = gameObject.GetComponent <SpriteRenderer>(); if (spriteRenderer != null) { spriteRenderer.sortingLayerName = SortingOrder.GRASS_OBJECTS; spriteRenderer.sortingOrder = SortingOrder.GetNext(SortingOrder.GRASS_OBJECTS); } } }