wuxianshengcong/Library/PackageCache/com.unity.2d.aseprite@1.1.6/Documentation~/ImporterFAQ.md
2025-01-02 14:50:41 +08:00

4.9 KiB

Frequently asked questions

How are the layers sorted in Unity?

When an Aseprite file is imported using the Import Mode: Individual, every layer from the source file becomes a GameObject inside the generated model prefab. If the layer is a normal layer (and not a group layer), it has a SpriteRenderer compontent added to it. To make sure each SpriteRenderer renders in the same order as inside of Aseprite, Unity automatically assigns a value to the Order in Layer field on the SpriteRenderer component. This value is a combination of two variables, the order of the layer inside of Aseprite, where the bottom layer has the value of 0 and the top layer has the value of Number of layers, e.g. if the source file contains 5 layers, the top layer will have the value of 5. The second value is the z-index, which can be set per cell in Aseprite. The final formula to calculate the Order in Layer value is: Layer count from the bottom + Z-Index of the current cell.

Apart from the Order in Layer value, Unity also adds a Sorting Group component onto the root GameObject of the model prefab, to make sure that every SpriteRenderer within the prefab is sorted together.

For more information about general 2D sorting in Unity, see the 2D Sorting page in the Unity manual.

How to combine multiple sprite sheets into one?

You can make use of Sprite Atlases to combine multiple sprite sheets into a single texture. Read more about Sprite Atlas here. Combining multiple sprite sheets into one is a good way to reduce the draw calls in a scene.

Why is my trimmed Aseprite file not trimmed in Unity?

When modifying the canvas size, make sure the Trim content outside the canvas checkbox is checked. This way, the stored texture will be cropped to the specified size.

How to add events to Animation Clips?

Animation Events can be generated by adding user data to Cels inside of Aseprite.

Follow these steps to add an Animation Event to a frame:

  • In Aseprite, select any cel in the frame you want to add an event to.
  • Right click on the cel and select Cel Properties.

  • Press the User Data-button to the left of the Opacity slider to bring up the User Data-field.

  • Enter the event name in the following format: event:EventName. E.g. event:OnIdle.

  • Save the file and switch over to Unity.

  • Open the Animation Window and inspect the Animation Clip. You can see that the event has been added to the frame.

How to make changes to an Animator Controller?

The Aseprite Importer generates an Animator Controller if the Aseprite file contains more than one frame and the Animation Clip checkbox is checked in the importer. This Animator Controller is Read Only, meaning that it cannot be changed.

The Animator Controller is Read Only.

If you like to have an Animator Controller which you can change, follow these steps:

  • Select an Aseprite file in Unity.
  • Press the Export Animation Assets-button.
  • In the popup, make sure the Animator Controller checkbox is checked. Leave the Animation Clips checkbox unchecked if you do not wish to edit any of the clips.
  • Press Export and select a folder to place the Asset(s) into.

An Animator Controller should now be located in the selected folder. If the Animation Clip checkbox was left unchecked, all the states within the Animator Controller are linked back to the Aseprite file, meaning that the clips will stay up to date with any changes made in Aseprite.

Do note that if you add a new tag in Aseprite, you need to add the resulting Animation Clip to the exported Animator Controller, as this will not happen automatically.

How to inject custom assets on import?

The Aseprite Importer comes with an event, OnPostAsepriteImport, which is fired at the end of the import process. This event can be used to inject or change the generated assets when importing an Aseprite file.

using UnityEditor;
using UnityEditor.U2D.Aseprite;
using UnityEngine;

public class GameObjectInjector : AssetPostprocessor
{
    void OnPreprocessAsset()
    {
        if (assetImporter is AsepriteImporter aseImporter)
            aseImporter.OnPostAsepriteImport += OnPostAsepriteImport;
    }

    static void OnPostAsepriteImport(AsepriteImporter.ImportEventArgs args)
    {
        var myGo = new GameObject("MyGameObject");
        args.context.AddObjectToAsset(myGo.name, myGo);
    }
}