Skip to content

Instantly share code, notes, and snippets.

@themangomago
Created October 26, 2023 11:44
Show Gist options
  • Save themangomago/104fdb9659e5ad942fb10ba6c780e1cb to your computer and use it in GitHub Desktop.
Save themangomago/104fdb9659e5ad942fb10ba6c780e1cb to your computer and use it in GitHub Desktop.
Parse Aseprite File for Unity
const Aseprite = require("ase-parser");
const fs = require("fs");
const buff = fs.readFileSync(
"../../Assets/Resources/Textures/Character/Character.ase"
);
const template = `%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d4c0c1394c3b84468e2f971a8c5bcdc, type: 3}
m_Name: <file_name>
m_EditorClassIdentifier:
animationName: <anim_name>
keyFrameTimes:
<anim_times>
keyFrameSpriteIndexes: <anim_frames>
timeStepping: 0.1
totalLength: <anim_length>
loopable: 1
`;
const aseFile = new Aseprite(buff, "somefile.aseprite");
aseFile.parse();
for (let i = 0; i < aseFile.tags.length; i++) {
const tag = aseFile.tags[i];
let frameString = "";
let frameTimeString = "";
let frameTime = 0.0;
for (let j = tag.from; j <= tag.to; j++) {
frameString += j.toString(16).padStart(2, "0");
let duration = aseFile.frames[j].frameDuration / 1000;
frameTime = parseFloat(frameTime.toFixed(2));
frameTimeString += " - " + frameTime + "\n";
frameTime += duration;
}
storeAnimation(tag.name, frameTimeString, frameString, frameTime);
}
function storeAnimation(anim_name, anim_times, anim_frames, anim_length) {
const file_name = "NBAnim_Player_" + anim_name;
anim_times = anim_times.trimEnd();
const replacedTemplate = template
.replace("<file_name>", file_name)
.replace("<anim_name>", anim_name)
.replace("<anim_times>", anim_times)
.replace("<anim_frames>", anim_frames)
.replace("<anim_length>", anim_length);
console.log("Creating " + file_name);
fs.writeFileSync("generated/" + file_name + ".asset", replacedTemplate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment