I’m creating an animation importer and the file format can reorder the sprites based on the animation.
The problem I’m running into is that I need I don’t have a way to set the sorting order property.
Here is what I have currently:
int order = 0;
foreach(XmlElement layer in movie)
{
Keyframe[] sortingLayers = {newKeyframe(0.0f, order++)};
clip.SetCurve(reference, typeof(SpriteRenderer), "m_SortingOrder", newAnimationCurve(sortingLayers));
}
I have tried using “m_SortingOrder”, “sortingOrder”, and “SortingOrder” with no success.
Any help/recommendations would be much appreciated.
Thanks!
Alright, I found a solution so I thought I’d post it in case anyone else encounters this issue.
Unity only allows float values to be animated. Sorting order is an int. So a work around is to create a monobehaviour with a float that gets truncated to an int, then sets the sorting order with that int.
public class AnimatableSortingOrder : MonoBehaviour {
private SpriteRenderer sr; /**< The Sprite renderer to set the sort order */
public float sortingLayer = 0.0f; /**< Only floats can be animated, so we set the sorting layer as a float*/
void Start () {
sr = GetComponent<SpriteRenderer>();/**Get the SpriteRenderer*/
}
void Update () {
int layer = (int) sortingLayer; /* Truncate the sorting layer to an int*/
sr.sortingOrder = layer; /* Set the sorting layer in the SpriteRenderer*/
}
}
So I just attach this to the sprite, then the sortingLayer float can be animated with the animator.
Hope this helps.
7 Likes