Last Updated on 2021-10-30 by Clay
To make the SortingIndex()
function effective, we need to set the Sorting Layer.
And the Order in Layer is the order we can change with program. The higher number, the higher layer the object will be, and the non-transparent objects will cover the below objects.
Sample Code
In order to specify the order in the layer, we need to use the SpriteRenderer
property of the object. Assume the order is i
:
為了要指定在層(layer)中的順序,我們需要使用物件的 SpriteRenderer
屬性。在指定順序為 i
的情況下:
gameObject.GetComponent<SpriteRenderer>().sortingOrder = i;
References
- https://docs.unity3d.com/ScriptReference/Renderer-sortingOrder.html
- https://forum.unity.com/threads/sort-an-integer-array-and-return-the-index-values-in-order-of-size-javascript.506879/
- https://docs.unity3d.com/Manual/2DSorting.html