Last active
January 6, 2020 08:51
-
-
Save MattRix/8d884031c4f4ce43a78ce533f7d36522 to your computer and use it in GitHub Desktop.
Export assets from a Flash fla as a spritesheet and automatically repopulate the Unity atlas metadata with the correct sprite dimensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://help.adobe.com/en_US/flash/cs/extend/index.html | |
var outputScaleFactor = 1; | |
var libraryFolderName = "Export"; | |
var shouldExportSubfolderNames = true; | |
main(); | |
function main() | |
{ | |
fl.outputPanel.clear(); | |
var doc = fl.getDocumentDOM(); | |
if(!doc.pathURI) //check if this document has never been saved | |
{ | |
alert("Export failed! We are in an untitled document!"); | |
return; | |
} | |
var newDoc = fl.createDocument(); | |
var folderURI = doc.pathURI.substring(0, doc.pathURI.lastIndexOf("/")); | |
var docName = doc.pathURI.substring(doc.pathURI.lastIndexOf("/")+1,doc.pathURI.lastIndexOf(".")); | |
folderURI = folderURI+"/"; | |
FLfile.createFolder(folderURI); //make the export folder if it doesn't exist | |
var ss = new SpriteSheetExporter(); | |
ss.beginExport(); | |
ss.layoutFormat = "JSON"; | |
ss.shapePadding = 4; | |
ss.borderPadding = 4; | |
ss.allowTrimming = false; //could try to support this later | |
var namesToReplace = {}; | |
for(i in doc.library.items) | |
{ | |
var currentItem = doc.library.items[i]; | |
if(currentItem.itemType == "graphic" || | |
currentItem.itemType == "bitmap" || | |
currentItem.itemType == "movie clip") | |
{ | |
exportItemAsPng(currentItem, outputScaleFactor); | |
} | |
} | |
function exportItemAsPng(item, scaleFactor) | |
{ | |
if(item.name.indexOf(libraryFolderName) == -1) return; | |
var itemName; | |
if(shouldExportSubfolderNames) | |
{ | |
itemName = item.name.substring(item.name.indexOf("/")+1,item.name.length); | |
} | |
else | |
{ | |
itemName = item.name.substring(item.name.lastIndexOf("/")+1,item.name.length); | |
} | |
var exportPath = folderURI + "/" + itemName; | |
newDoc.addItem({x:0.0, y:0.0}, item); | |
newDoc.library.selectItem(item.name, false); | |
newDoc.scaleSelection(scaleFactor, scaleFactor); | |
//use the instance instead of the library item so we can give it a unique name | |
var instance = newDoc.selection[0]; | |
if(item.itemType == "movie clip") | |
{ | |
ss.addSymbol(instance, itemName); | |
var topLayer = item.timeline.layers[0]; | |
for(var f = 0; f<item.timeline.frameCount; f++) | |
{ | |
var zeropad = ("0000" + f).slice(-4); | |
if(topLayer.name == "names") | |
{ | |
var frame = topLayer.frames[f]; | |
if(frame && frame.startFrame == f && frame.name && frame.name != "") | |
{ | |
namesToReplace[itemName+zeropad] = itemName+"_"+frame.name; | |
} | |
else | |
{ | |
namesToReplace[itemName+zeropad] = itemName+"_"+f; | |
} | |
} | |
else | |
{ | |
if(item.timeline.frameCount == 1) //don't add frame number unless it has multiple frames | |
{ | |
namesToReplace[itemName+zeropad] = itemName; | |
} | |
else | |
{ | |
namesToReplace[itemName+zeropad] = itemName+"_"+f; | |
} | |
} | |
} | |
} | |
newDoc.deleteSelection(); | |
} | |
var docPath = folderURI+"/"+docName; | |
var ssdata = ss.exportSpriteSheet(docPath,{format:"png", bitDepth:32, backgroundColor:"#00000000"},true); | |
fl.closeDocument(newDoc, false); | |
//remove the json file from the disk since we already have the data we need | |
FLfile.remove(docPath+".json"); | |
eval("var ssjson = " + ssdata); //parse the json using eval | |
if(!ssjson) //there were no items found | |
{ | |
alert("No items found! You must put the stuff you want to export in a library folder called '"+libraryFolderName+"'"); | |
return; | |
} | |
for(var frameName in ssjson.frames) | |
{ | |
if(namesToReplace[frameName]) | |
{ | |
ssjson.frames[namesToReplace[frameName]] = ssjson.frames[frameName]; | |
delete(ssjson.frames[frameName]); | |
} | |
} | |
var atlasHeight = ssjson.meta.size.h; | |
var umeta = FLfile.read(docPath+".png.meta"); | |
if(!umeta || umeta == "") | |
{ | |
fl.trace("We created the atlas but could not fine the unity .png.meta file. Set the png to be a spritesheet asset in Unity."); | |
return; | |
} | |
var startIndex = umeta.indexOf("sprites:")+8; | |
var endIndex = umeta.indexOf("spritePackingTag:"); | |
var content = ""; | |
for(var frameName in ssjson.frames) | |
{ | |
var frameContent = ssjson.frames[frameName]; | |
//we have to flip the frameY because unity's tex coord Y is inverted | |
var frameY = (atlasHeight-frameContent.frame.y)-frameContent.frame.h; | |
content += "\n - name: " + frameName; | |
content += "\n rect: "; | |
content += "\n serializedVersion: 2"; | |
content += "\n x: " + frameContent.frame.x; | |
content += "\n y: " + frameY; | |
content += "\n width: "+ frameContent.frame.w; | |
content += "\n height: "+ frameContent.frame.h; | |
content += "\n alignment: 0"; | |
content += "\n pivot: {x: 0.5, y: 0.5}"; | |
content += "\n border: {x: 0, y: 0, z: 0, w: 0}"; | |
} | |
var result = umeta.substring(0,startIndex) + content + "\n" + umeta.substring(endIndex,umeta.length); | |
FLfile.write(docPath+".png.meta",result); | |
fl.trace(docName + " exported successfully!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment