Last active
October 24, 2016 20:09
-
-
Save desplesda/1d60486ca47daae8aeeeb4cf98816e14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sorting Layer Offset, for Unity sprites. By Jon Manning (@desplesda) at Secret Lab! http://secretlab.com.au | |
// This code is released under the CC-0 license. https://creativecommons.org/publicdomain/zero/1.0/ | |
// You can do whatever you like with it, but neither I nor Secret Lab offer any support for it. | |
using UnityEngine; | |
using System.Collections; | |
// Shift all sorting layers based on Z position - the farther they are in Z, | |
// the lower their sorting order will be! | |
public class SortingLayerOffsetter : MonoBehaviour { | |
// Increase this if you need sorting layers to be offset more | |
static int multiplier = 10; | |
// Do this first so that the player never suspects a thing | |
void Awake () { | |
// Figure out how far into the back we're moving each sprite | |
int offset = (int)(transform.position.z * multiplier); | |
// Get every single sprite renderer | |
var allSpriteRenderers = GetComponentsInChildren<SpriteRenderer>(includeInactive:true); | |
// Shift it back based on how far it is in Z | |
foreach (var renderer in allSpriteRenderers) { | |
renderer.sortingOrder -= offset; | |
} | |
// NICE. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment