Beispiel #1
0
 void Start()
 {
     _unityArmature = GetComponent <Armature>();
     if (_unityArmature.isUGUI)
     {
         Destroy(gameObject);
         return;
     }
     _mesh = new Mesh();
     _mesh.MarkDynamic();
     Init();
 }
Beispiel #2
0
 void DisplayEnable(Armature armature, bool flag)
 {
     foreach (Slot slot in armature.slots)
     {
         if (slot.childArmature != null)
         {
             DisplayEnable(slot.childArmature, flag);
         }
         else if (slot.currentDisplay)
         {
             MeshRenderer mr = slot.currentDisplay.GetComponent <MeshRenderer>();
             if (mr)
             {
                 mr.enabled = flag;
             }
         }
     }
 }
Beispiel #3
0
        void CollectMesh(Armature armature, List <List <CombineInstance> > combines, List <Material> mats)
        {
            List <Slot> slots = armature.sortedSlots;

            foreach (Slot slot in slots)
            {
                if (slot.currentDisplay)
                {
                    if (slot.childArmature != null)
                    {
                        CollectMesh(slot.childArmature, combines, mats);
                        continue;
                    }

                    var meshRenderer = slot.currentDisplay.GetComponent <MeshRenderer>();
                    if (meshRenderer != null && meshRenderer.sharedMaterial)
                    {
                        if (mats.Count == 0 || mats[mats.Count - 1] != meshRenderer.sharedMaterial)
                        {
                            mats.Add(meshRenderer.sharedMaterial);
                        }
                        if (combines.Count < mats.Count)
                        {
                            combines.Add(new List <CombineInstance>());
                        }
                    }
                    else
                    {
                        continue;
                    }
                    var meshFilter = slot.currentDisplay.GetComponent <MeshFilter>();
                    if (meshFilter && meshFilter.sharedMesh)
                    {
                        CombineInstance com = new CombineInstance();
                        com.mesh      = meshFilter.sharedMesh;
                        com.transform = transform.worldToLocalMatrix * meshRenderer.transform.localToWorldMatrix;
                        combines[mats.Count - 1].Add(com);
                    }
                }
            }
        }
        void OnEnable()
        {
            armature = target as Armature;
            if (armature == null)
            {
                return;
            }
            sortingLayerNames = GetSortingLayerNames();
            selectedOption    = GetSortingLayerIndex(armature.sortingLayerName);
            flipX             = armature.flipX;
            flipY             = armature.flipY;
            zspace            = armature.zSpace;
            sortType          = armature.sortType;

            if (armature.textureFrames && armature.textureFrames.materials != null && armature.textureFrames.materials.Length > 0 && armature.textureFrames.materials[0] != null)
            {
                preMultiplyAlpha = armature.textureFrames.materials[0].GetFloat("_BlendSrc") == (int)UnityEngine.Rendering.BlendMode.One;
            }
            armature.preMultiplyAlpha = preMultiplyAlpha;

            Initialize();
        }
Beispiel #5
0
        public static void AddSlot(SpineArmatureEditor armatureEditor)
        {
            m_rootSlot = null;
            if (armatureEditor.armatureData.slots != null)
            {
                armatureEditor.slotsKV.Clear();
                int      len      = armatureEditor.armatureData.slots.Length;
                Armature armature = armatureEditor.armature.GetComponent <Armature>();
                for (int i = 0; i < len; ++i)
                {
                    SpineData.SlotData slotData = armatureEditor.armatureData.slots[i];
                    GameObject         go       = new GameObject(slotData.name);
                    armatureEditor.slotsKV[slotData.name] = go.transform;

                    Slot slot = go.AddComponent <Slot>();
                    slot.zOrder    = i;
                    slot.armature  = armature;
                    slot.blendMode = slot.ConvertBlendMode(slotData.blend.ToLower());
                    armatureEditor.slots.Add(slot);
                    slot.color = slotData.color;
                }
            }
        }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (um.vertControlTrans != null && um.vertControlTrans.Length > 0)
            {
                foreach (Transform child in um.vertControlTrans)
                {
                    if (um.isEdit)
                    {
                        if (child.gameObject.activeSelf)
                        {
                            break;
                        }
                        else
                        {
                            child.gameObject.SetActive(true);
                        }
                    }
                    else
                    {
                        if (!child.gameObject.activeSelf)
                        {
                            break;
                        }
                        else
                        {
                            child.gameObject.SetActive(false);
                        }
                    }
                }
            }
            //Modify bindPose by hand
            if (um.bindposes != null && um.bindposes.Length > 0)
            {
                GUILayout.Space(20f);
                GUILayout.Label("Press shift to speed up");
                GUILayout.BeginHorizontal();
                if (GUILayout.Button("BindPose Left Move"))
                {
                    for (int i = 0; i < um.bindposes.Length; ++i)
                    {
                        Matrix4x4 m = um.bindposes[i];
                        m.m03          -= Event.current.shift ? 0.1f:0.01f;
                        um.bindposes[i] = m;
                    }
                    um.UpdateSkinnedMesh();
                    if (!Application.isPlaying)
                    {
                        UnityEditor.EditorUtility.SetDirty(um);
                    }
                }
                if (GUILayout.Button("BindPose Right Move"))
                {
                    for (int i = 0; i < um.bindposes.Length; ++i)
                    {
                        Matrix4x4 m = um.bindposes[i];
                        m.m03          += Event.current.shift ? 0.1f:0.01f;
                        um.bindposes[i] = m;
                    }
                    um.UpdateSkinnedMesh();
                    if (!Application.isPlaying)
                    {
                        UnityEditor.EditorUtility.SetDirty(um);
                    }
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                if (GUILayout.Button("BindPose Up Move"))
                {
                    for (int i = 0; i < um.bindposes.Length; ++i)
                    {
                        Matrix4x4 m = um.bindposes[i];
                        m.m13          += Event.current.shift ? 0.1f:0.01f;
                        um.bindposes[i] = m;
                    }
                    um.UpdateSkinnedMesh();
                    if (!Application.isPlaying)
                    {
                        UnityEditor.EditorUtility.SetDirty(um);
                    }
                }
                if (GUILayout.Button("BindPose Down Move"))
                {
                    for (int i = 0; i < um.bindposes.Length; ++i)
                    {
                        Matrix4x4 m = um.bindposes[i];
                        m.m13          -= Event.current.shift ? 0.1f:0.01f;
                        um.bindposes[i] = m;
                    }
                    um.UpdateSkinnedMesh();
                    if (!Application.isPlaying)
                    {
                        UnityEditor.EditorUtility.SetDirty(um);
                    }
                }
                GUILayout.EndHorizontal();


                GUILayout.BeginHorizontal();
                if (GUILayout.Button("BindPose Rotate +"))
                {
                    Matrix4x4 rotate = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0, 0, Event.current.shift ? 1f:0.1f), Vector3.one);
                    for (int i = 0; i < um.bindposes.Length; ++i)
                    {
                        Matrix4x4 m = um.bindposes[i];
                        um.bindposes[i] = m * rotate;
                    }
                    um.UpdateSkinnedMesh();
                    if (!Application.isPlaying)
                    {
                        UnityEditor.EditorUtility.SetDirty(um);
                    }
                }
                if (GUILayout.Button("BindPose Rotate -"))
                {
                    Matrix4x4 rotate = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0, 0, -(Event.current.shift ? 1f:0.1f)), Vector3.one);
                    for (int i = 0; i < um.bindposes.Length; ++i)
                    {
                        Matrix4x4 m = um.bindposes[i];
                        um.bindposes[i] = m * rotate;
                    }
                    um.UpdateSkinnedMesh();
                    if (!Application.isPlaying)
                    {
                        UnityEditor.EditorUtility.SetDirty(um);
                    }
                }
                GUILayout.EndHorizontal();
            }
            if (!Application.isPlaying && um.isEdit)
            {
                GUILayout.Space(5);
                if (GUILayout.Button("Save To Pose"))
                {
                    Armature armature = um.GetComponentInParent <Armature> ();
                    if (armature && armature.poseData)
                    {
                        for (int i = 0; i < armature.uiAttachments.Length; ++i)
                        {
                            UnityEngine.UI.MaskableGraphic render = armature.uiAttachments [i];
                            if (um.gameObject == render.gameObject)
                            {
                                PoseData.DisplayData dd = armature.poseData.displayDatas [i];
                                dd.color  = um.color;
                                dd.vertex = (Vector3[])um.vertices.Clone();
                                armature.poseData.displayDatas [i] = dd;

                                AssetDatabase.Refresh();
                                EditorUtility.SetDirty(armature.poseData);
                                AssetDatabase.SaveAssets();
                                break;
                            }
                        }
                    }
                }
            }
        }
Beispiel #7
0
        public void UpdateDisplayColor(Color col)
        {
            if (armature == null)
            {
                return;
            }

            Color rc = col;

            rc.r *= armature.color.r;
            rc.g *= armature.color.g;
            rc.b *= armature.color.b;
            rc.a *= armature.color.a;

            if (armature.isUGUI)
            {
                if (m_UIFrame)
                {
                    if (m_UIFrame.gameObject.activeSelf)
                    {
                        m_UIFrame.m_PreMultiplyAlpha = armature.m_PreMultiplyAlpha;
                        if (!m_UIFrame.color.Equals(rc))
                        {
                            m_UIFrame.m_ColorIsDirty = true;
                            m_UIFrame.color          = rc;
                        }
                        m_UIFrame.UpdateFrame();
                    }
                }
                else if (m_UIMesh)
                {
                    if (m_UIMesh.gameObject.activeSelf)
                    {
                        m_UIMesh.m_PreMultiplyAlpha = armature.m_PreMultiplyAlpha;
                        if (!m_UIMesh.color.Equals(rc))
                        {
                            m_UIMesh.m_ColorIsDirty = true;
                            m_UIMesh.color          = rc;
                        }
                        m_UIMesh.UpdateMesh();
                        if (m_UIMesh.mesh)
                        {
                            m_UIMesh.rectTransform.sizeDelta = (Vector2)m_UIMesh.mesh.bounds.size;
                        }
                    }
                }
                else if (m_Image)
                {
                    if (m_Image.gameObject.activeSelf)
                    {
                        Color c = rc;
                        if (armature.m_PreMultiplyAlpha)
                        {
                            c.r *= c.a;
                            c.g *= c.a;
                            c.b *= c.a;
                        }
                        m_Image.color = c;
                    }
                }
                else if (m_Armature)
                {
                    Color    c = color;
                    Armature parentArmature = m_Armature.parentArmature;
                    while (parentArmature)
                    {
                        c.r           *= parentArmature.color.r;
                        c.g           *= parentArmature.color.g;
                        c.b           *= parentArmature.color.b;
                        c.a           *= parentArmature.color.a;
                        parentArmature = parentArmature.parentArmature;
                    }
                    m_Armature.color = c;
                }
            }
            else
            {
                if (m_SpriteFrame)
                {
                    if (m_SpriteFrame.gameObject.activeSelf)
                    {
                        m_SpriteFrame.m_PreMultiplyAlpha = armature.m_PreMultiplyAlpha;
                        m_SpriteFrame.color = rc;
                        m_SpriteFrame.UpdateFrame();
                    }
                }
                else if (m_SpriteMesh)
                {
                    if (m_SpriteMesh.gameObject.activeSelf)
                    {
                        m_SpriteMesh.m_PreMultiplyAlpha = armature.m_PreMultiplyAlpha;
                        m_SpriteMesh.color = rc;
                        m_SpriteMesh.UpdateMesh();
                    }
                }
                else if (m_SpriteRenderer)
                {
                    if (m_SpriteRenderer.gameObject.activeSelf)
                    {
                        Color c = rc;
                        if (armature.m_PreMultiplyAlpha)
                        {
                            c.r *= c.a;
                            c.g *= c.a;
                            c.b *= c.a;
                        }
                        m_SpriteRenderer.color = c;
                    }
                }
                else if (m_Armature)
                {
                    Color    c = color;
                    Armature parentArmature = m_Armature.parentArmature;
                    while (parentArmature)
                    {
                        c.r           *= parentArmature.color.r;
                        c.g           *= parentArmature.color.g;
                        c.b           *= parentArmature.color.b;
                        c.a           *= parentArmature.color.a;
                        parentArmature = parentArmature.parentArmature;
                    }
                    m_Armature.color = c;
                }
            }
        }
Beispiel #8
0
        protected internal void UpdateCurrentDisplay()
        {
            m_SpriteFrame    = null;
            m_SpriteMesh     = null;
            m_SpriteRenderer = null;
            m_Armature       = null;
            m_Image          = null;
            m_UIFrame        = null;
            m_UIMesh         = null;
            m_CurrentDisplay = null;
            if (armature == null)
            {
                return;
            }

            if (__displayIndex > -1 && transform.childCount > 0)
            {
                Transform skin = (armature.skins == null || armature.skins.Length <= 1) ?transform :transform.Find(armature.skinName);
                if (skin && skin.childCount > 0)
                {
                    Transform child = skin.GetChild(__displayIndex);
                    m_CurrentDisplay = child.gameObject;
                    if (armature.isUGUI)
                    {
                        m_UIFrame = child.GetComponent <UIFrame>();
                        if (!m_UIFrame)
                        {
                            m_UIMesh = child.GetComponent <UIMesh>();
                            if (!m_UIMesh)
                            {
                                m_Image = child.GetComponent <Image>();
                                if (!m_Image)
                                {
                                    m_Armature = child.GetComponent <Armature>();
                                }
                            }
                        }
                    }
                    else
                    {
                        m_SpriteFrame = child.GetComponent <SpriteFrame>();
                        if (!m_SpriteFrame)
                        {
                            m_SpriteMesh = child.GetComponent <SpriteMesh>();
                            if (!m_SpriteMesh)
                            {
                                m_SpriteRenderer = child.GetComponent <SpriteRenderer>();
                                if (!m_SpriteRenderer)
                                {
                                    m_Armature = child.GetComponent <Armature>();
                                }
                            }
                        }
                        UpdateZOrder();
                    }
                }
            }
            else if (armature.skins != null && armature.skins.Length > 1)
            {
                for (int i = 0; i < transform.childCount; ++i)
                {
                    transform.GetChild(i).gameObject.SetActive(false);
                }
            }
        }
Beispiel #9
0
        public static void ShowSkin(SpineArmatureEditor armatureEditor)
        {
            if (armatureEditor.armatureData.skins != null)
            {
                Armature armature = armatureEditor.armature.GetComponent <Armature>();

                Dictionary <Texture, List <SpriteMetaData> > metaDatas = new Dictionary <Texture, List <SpriteMetaData> >();
                List <SpriteRenderer> sprites = new List <SpriteRenderer>();
                List <Image>          images  = new List <Image>();

                int len = armatureEditor.armatureData.skins.Length;
                for (int i = 0; i < len; ++i)
                {
                    SpineData.SkinData skinData = armatureEditor.armatureData.skins[i];
                    foreach (string slotName in skinData.slots.Keys)
                    {
                        Transform slot       = armatureEditor.slotsKV[slotName];
                        Transform skinParent = slot;
                        if (len > 1)
                        {
                            skinParent = slot.Find(skinData.skinName);
                            if (!skinParent)
                            {
                                GameObject skinParentGo = new GameObject(skinData.skinName);
                                skinParentGo.transform.parent        = slot;
                                skinParentGo.transform.localScale    = Vector3.one;
                                skinParentGo.transform.localPosition = Vector3.zero;
                                skinParentGo.transform.localRotation = Quaternion.identity;
                                skinParent = skinParentGo.transform;
                                skinParent.gameObject.SetActive(i == 0);
                            }
                        }
                        SpineData.SlotData slotData = armatureEditor.slotsDataKV[slotName];
                        List <SpineData.SkinAttachment> attachmentDataArr = skinData.slots[slotName];
                        for (int j = 0; j < attachmentDataArr.Count; ++j)
                        {
                            SpineData.SkinAttachment attachmentData = attachmentDataArr[j];
                            TextureFrame             frame          = armatureEditor.m_TextureFrames.GetTextureFrame(attachmentData.textureName);
                            if (attachmentData.type == "region")                         //region,mesh,linkedmesh,boundingBox,path
                            {
                                if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.Default)
                                {
                                    ShowSpriteFrame(frame, attachmentData, slot, skinParent, armatureEditor);
                                }
                                else if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.SpriteRender ||
                                         armatureEditor.displayType == Bone2DSetupEditor.DisplayType.UGUIImage)
                                {
                                    SpriteMetaData metaData = new SpriteMetaData();
                                    metaData.name      = attachmentData.textureName;
                                    metaData.rect      = frame.rect;
                                    metaData.rect.y    = frame.texture.height - metaData.rect.y - metaData.rect.height;
                                    metaData.alignment = (int)SpriteAlignment.Custom;
                                    metaData.pivot     = Vector2.one * 0.5f;
                                    if (!metaDatas.ContainsKey(frame.texture))
                                    {
                                        metaDatas[frame.texture] = new List <SpriteMetaData>();
                                    }
                                    metaDatas[frame.texture].Add(metaData);

                                    if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.SpriteRender)
                                    {
                                        SpriteRenderer sr = ShowUnitySprite(attachmentData, slot, skinParent, metaData, frame);
                                        if (armatureEditor.genMeshCollider)
                                        {
                                            sr.gameObject.AddComponent <BoxCollider2D>();
                                        }
                                        sprites.Add(sr);
                                    }
                                    else
                                    {
                                        Image img = ShowUIImage(attachmentData, slot, skinParent, metaData, frame);
                                        if (armatureEditor.genMeshCollider)
                                        {
                                            img.gameObject.AddComponent <BoxCollider2D>();
                                        }
                                        images.Add(img);
                                    }
                                }
                                else if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.UGUIDefault)
                                {
                                    ShowUIFrame(frame, attachmentData, slot, skinParent, armatureEditor, slotData);
                                }
                            }
                            else if (attachmentData.type == "mesh")
                            {
                                if (frame.rect.width > 0 && frame.rect.height > 0)
                                {
                                    if (armature.isUGUI)
                                    {
                                        ShowUIMesh(frame, attachmentData, slot, skinParent, armatureEditor);
                                    }
                                    else
                                    {
                                        ShowSpriteMesh(frame, attachmentData, slot, skinParent, armatureEditor);
                                    }
                                }
                            }
                            else if (attachmentData.type == "boundingbox")
                            {
                                ShowCustomCollider(attachmentData, slot, skinParent, armatureEditor);
                            }

                            if (string.IsNullOrEmpty(slotData.attachment))
                            {
                                slot.GetComponent <Slot>().displayIndex = -1;
                            }
                            else
                            {
                                if (armatureEditor.isUGUI)
                                {
                                    MaskableGraphic[] renders = slot.GetComponentsInChildren <MaskableGraphic>(true);
                                    for (int p = 0; p < renders.Length; ++p)
                                    {
                                        if (renders[p].name == slotData.attachment)
                                        {
                                            slot.GetComponent <Slot>().displayIndex = p;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    Renderer[] renders = slot.GetComponentsInChildren <Renderer>(true);
                                    for (int p = 0; p < renders.Length; ++p)
                                    {
                                        if (renders[p].name == slotData.attachment)
                                        {
                                            slot.GetComponent <Slot>().displayIndex = p;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (armatureEditor.displayType == Bone2DSetupEditor.DisplayType.SpriteRender ||
                    armatureEditor.displayType == Bone2DSetupEditor.DisplayType.UGUIImage)
                {
                    if (metaDatas.Count > 0)
                    {
                        foreach (Texture k in metaDatas.Keys)
                        {
                            string          textureAtlasPath = AssetDatabase.GetAssetPath(k);
                            TextureImporter textureImporter  = AssetImporter.GetAtPath(textureAtlasPath) as TextureImporter;
                            textureImporter.maxTextureSize      = 2048;
                            textureImporter.spritesheet         = metaDatas[k].ToArray();
                            textureImporter.textureType         = TextureImporterType.Sprite;
                            textureImporter.spriteImportMode    = SpriteImportMode.Multiple;
                            textureImporter.spritePixelsPerUnit = 100;
                            AssetDatabase.ImportAsset(textureAtlasPath, ImportAssetOptions.ForceUpdate);
                            Object[] savedSprites = AssetDatabase.LoadAllAssetsAtPath(textureAtlasPath);
                            foreach (Object obj in savedSprites)
                            {
                                Sprite objSprite = obj as Sprite;
                                if (objSprite)
                                {
                                    len = sprites.Count;
                                    for (int i = 0; i < len; ++i)
                                    {
                                        if (sprites[i].name.Equals(objSprite.name))
                                        {
                                            sprites[i].sprite = objSprite;
                                        }
                                    }
                                    len = images.Count;
                                    for (int i = 0; i < len; ++i)
                                    {
                                        if (images[i].name.Equals(objSprite.name))
                                        {
                                            images[i].sprite = objSprite;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #10
0
        //init
        public void InitShow()
        {
            DragonBoneShowArmature.AddBones(this);
            DragonBoneShowArmature.AddSlot(this);
            DragonBoneShowArmature.ShowBones(this);
            DragonBoneShowArmature.ShowSlots(this);
            DragonBoneShowArmature.ShowSkins(this);
            DragonBoneShowArmature.SetIKs(this);
            DragonBoneAnimFile.CreateAnimFile(this);

            Armature armature = _armature.GetComponent <Armature>();

            m_prefabs.Add(_armature.gameObject);
            armature.textureFrames = m_TextureFrames;

            //update slot display
            for (int s = 0; s < m_slots.Count; ++s)
            {
                m_slots[s].displayIndex = m_slots[s].displayIndex;
            }

            if (armature.isUGUI)
            {
                UnityEngine.UI.MaskableGraphic[] renders = _armature.GetComponentsInChildren <UnityEngine.UI.MaskableGraphic>(true);
                armature.uiAttachments = renders;
                armature.attachments   = new Renderer[0];
            }
            else
            {
                Renderer[] renders = _armature.GetComponentsInChildren <Renderer>(true);
                armature.attachments   = renders;
                armature.uiAttachments = new UnityEngine.UI.MaskableGraphic[0];
            }
            armature.slots        = m_slots.ToArray();
            armature.bones        = m_bones.ToArray();
            armature.zSpace       = zoffset;
            armature.sonArmatures = m_sonArmature.ToArray();
            if (armatureAnimList.ContainsKey(armature.name))
            {
                armature.anims = armatureAnimList[armature.name].ToArray();
            }
            armature.ResetSlotZOrder();

            string path = AssetDatabase.GetAssetPath(animTextAsset);

            path = path.Substring(0, path.LastIndexOf('/')) + "/" + _armature.name;


            //create pose data
            PoseData poseData = AssetDatabase.LoadAssetAtPath <PoseData>(path + "_Pose.asset");

            if (poseData == null)
            {
                poseData = ScriptableObject.CreateInstance <PoseData>();
                AssetDatabase.CreateAsset(poseData, path + "_Pose.asset");
            }
            poseData.slotDatas = new PoseData.SlotData[armature.slots.Length];
            for (int i = 0; i < armature.slots.Length; ++i)
            {
                poseData.slotDatas[i]              = new PoseData.SlotData();
                poseData.slotDatas[i].color        = armature.slots[i].color;
                poseData.slotDatas[i].displayIndex = armature.slots[i].displayIndex;
                poseData.slotDatas[i].zorder       = armature.slots[i].z;
                armature.slots[i].SendMessage("UpdateSlotByInheritSlot", SendMessageOptions.DontRequireReceiver);
            }
            poseData.boneDatas = new PoseData.TransformData[armature.bones.Length];
            for (int i = 0; i < armature.bones.Length; ++i)
            {
                poseData.boneDatas[i]          = new PoseData.TransformData();
                poseData.boneDatas[i].x        = armature.bones[i].localPosition.x;
                poseData.boneDatas[i].y        = armature.bones[i].localPosition.y;
                poseData.boneDatas[i].sx       = armature.bones[i].localScale.x;
                poseData.boneDatas[i].sy       = armature.bones[i].localScale.y;
                poseData.boneDatas[i].rotation = armature.bones[i].localEulerAngles.z;
            }
            if (isUGUI)
            {
                poseData.displayDatas = new PoseData.DisplayData[armature.uiAttachments.Length];
                for (int i = 0; i < armature.uiAttachments.Length; ++i)
                {
                    poseData.displayDatas[i] = new PoseData.DisplayData();
                    UnityEngine.UI.MaskableGraphic render = armature.uiAttachments[i];

                    UIFrame sf = render.GetComponent <UIFrame>();
                    if (sf)
                    {
                        poseData.displayDatas[i].type  = PoseData.AttachmentType.IMG;
                        poseData.displayDatas[i].color = sf.color;
                    }
                    else
                    {
                        UIMesh sm = render.GetComponent <UIMesh>();
                        if (sm)
                        {
                            poseData.displayDatas[i].type   = PoseData.AttachmentType.MESH;
                            poseData.displayDatas[i].color  = sm.color;
                            poseData.displayDatas[i].vertex = (Vector3[])sm.vertices.Clone();
                            if (sm.weights == null || sm.weights.Length == 0)
                            {
                                for (int k = 0; k < poseData.displayDatas[i].vertex.Length; ++k)
                                {
                                    poseData.displayDatas[i].vertex[k] /= 100f;
                                }
                            }
                        }
                        else
                        {
                            UnityEngine.UI.Image sr = render.GetComponent <UnityEngine.UI.Image>();
                            if (sr)
                            {
                                poseData.displayDatas[i].type  = PoseData.AttachmentType.IMG;
                                poseData.displayDatas[i].color = sr.color;
                            }
                            else
                            {
                                poseData.displayDatas[i].type = PoseData.AttachmentType.BOX;
                            }
                        }
                    }
                    poseData.displayDatas[i].transform          = new PoseData.TransformData();
                    poseData.displayDatas[i].transform.x        = render.transform.localPosition.x;
                    poseData.displayDatas[i].transform.y        = render.transform.localPosition.y;
                    poseData.displayDatas[i].transform.sx       = render.transform.localScale.x;
                    poseData.displayDatas[i].transform.sy       = render.transform.localScale.y;
                    poseData.displayDatas[i].transform.rotation = render.transform.localEulerAngles.z;
                    render.transform.localScale *= unit;
                }
            }
            else
            {
                poseData.displayDatas = new PoseData.DisplayData[armature.attachments.Length];
                for (int i = 0; i < armature.attachments.Length; ++i)
                {
                    poseData.displayDatas[i] = new PoseData.DisplayData();
                    Renderer render = armature.attachments[i];

                    SpriteFrame sf = render.GetComponent <SpriteFrame>();
                    if (sf)
                    {
                        poseData.displayDatas[i].type  = PoseData.AttachmentType.IMG;
                        poseData.displayDatas[i].color = sf.color;
                    }
                    else
                    {
                        SpriteMesh sm = render.GetComponent <SpriteMesh>();
                        if (sm)
                        {
                            poseData.displayDatas[i].type   = PoseData.AttachmentType.MESH;
                            poseData.displayDatas[i].color  = sm.color;
                            poseData.displayDatas[i].vertex = (Vector3[])sm.vertices.Clone();
                        }
                        else
                        {
                            SpriteRenderer sr = render.GetComponent <SpriteRenderer>();
                            if (sr)
                            {
                                poseData.displayDatas[i].type  = PoseData.AttachmentType.IMG;
                                poseData.displayDatas[i].color = sr.color;
                            }
                            else
                            {
                                poseData.displayDatas[i].type = PoseData.AttachmentType.BOX;
                            }
                        }
                    }
                    poseData.displayDatas[i].transform          = new PoseData.TransformData();
                    poseData.displayDatas[i].transform.x        = render.transform.localPosition.x;
                    poseData.displayDatas[i].transform.y        = render.transform.localPosition.y;
                    poseData.displayDatas[i].transform.sx       = render.transform.localScale.x;
                    poseData.displayDatas[i].transform.sy       = render.transform.localScale.y;
                    poseData.displayDatas[i].transform.rotation = render.transform.localEulerAngles.z;
                }
            }
            armature.poseData = poseData;
            AssetDatabase.Refresh();
            EditorUtility.SetDirty(poseData);
            AssetDatabase.SaveAssets();
            if (armature.textureFrames && armature.textureFrames.materials != null && armature.textureFrames.materials.Length > 0)
            {
                armature.preMultiplyAlpha = armature.textureFrames.materials[0].GetFloat("_BlendSrc") == (int)UnityEngine.Rendering.BlendMode.One;
            }
            else
            {
                armature.preMultiplyAlpha = true;
            }

            if (armature.isUGUI)
            {
                GameObject canvas = GameObject.Find("/Canvas");
                if (canvas)
                {
                    _armature.SetParent(canvas.transform);
                    _armature.localScale    = Vector3.one / unit;
                    _armature.localPosition = Vector3.zero;
                }
            }

            if (genPrefab)
            {
                string     prefabPath = path + ".prefab";
                GameObject prefab     = AssetDatabase.LoadAssetAtPath <GameObject>(prefabPath);
                if (!prefab)
                {
                    PrefabUtility.CreatePrefab(prefabPath, _armature.gameObject, ReplacePrefabOptions.ConnectToPrefab);
                }
                else
                {
                    PrefabUtility.ReplacePrefab(_armature.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
                }
            }
        }
        static void HierarchyIconsOnGUI(int instanceId, Rect selectionRect)
        {
            Rect       rect       = new Rect(selectionRect.x - 25f, selectionRect.y, 16f, 16f);
            GameObject armatureGo = (GameObject)EditorUtility.InstanceIDToObject(instanceId);

            if (armatureGo && armatureGo.GetComponent <Armature>() != null && textureArmature)
            {
                rect.x = selectionRect.x + selectionRect.width - 16f;
                GUI.Label(rect, textureArmature);
            }


            GameObject go = Selection.activeGameObject;

            if (go)
            {
                _armature = go.GetComponentInParent <Armature>();
                if (_armature == null)
                {
                    return;
                }
            }
            else if (_armature == null)
            {
                return;
            }
            if (_armature.bones != null && textureBone != null)
            {
                foreach (Transform bone in _armature.bones)
                {
                    if (bone && EditorUtility.InstanceIDToObject(instanceId) == bone.gameObject)
                    {
                        GUI.Label(rect, textureBone);
                        break;
                    }
                }
            }
            if (_armature.slots != null && textureSlot != null)
            {
                foreach (Slot slot in _armature.slots)
                {
                    GameObject slotGo = (GameObject)EditorUtility.InstanceIDToObject(instanceId);
                    if (slot && slot.GetComponent <Armature>() == null)
                    {
                        if (slotGo == slot.gameObject)
                        {
                            GUI.Label(rect, textureSlot);
                            break;
                        }
                        else if (slot.inheritSlot && slot.inheritSlot.gameObject == slotGo)
                        {
                            rect.x += 10f;
                            rect.y += 2f;
                            GUI.Label(rect, textureSlot);
                            break;
                        }
                    }
                }
            }
            if (_armature.uiAttachments != null && textureImg && textureMesh)
            {
                foreach (UnityEngine.UI.MaskableGraphic g in _armature.uiAttachments)
                {
                    if (g && EditorUtility.InstanceIDToObject(instanceId) == g.gameObject)
                    {
                        rect.x += 8f;
                        if (g.transform.childCount > 0)
                        {
                            rect.x -= 8;
                        }
                        if (g.GetComponent <UIFrame>() || g.GetComponent <UnityEngine.UI.Image>())
                        {
                            GUI.Label(rect, textureImg);
                        }
                        else
                        {
                            GUI.Label(rect, textureMesh);
                        }
                        break;
                    }
                }
            }

            if (_armature.attachments != null && textureImg && textureMesh)
            {
                foreach (Renderer r in _armature.attachments)
                {
                    if (r && EditorUtility.InstanceIDToObject(instanceId) == r.gameObject)
                    {
                        rect.x += 8f;
                        if (r.transform.childCount > 0)
                        {
                            rect.x -= 8;
                        }
                        if (r.GetComponent <SpriteFrame>() || r.GetComponent <SpriteRenderer>())
                        {
                            GUI.Label(rect, textureImg);
                        }
                        else
                        {
                            GUI.Label(rect, textureMesh);
                        }
                        break;
                    }
                }
            }
        }
        static void UpdateSprites()
        {
            Armature armature         = (Selection.activeObject as GameObject).GetComponent <Armature>();
            bool     isSpriteRenderer = false;

            foreach (Renderer render in armature.attachments)
            {
                if (render is SpriteRenderer)
                {
                    isSpriteRenderer = true;
                    break;
                }
            }
            if (isSpriteRenderer)
            {
                Texture  t    = null;
                Object[] objs = null;
                foreach (Renderer render in armature.attachments)
                {
                    if (render is SpriteRenderer)
                    {
                        SpriteRenderer sr = render as SpriteRenderer;
                        if (sr.sprite)
                        {
                            if (t != sr.sharedMaterial.mainTexture || objs == null)
                            {
                                objs = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(sr.sharedMaterial.mainTexture));
                                t    = sr.sharedMaterial.mainTexture;
                            }
                            if (objs != null)
                            {
                                foreach (Object obj in objs)
                                {
                                    if (obj is Sprite && obj.name.Equals(sr.sprite.name))
                                    {
                                        sr.sprite = obj as Sprite;
                                        UnityEditor.EditorUtility.SetDirty(sr);
                                    }
                                }
                            }
                        }
                    }
                }
                UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                return;
            }

            bool isImage = false;

            foreach (UnityEngine.UI.MaskableGraphic render in armature.uiAttachments)
            {
                if (render is UnityEngine.UI.Image)
                {
                    isImage = true;
                    break;
                }
            }
            if (isImage)
            {
                Material m    = null;
                Object[] objs = null;
                foreach (UnityEngine.UI.MaskableGraphic render in armature.uiAttachments)
                {
                    if (render is UnityEngine.UI.Image)
                    {
                        UnityEngine.UI.Image sr = render as UnityEngine.UI.Image;
                        if (sr.sprite)
                        {
                            if (m != sr.material || objs == null)
                            {
                                objs = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(sr.material.mainTexture));
                                m    = sr.material;
                            }
                            if (objs != null)
                            {
                                foreach (Object obj in objs)
                                {
                                    if (obj is Sprite && obj.name.Equals(sr.sprite.name))
                                    {
                                        sr.sprite = obj as Sprite;
                                    }
                                }
                            }
                        }
                    }
                }
                UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                return;
            }
        }
        public override void OnInspectorGUI()
        {
            Armature armature = target as Armature;

            if (armature == null)
            {
                return;
            }

            bool haveGroup = false;

                        #if UNITY_5_6_OR_NEWER
            haveGroup = armature.sortingGroup != null;
                        #endif

            if (!armature.isUGUI && !haveGroup)
            {
                selectedOption = EditorGUILayout.Popup("Sorting Layer", selectedOption, sortingLayerNames);
                if (sortingLayerNames[selectedOption] != armature.sortingLayerName)
                {
                    Undo.RecordObject(armature, "Sorting Layer");
                    armature.sortingLayerName = sortingLayerNames[selectedOption];
                    EditorUtility.SetDirty(armature);
                }

                int newSortingLayerOrder = EditorGUILayout.IntField("Order in Layer", armature.sortingOrder);
                if (newSortingLayerOrder != armature.sortingOrder)
                {
                    Undo.RecordObject(armature, "Edit Sorting Order");
                    armature.sortingOrder = newSortingLayerOrder;
                    EditorUtility.SetDirty(armature);
                }

                if (GUILayout.Button("Update All Sorting", GUILayout.Height(20)))
                {
                    armature.sortingLayerName = armature.sortingLayerName;
                    armature.sortingOrder     = armature.sortingOrder;
                    EditorUtility.SetDirty(armature);

                    foreach (Renderer render in armature.GetComponentsInChildren <Renderer>(true))
                    {
                        render.sortingLayerName = armature.sortingLayerName;
                        render.sortingOrder     = armature.sortingOrder;
                        EditorUtility.SetDirty(render);

                        SpriteFrame sf = render.GetComponent <SpriteFrame>();
                        if (sf)
                        {
                            sf.sortingLayerName = armature.sortingLayerName;
                            sf.sortingOrder     = armature.sortingOrder;
                            UnityEditor.EditorUtility.SetDirty(sf);
                        }
                        else
                        {
                            SpriteMesh sm = render.GetComponent <SpriteMesh>();
                            if (sm)
                            {
                                sm.sortingLayerName = armature.sortingLayerName;
                                sm.sortingOrder     = armature.sortingOrder;
                                UnityEditor.EditorUtility.SetDirty(sm);
                            }
                        }
                    }

                    foreach (Armature sonArmature in armature.GetComponentsInChildren <Armature>(true))
                    {
                        sonArmature.sortingLayerName = sonArmature.sortingLayerName;
                        sonArmature.sortingOrder     = sonArmature.sortingOrder;
                        EditorUtility.SetDirty(sonArmature);
                    }

                    if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                    {
                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                    }
                }
                EditorGUILayout.Space();
            }

            serializedObject.Update();
            EditorGUILayout.PropertyField(serializedObject.FindProperty("color"), true);
            if (!Application.isPlaying)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("m_FlipX"), true);
                EditorGUILayout.PropertyField(serializedObject.FindProperty("m_FlipY"), true);
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("m_PreMultiplyAlpha"), true);

            if (!Application.isPlaying)
            {
                if (!armature.isUGUI)
                {
                                        #if UNITY_5_6_OR_NEWER
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("sortType"), true);
                                        #else
                    armature.sortType = Armature.SortType.ZSpace;
                                        #endif
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("zSpace"), true);

                    if (sortType != armature.sortType)
                    {
                        sortType = armature.sortType;
                        armature.sortingOrder = armature.sortingOrder;
                    }
                }
            }
            if (armature.anims != null && armature.anims.Length > 0)
            {
                int temp = (int)armature.animIndex;
                System.Collections.Generic.List <string> animsList = new System.Collections.Generic.List <string>(armature.anims);
                animsList.Insert(0, "<None>");
                armature.animIndex = EditorGUILayout.Popup("Current Animation", temp + 1, animsList.ToArray()) - 1;
                if (armature.animIndex != temp && !Application.isPlaying)
                {
                    UnityEditor.EditorUtility.SetDirty(armature);
                    if (!Application.isPlaying && !string.IsNullOrEmpty(armature.gameObject.scene.name))
                    {
                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                    }
                }
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("anims"), true);
            if (armature.skins != null && armature.skins.Length > 1)
            {
                int temp = armature.skinIndex;
                armature.skinIndex = EditorGUILayout.Popup("Skins", armature.skinIndex, armature.skins);
                if (temp != armature.skinIndex && !Application.isPlaying)
                {
                    UnityEditor.EditorUtility.SetDirty(armature);
                    if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                    {
                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                    }
                }
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("slots"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("bones"), true);
            if (armature.isUGUI)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("uiAttachments"), true);
            }
            else
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("attachments"), true);
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("sonArmatures"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("parentArmature"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("textureFrames"), true);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("poseData"), true);
            serializedObject.ApplyModifiedProperties();

            if (!Application.isPlaying && armature.flipX != flipX)
            {
                armature.flipX = armature.flipX;
                flipX          = armature.flipX;
                if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }
            if (!Application.isPlaying && armature.flipY != flipY)
            {
                armature.flipY = armature.flipY;
                flipY          = armature.flipY;
                if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }
            if (!Application.isPlaying && armature.zSpace != zspace)
            {
                zspace = armature.zSpace;
                armature.ResetSlotZOrder();
                if (!string.IsNullOrEmpty(armature.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }

            if (armature.preMultiplyAlpha != preMultiplyAlpha)
            {
                preMultiplyAlpha          = armature.preMultiplyAlpha;
                armature.preMultiplyAlpha = preMultiplyAlpha;
                if (!Application.isPlaying && !string.IsNullOrEmpty(armature.gameObject.scene.name))
                {
                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene());
                }
            }
            if (!Application.isPlaying && !armature.isUGUI && armature.parentArmature == null)
            {
                ArmatureMesh am = armature.gameObject.GetComponent <ArmatureMesh>();
                if (!am)
                {
                    if (GUILayout.Button("Add Armature Mesh", GUILayout.Height(20)))
                    {
                        am = armature.gameObject.AddComponent <ArmatureMesh>();
                    }
                }
            }
            GUILayout.Space(5);
            if (GUILayout.Button("Set To Pose", GUILayout.Height(20)))
            {
                armature.SetToPose();
            }
        }