Search Unity

Using Unity EditorWindow to Create Animations from Spritesheet?

Discussion in 'Getting Started' started by ImranKhalid23, Jan 12, 2022.

  1. ImranKhalid23

    ImranKhalid23

    Joined:
    Nov 13, 2021
    Posts:
    5
    Currently I am working on a 2D game in Unity. I am working on a EditorWindow to slice an imported spritesheet and create animations from these sprites.

    Currently, I have the code to slice the spreadsheet functioning, detailed below for those interested in referencing:
    Code
    Code (CSharp):
    1. public void Slice()
    2. {
    3.    var textures = Selection.GetFiltered<Texture2D>(SelectionMode.Assets);
    4.  
    5.    foreach (var texture in textures)
    6.    {
    7.        ProcessTexture(texture, pixelPerUnit, spriteSize, pivot, alignment);
    8.    }
    9. }
    10.  
    11. static void ProcessTexture(Texture2D texture, int pixelPerUnit,
    12.    Vector2Int spriteSize, Vector2 pivot, Alignment alignment)
    13. {
    14.    string path = AssetDatabase.GetAssetPath(texture);
    15.    {
    16.        TextureImporter textureImporter =
    17.            TextureImporter.GetAtPath(path) as TextureImporter;
    18.  
    19.        //Set characteristics for spritesheet
    20.        textureImporter.textureType = TextureImporterType.Sprite;
    21.        textureImporter.spriteImportMode = SpriteImportMode.Multiple;
    22.        textureImporter.spritePixelsPerUnit = pixelPerUnit;
    23.        textureImporter.filterMode = FilterMode.Point;
    24.        textureImporter.textureCompression = TextureImporterCompression.Uncompressed;
    25.  
    26.        int colCount = texture.width / spriteSize.x;
    27.        int rowCount = texture.height / spriteSize.y;
    28.  
    29.        //Create Spritesheet Metadata based on characteristics
    30.        List<SpriteMetaData> metas = new List<SpriteMetaData>();
    31.        for (int c = 0; c < colCount; c++)
    32.        {
    33.            for (int r = 0; r < rowCount; r++)
    34.            {
    35.                SpriteMetaData meta = new SpriteMetaData();
    36.                meta.rect = new Rect(c * spriteSize.x,
    37.                    r * spriteSize.y,
    38.                    spriteSize.x, spriteSize.y);
    39.  
    40.                meta.name = (rowCount - r - 1) + "-" + c;
    41.                meta.alignment = (int)alignment;
    42.                meta.pivot = pivot;
    43.                metas.Add(meta);
    44.            }
    45.        }
    46.        //Apply the metadata to the spritesheet
    47.        textureImporter.spritesheet = metas.ToArray();
    48.        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    49.    }
    50. }
    The portion that is currently giving me grief is converting this newly sliced spreadsheet into animations through script.

    Currently I can create an empty animation clip in the directory of the spritesheet using the code snippet below:

    string path = AssetDatabase.GetAssetPath(texture);
    string newPath = Path.GetDirectoryName(path);

    AnimationClip clip = new AnimationClip();
    AssetDatabase.CreateAsset(clip, newPath + "\\" + spriteName ".anim");


    I am having difficulty finding out how to add sprites to this newly created animation. I have looked into AnimationCurves and AnimationEvents but I cannot seem to find the step to link a sprite to the animation through the editor.

    If anyone has experience or knowledge regarding the creation of unity animationclips through script, any insight would be greatly appreciated. If any more information is needed on my end, please let me know. This is my first time using this service. Thank you all for your help!
     
    Last edited: Jan 16, 2022