Forum rules - please read before posting.

About sorting layers and objects

Hello! I have done some animations with inventory objects and the problem is that in the beginning they are in the **Back **Sorting layer because they start hidden.

But when I do the animation for them I can't change the** Sorting layer,** so objects are hidden by other objects. I need that the turtles will be in front of the chair. I have tried at the timeline and animation window and don't allow me to change sorting layer there. I also try to do with action lists but there is no option to change sorting layer in objects. Also I try to use "Follow sorting map" in the turtles but if I do that turtles don't teleport to their new location.

Help?

Comments

  • Unity doesn't allow for a Sprite Renderer's Sorting Layer to be animated, but you can do it with the help of a custom Action:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionSpriteLayer : Action
        {
    
            public GameObject gameObject;
            public int constantID;
            public string newLayerName;
            private SpriteRenderer spriteRenderer;
    
            public ActionSpriteLayer ()
            {
                this.isDisplayed = true;
                category = ActionCategory.Object;
                title = "Set sprite layer";
            }
    
            public override void AssignValues ()
            {
                spriteRenderer = AssignFile (constantID, gameObject).GetComponent <SpriteRenderer>();
            }
    
            public override float Run ()
            {
                spriteRenderer.sortingLayerName = newLayerName;
                return 0f;
            }
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {
                gameObject = (GameObject) EditorGUILayout.ObjectField ("Sprite renderer:", gameObject, typeof (GameObject), true);
    
                constantID = FieldToID (gameObject, constantID);
                gameObject = IDToField (gameObject, constantID, false);
    
                newLayerName = EditorGUILayout.TextField ("New layer:", newLayerName);
    
                AfterRunningOption ();
            }
    
            #endif
    
        }
    
    }
    

    Install that as in an Action script named ActionSpriteLayer.cs using the instructions in the Manual's "Custom Actions" chapter, and it should then be available in ActionLists as a new "Object: Set sprite layer" Action.

  • It worked :D. Thanks a lot for the code. <3

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.