Search Unity

Question Automated creation of .anim files from sliced PNGs

Discussion in 'Animation' started by Inksuki, Sep 6, 2023.

  1. Inksuki


    Mar 17, 2022
    Hello all, i'll try to be succinct in how i explain and ask this question. Here goes!

    I currently have a character who can "equip" armor. I handle this via an armor scriptable object that contains an array of animations for the specific armor. When the armor is "equipped" a new script loads all the animations from the array into an animation override controller for the appropriate layer of my character. All is well in the world, and works fine. I can equip pants, boots, gloves, shirts, skins, tattoos, etc etc. And all the layers work in harmony via sorting layers and is perfect.

    Now, say i want to create a new piece of armor. I draw my art, slice the PNG files appropriately (still waiting for the option to bulk slice files as well), and I am then faced with the onerous task of creating some 67 new animation files to load into my SO's animation array.

    Each animation is going to be the exactly the same structure as any other previous armor piece i made. For instance, an idle down animation knows that the png's slice 1 goes at time stamp 0, slice 2 at time stamp 16, etc etc.

    Hopefully this is clear. Since i have a "blueprint" for each .anim file, and all i'm doing is replacing the specific png at a timestamp for a frame, I'm asking myself how can i automate this? Currently, making 67 animations every time i draw a new pair of pants, or shirt, takes me several hours of painstaking .anim creation and testing to make sure the frames all line up and there was no human error. For a project that i want to have 100s if not 1000s of different pieces of gear to equip, this will end up taking months, if not years, worth of time to do just this.

    How do i go about solving this through code? Do i create a scriptable wizard or an editor script or anything that can say "Take folder of PNG files, process it into 67 animations." Are there any plugins that i could purchase from the store that solve this (i didn't find anything on my rudimentary searching).

    Is this ask even possible? A game dev discord i'm a member of told me that i should redesign my entire process to not even use .anim files, but that is really drastic, i just want to know how to auto create anim files if i know what the final product should already look like.
  2. tsukimi


    Dec 10, 2014
    Yes, you totally can do it through code.
    Here are the steps that how I would do it:
    1. Load your sprite from the png by AssetDatabase.LoadAssetAtPath()
      1. If you have multiple sprites in one png, you can load all of them by LoadAllAssetsAtPath()
    2. Copy the blueprint anim by: AssetDatabase.CopyAsset()
    3. Get the ObjectCurves from the new anim by: AnimationUtility.GetObjectReferenceCurveBindings() and AnimationUtility.GetObjectReferenceCurve()
    4. Find your target ObjectRefCurve, and set your sprites to the ObjectRefCurve
    5. Set it back to the anim by: AnimationUtility.SetObjectReferenceCurve()
    6. Done!
    Inksuki likes this.
  3. Inksuki


    Mar 17, 2022
    Awesome! tyvm. I was also starting to look into aseprite importer from unity as that creates anim files automatically, so it looks like i have options! Thanks for your response @tsukimi!
    tsukimi likes this.