//using a continous distribution between [ymin,ymax] to calculate the orderInLayer value int CalculateLayer(SpriteRenderer sprite) { //get the y value of the bottom of the sprite Vector2 position = sprite.transform.position; float realY = sprite.transform.position.y - (sprite.bounds.size.y / 2f); //check if there is an offset for the sprite to use SpriteOffset offset = sprite.GetComponent <SpriteOffset>(); if (offset != null) { realY += offset.spriteOffset; } float actualY = RoundFloat(realY, sensitivity); //convert the y value in a scale between [0,1] float fit = fittingConstant * (actualY - yMin); //invert the fitness value fit = 1f - fit; //round fit to sensitivity decimal fit = RoundFloat(fit, sensitivity); //calculate the order in layer removing floating point to fit value float orderInLayer = fit * Mathf.Pow(10f, sensitivity); return((int)RoundFloat(orderInLayer, 0)); }
private void Awake() { sprOffset = GetComponent <SpriteOffset>(); stdColor = sprOffset.Color; textureVertSize = transform.lossyScale.y; }