Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Static and Moving Depth Sort Behaviours for Unity
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;
}
}
}
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