/gist:1afa9a717b040a9c51ad Secret
Last active
August 29, 2015 14:07
Star
You must be signed in to star a gist
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
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