Created
January 12, 2019 12:11
-
-
Save Radagaisus/b6929290b56d751b3ecc37c519c72951 to your computer and use it in GitHub Desktop.
Static and Moving Depth Sort Behaviours for Unity
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
using UnityEngine; | |
namespace Islands { | |
[ExecuteInEditMode] | |
[RequireComponent(typeof(SpriteRenderer))] | |
public class DynamicDepthSort: MonoBehaviour { | |
/// <summary> | |
/// A reference to the game object’s sprite renderer component. | |
/// </summary> | |
private SpriteRenderer spriteRenderer; | |
// Start is called before the first frame update | |
void Start() { | |
// Get a reference to the sprite renderer component | |
spriteRenderer = GetComponent<SpriteRenderer>(); | |
} | |
// Update is called once per frame | |
void Update() { | |
// Set this game object’s sorting order based on its y-position. This will make | |
// objects in the back show behind objects in the front. | |
// See: https://breadcrumbsinteractive.com/two-unity-tricks-isometric-games/ | |
spriteRenderer.sortingOrder = 0 - (int)transform.position.y; | |
} | |
} | |
} |
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
using UnityEngine; | |
namespace Islands { | |
[ExecuteInEditMode] | |
[RequireComponent(typeof(SpriteRenderer))] | |
public class StaticDepthSort: MonoBehaviour { | |
/// <summary> | |
/// A reference to the game object’s sprite renderer component. | |
/// </summary> | |
private SpriteRenderer spriteRenderer; | |
// Start is called before the first frame update | |
void Start() { | |
// Get a reference to the sprite renderer component | |
spriteRenderer = GetComponent<SpriteRenderer>(); | |
// Set this game object’s sorting order based on its y-position. This will make | |
// objects in the back show behind objects in the front. | |
// See: https://breadcrumbsinteractive.com/two-unity-tricks-isometric-games/ | |
spriteRenderer.sortingOrder = 0 - (int)transform.position.y; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment