Skip to content

Instantly share code, notes, and snippets.

@toxicFork
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toxicFork/1afa9a717b040a9c51ad to your computer and use it in GitHub Desktop.
Save toxicFork/1afa9a717b040a9c51ad to your computer and use it in GitHub Desktop.
private void PerformSlice()
{
XmlDocument document = new XmlDocument();
document.LoadXml(xmlAsset.text);
XmlElement root = document.DocumentElement;
if (root.Name == "TextureAtlas") {
bool failed = false;
Texture2D texture = AssetDatabase.LoadMainAssetAtPath(importer.assetPath) as Texture2D;
int textureHeight = texture.height;
List<SpriteMetaData> metaDataList = new List<SpriteMetaData>();
foreach (XmlNode childNode in root.ChildNodes)
{
if (childNode.Name == "SubTexture") {
try {
int width = Convert.ToInt32(childNode.Attributes["width"].Value);
int height = Convert.ToInt32(childNode.Attributes["height"].Value);
int x = Convert.ToInt32(childNode.Attributes["x"].Value);
int y = textureHeight - (height + Convert.ToInt32(childNode.Attributes["y"].Value));
SpriteMetaData spriteMetaData = new SpriteMetaData
{
alignment = (int)spriteAlignment,
border = new Vector4(),
name = childNode.Attributes["name"].Value,
pivot = GetPivotValue(spriteAlignment, customOffset),
rect = new Rect(x, y, width, height)
};
metaDataList.Add(spriteMetaData);
}
catch (Exception exception) {
failed = true;
Debug.LogException(exception);
}
}
else
{
Debug.LogError("Child nodes should be named 'SubTexture' !");
failed = true;
}
}
if (!failed) {
importer.spriteImportMode = SpriteImportMode.Multiple;
importer.spritesheet = metaDataList.ToArray();
EditorUtility.SetDirty(importer);
try
{
AssetDatabase.StartAssetEditing();
AssetDatabase.ImportAsset(importer.assetPath);
}
finally
{
AssetDatabase.StopAssetEditing();
Close();
}
}
}
else
{
Debug.LogError("XML needs to have a 'TextureAtlas' root node!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment