Skip to content

Instantly share code, notes, and snippets.

@nicloay
Last active May 30, 2016 20:06
Show Gist options
  • Select an option

  • Save nicloay/03f2fab63729c9ac1fda to your computer and use it in GitHub Desktop.

Select an option

Save nicloay/03f2fab63729c9ac1fda to your computer and use it in GitHub Desktop.
DragonBones importer https://drive.google.com/file/d/0B-19PBUPw77UYVN6NVhpclhJUHM/edit?usp=sharing here is a package in case if I missed something.
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;
using animaster;
using System.Linq;
public class DragonImporter : AssetPostprocessor {
void OnPostprocessTexture(Texture2D texture){
string fileName = Path.GetFileNameWithoutExtension(assetPath);
string textureDirectory = Path.GetDirectoryName( assetPath);
string atlasXmlPath = textureDirectory + "/" +fileName+".xml";
string skeletonPath = textureDirectory + "/skeleton.xml";
if(File.Exists(atlasXmlPath) && File.Exists(skeletonPath)) {
Stream fileStream= new FileStream (atlasXmlPath,FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(TextureAtlas));
TextureAtlas atlas = serializer.Deserialize(fileStream) as TextureAtlas;
fileStream.Close();
SpriteMetaData[] spriteSheet = getSpriteMetadataFromXML (atlas, texture);
TextureImporter importer = assetImporter as TextureImporter;
importer.textureType = TextureImporterType.Sprite;
importer.spriteImportMode = SpriteImportMode.Multiple;
importer.spritesheet = spriteSheet;
importer.spritePixelsToUnits = 1;
performSkeleton(skeletonPath, atlas);
}
}
void performSkeleton(string skeletonPath, TextureAtlas textureAtlas){
Stream fileStream= new FileStream (skeletonPath,FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(SkeletonType));
SkeletonType skeleton = serializer.Deserialize(fileStream) as SkeletonType;
Dictionary<string, SubTexture> subTextureByName = new Dictionary<string, SubTexture>();
for (int i = 0; i < textureAtlas.textures.Count; i++) {
subTextureByName.Add(textureAtlas.textures[i].name,textureAtlas.textures[i]);
}
Dictionary<string,Sprite> spriteDict = new Dictionary<string,Sprite >();
Object[] objects = AssetDatabase.LoadAllAssetsAtPath(assetPath);
for (int i = 0; i < objects.Length; i++) {
if (objects[i] is Sprite){
Sprite s = objects[i] as Sprite;
spriteDict.Add(s.name,s);
}
}
Texture2D wtf = AssetDatabase.LoadMainAssetAtPath(assetPath) as Texture2D;
string spritePath = "";
string bonePath;
for (int i = 0; i < skeleton.armature.Length; i++) {
ArmatureType armature = skeleton.armature[i];
GameObject armatureGO = new GameObject(armature.name);
spritePath = armature.name;
resetBonePool();
for (int j = 0; j < armature.bone.Length; j++) {
BoneType bone = armature.bone[j];
bonePath = spritePath+"/"+bone.name;
GameObject boneGO = getBoneFromPool(bone.name);
if (string.IsNullOrEmpty( bone.parent))
boneGO.transform.parent = armatureGO.transform;
else
boneGO.transform.parent = getBoneFromPool( bone.parent).transform;
boneGO.transform.position = new Vector3(bone.transform.x,
bone.transform.y,
0.0f);
}
for (int j = 0; j < armature.skin.slot.Length; j++) {
SlotType slot = armature.skin.slot[j];
if (slot.display.type == "image"){
GameObject go = goPool[slot.name];
Debug.Log("slotname = "+slot.name);
Sprite sprite;
spriteDict.TryGetValue(slot.display.name, out sprite);
if (sprite!=null){
SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
if (sr == null)
sr = go.AddComponent<SpriteRenderer>();
sr.sprite = sprite;
} else {
Debug.LogError("cant find "+slot.display.name);
}
}
}
/*
SpriteRenderer sr = boneGO.AddComponent<SpriteRenderer>();
if (spriteDict.ContainsKey(bonePath)){
Debug.Log("assing bone"+bone.name);
sr.sprite = spriteDict[bonePath];
}
*/
}
}
Dictionary<string,GameObject> goPool;
void resetBonePool(){
goPool = new Dictionary<string, GameObject>();
}
GameObject getBoneFromPool(string goName){
if (goPool ==null)
goPool = new Dictionary<string,GameObject>();
if (goPool.ContainsKey(goName))
return goPool[goName];
GameObject go = new GameObject(goName);
goPool.Add(goName, go);
return go;
}
SpriteMetaData[] getSpriteMetadataFromXML (TextureAtlas atlas, Texture2D texture){
SpriteMetaData[] result = new SpriteMetaData[atlas.textures.Count];
for (int i = 0; i < atlas.textures.Count; i++) {
result[i] = new SpriteMetaData();
result[i].name = atlas.textures[i].name;
result[i].rect = atlas.textures[i].rect;
result[i].rect.y = texture.height - result[i].rect.height - result[i].rect.y;
}
return result;
}
}
using System;
using System.Xml.Serialization;
namespace animaster {
[SerializableAttribute()]
[XmlRootAttribute("dragonBones", Namespace="", IsNullable=false)]
public class SkeletonType {
[XmlAttributeAttribute()]
public string name ;
[XmlAttributeAttribute()]
public string frameRate ;
[XmlAttributeAttribute()]
public string version ;
[XmlElementAttribute("armature")]
public ArmatureType[] armature ;
}
[SerializableAttribute()]
public class ArmatureType {
[XmlAttributeAttribute()]
public string name ;
[XmlElementAttribute("bone")]
public BoneType[] bone;
public SkinType skin;
[XmlElementAttribute("animation")]
public AnimationType[] animation;
}
[SerializableAttribute()]
public class BoneType {
[XmlAttributeAttribute()]
public string name;
[XmlAttributeAttribute()]
public string parent;
public TransformType transform;
}
[SerializableAttribute()]
public class TransformType {
[XmlAttributeAttribute()]
public float x;
[XmlAttributeAttribute()]
public float y;
[XmlAttributeAttribute()]
public float skX;
[XmlAttributeAttribute()]
public float skY;
[XmlAttributeAttribute()]
public float scX;
[XmlAttributeAttribute()]
public float scY;
[XmlAttributeAttribute()]
public float pX;
[XmlAttributeAttribute()]
public float pY;
[XmlTextAttribute()]
public string Value;
}
[SerializableAttribute()]
public class SkinType {
[XmlAttributeAttribute()]
public string name;
[XmlElementAttribute("slot")]
public SlotType[] slot;
}
[SerializableAttribute()]
public class SlotType {
[XmlAttributeAttribute()]
public string name;
[XmlAttributeAttribute()]
public string parent;
[XmlAttributeAttribute()]
public string z;
public DisplayType display;
}
[SerializableAttribute()]
public class DisplayType {
[XmlAttributeAttribute()]
public string name;
[XmlAttributeAttribute()]
public string type;
public TransformType transform;
}
[SerializableAttribute()]
public class AnimationType {
[XmlAttributeAttribute()]
public string name;
[XmlAttributeAttribute()]
public string fadeInTime;
[XmlAttributeAttribute()]
public string duration;
[XmlAttributeAttribute()]
public string scale;
[XmlAttributeAttribute()]
public string loop;
[XmlAttributeAttribute()]
public string tweenEasing;
[XmlElementAttribute("timeline")]
public TimelineType[] timeline;
}
[SerializableAttribute()]
public class TimelineType {
[XmlAttributeAttribute()]
public string name;
[XmlAttributeAttribute()]
public string scale;
[XmlAttributeAttribute()]
public string offset;
[XmlElementAttribute("frame")]
public FrameType[] frame;
}
[SerializableAttribute()]
public class FrameType {
[XmlAttributeAttribute()]
public string z;
[XmlAttributeAttribute()]
public string duration;
public TransformType transform;
}
}
// ------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Mono Runtime Version: 4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
// ------------------------------------------------------------------------------
//
//This source code was auto-generated by MonoXSD
//
namespace Schemas {
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class NewDataSet {
private NewDataSetTextureAtlas[] textureAtlasField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("TextureAtlas", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public NewDataSetTextureAtlas[] TextureAtlas {
get {
return this.textureAtlasField;
}
set {
this.textureAtlasField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class NewDataSetTextureAtlas {
private string nameField;
private string imagePathField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string imagePath {
get {
return this.imagePathField;
}
set {
this.imagePathField = value;
}
}
}
}
using UnityEngine;
using System.Collections;
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace animaster{
public class SubTexture{
[XmlAttribute]
public string name;
[XmlAttribute]
public int x;
[XmlAttribute]
public int y;
[XmlAttribute]
public int width;
[XmlAttribute]
public int height;
public Rect rect{
get{
return new Rect(x,y,width,height);
}
}
}
[Serializable]
public class TextureAtlas {
[XmlAttribute]
public string name;
[XmlAttribute]
public string imagePath;
[XmlElement("SubTexture")]
public List<SubTexture> textures;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment