Hi There,
I have just purchased Spine and starting with my first project with it, using Unity3d tk2d runtime.
As you know, tk2d sprite collections are using sorting orders to control the orders how the sprites are rendered. However, when my spine skeleton animation is imported into our project, I cannot find where to to set its sorting order. (in tk2d world, it's normally set in tk2dSprite components) As a result, the feet of the character is blocked by the ground objects.
So how shall I set the tk2d sorting order?
Thank you!
After studied the code of tk2d...I managed to add the feature myself!
In u3d 4.3 and later versions, Renderer object is already supporting the sorting layer. So we just need below:
Add below properties into SkeletonRenderer:
Renderer _cachedRenderer = null;
Renderer CachedRenderer {
get {
if (_cachedRenderer == null) {
_cachedRenderer = renderer;
}
return _cachedRenderer;
}
}
[SerializeField] protected int renderLayer = 0;
/// <summary>
/// Gets or sets the sorting order
/// The sorting order lets you override draw order for sprites which are at the same z position
/// It is similar to offsetting in z - the sprite stays at the original position
/// This corresponds to the renderer.sortingOrder property in Unity 4.3
/// </summary>
public int SortingOrder {
get {
return CachedRenderer.sortingOrder;
}
set {
if (CachedRenderer.sortingOrder != value) {
renderLayer = value; // for awake
CachedRenderer.sortingOrder = value;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(CachedRenderer);
#endif
}
}
}
In SkeletonRendererInspector class, at the end of gui() method, add below:
int sortingOrder = EditorGUILayout.IntField("Sorting Order", component.SortingOrder);
if (sortingOrder != component.SortingOrder) {
component.SortingOrder = sortingOrder;
}
Then we can control the set the sorting order of the animation in editor inspector.
And in another way, you don't have to do any of the above. Just in the script, directly update the sortingOrder of the renderer object by code...