Пример #1
0
        /// <summary>
        /// Changes the user interface mesh.
        /// </summary>
        /// <param name="um">Um.</param>
        /// <param name="texture">Texture.</param>
        /// <param name="mat">Mat.</param>
        public void ChangeUIMesh(UIMesh um, Texture texture, Material mat = null)
        {
            if (!um || !texture)
            {
                return;
            }

            TextureFrame frame = new TextureFrame();

            frame.uiMaterial = mat == null ? um.material : mat;
            frame.texture    = texture;
            frame.isRotated  = false;

            frame.rect.x             = 0;
            frame.rect.y             = 0;
            frame.rect.width         = texture.width;
            frame.rect.height        = texture.height;
            frame.frameSize          = new Rect(0, 0, texture.width, texture.height);
            frame.atlasTextureSize.x = texture.width;
            frame.atlasTextureSize.y = texture.height;
            if (!um.isCreatedMesh)
            {
                um.CreateMesh();
            }
            um.frame = frame;
            if (mat)
            {
                um.material = mat;
            }
            //change texture
            um.texture = texture;
        }
Пример #2
0
        /// <summary>
        /// Changes the user interface frame.
        /// </summary>
        /// <param name="uf">Uf.</param>
        /// <param name="texture">Texture.</param>
        /// <param name="mat">Mat.</param>
        public void ChangeUIFrame(UIFrame uf, Texture texture, Material mat = null)
        {
            if (!uf || !texture)
            {
                return;
            }

            //new textureframe
            TextureFrame frame = new TextureFrame();

            frame.atlasTextureSize = new Vector2(texture.width, texture.height);
            frame.uiMaterial       = mat == null ? uf.material : mat;
            frame.rect.x           = 0f;
            frame.rect.y           = 0f;
            frame.rect.width       = texture.width;
            frame.rect.height      = texture.height;
            frame.frameSize        = new Rect(0, 0, texture.width, texture.height);
            frame.isRotated        = false;
            frame.rect             = uf.frame.frameSize;
            if (!uf.isCreatedMesh)
            {
                uf.CreateQuad();
            }
            uf.frame = frame;
            if (mat)
            {
                uf.material = mat;
            }
            //change texture
            uf.texture = texture;
        }
Пример #3
0
 public void UpdateFrames()
 {
     if (frames != null)
     {
         int  len  = frames.Length;
         bool temp = false;
         for (int i = 0; i < len; ++i)
         {
             TextureFrame frame = frames[i];
             if (frame.texture == null)
             {
                 if (frame.material && frame.material.mainTexture)
                 {
                     frame.texture = frame.material.mainTexture;
                     temp          = true;
                 }
                 if (frame.uiMaterial && frame.uiMaterial.mainTexture)
                 {
                     frame.texture = frame.uiMaterial.mainTexture;
                     temp          = true;
                 }
             }
         }
         if (temp)
         {
             AssetDatabase.Refresh();
             EditorUtility.SetDirty(this);
             AssetDatabase.SaveAssets();
         }
     }
 }
Пример #4
0
 public static List <TextureFrame> ParseDragonBoneAtlasText(string atlasText, Material material)
 {
     if (atlasText != null && material != null && material.mainTexture != null)
     {
         Bones2D.JSONClass   obj         = Bones2D.JSON.Parse(atlasText).AsObject;
         Bones2D.JSONArray   arr         = obj["SubTexture"].AsArray;
         List <TextureFrame> framesList  = new List <TextureFrame>();
         Vector2             textureSize = GetTextureSize(material.mainTexture);
         for (int i = 0; i < arr.Count; ++i)
         {
             Bones2D.JSONClass frameObj = arr[i].AsObject;
             TextureFrame      frame    = new TextureFrame();
             frame.atlasTextureSize = textureSize;
             frame.name             = frameObj["name"];
             frame.name             = frame.name.Replace('/', '_');
             frame.texture          = material.mainTexture;
             if (material.name.LastIndexOf("_UI_Mat") > -1)
             {
                 frame.uiMaterial = material;
             }
             else
             {
                 frame.material = material;
             }
             Rect rect = new Rect();
             rect.x      = frameObj["x"].AsFloat;
             rect.y      = frameObj["y"].AsFloat;
             rect.width  = frameObj["width"].AsFloat;
             rect.height = frameObj["height"].AsFloat;
             Rect frameSize = new Rect(0, 0, rect.width, rect.height);
             if (frameObj.ContainKey("frameX"))
             {
                 frameSize.x = frameObj["frameX"].AsFloat;
             }
             if (frameObj.ContainKey("frameY"))
             {
                 frameSize.y = frameObj["frameY"].AsFloat;
             }
             if (frameObj.ContainKey("frameWidth"))
             {
                 frameSize.width = frameObj["frameWidth"].AsFloat;
             }
             if (frameObj.ContainKey("frameHeight"))
             {
                 frameSize.height = frameObj["frameHeight"].AsFloat;
             }
             frame.rect      = rect;
             frame.frameSize = frameSize;
             framesList.Add(frame);
         }
         return(framesList);
     }
     return(null);
 }
Пример #5
0
 /// <summary>
 /// Get Frame by Name
 /// </summary>
 /// <returns>The texture frame.</returns>
 /// <param name="name">Name.</param>
 public TextureFrame GetTextureFrame(string name)
 {
     if (frames != null)
     {
         int len = frames.Length;
         for (int i = 0; i < len; ++i)
         {
             TextureFrame frame = frames[i];
             if (frame.name.Equals(name))
             {
                 return(frame);
             }
         }
     }
     return(null);
 }
Пример #6
0
        /// <summary>
        /// Changes the user interface mesh.
        /// </summary>
        /// <param name="uiMeshName">User interface mesh name.</param>
        /// <param name="newTextureFrameName">New texture frame name.</param>
        public void ChangeUIMesh(string uiMeshName, string newTextureFrameName)
        {
            MaskableGraphic attachment = GetUIAttachmentByName(uiMeshName);

            if (!attachment)
            {
                return;
            }

            UIMesh       um    = attachment.GetComponent <UIMesh>();
            TextureFrame frame = textureFrames.GetTextureFrame(newTextureFrameName);

            if (um != null && frame != null)
            {
                um.frame = frame;
            }
        }
Пример #7
0
        /// <summary>
        /// Changes the user interface frame.
        /// </summary>
        /// <param name="uiFrameName">User interface frame name.</param>
        /// <param name="newFrameName">New frame name.</param>
        public void ChangeUIFrame(string uiFrameName, string newFrameName)
        {
            MaskableGraphic attachment = GetUIAttachmentByName(uiFrameName);

            if (!attachment)
            {
                return;
            }

            UIFrame      uf    = attachment.GetComponent <UIFrame>();
            TextureFrame frame = textureFrames.GetTextureFrame(newFrameName);

            if (uf != null && frame != null)
            {
                uf.frame = frame;
            }
        }
Пример #8
0
        /// <summary>
        /// Changes the sprite mesh.
        /// </summary>
        /// <param name="spriteMeshName">Sprite mesh name.</param>
        /// <param name="newTextureFrameName">New texture frame name.</param>
        public void ChangeSpriteMesh(string spriteMeshName, string newTextureFrameName)
        {
            Renderer attachment = GetAttachmentByName(spriteMeshName);

            if (!attachment)
            {
                return;
            }

            SpriteMesh   sm    = attachment.GetComponent <SpriteMesh>();
            TextureFrame frame = textureFrames.GetTextureFrame(newTextureFrameName);

            if (sm != null && frame != null)
            {
                sm.frame = frame;
            }
        }
Пример #9
0
        /// <summary>
        /// Changes the sprite mesh.
        /// </summary>
        /// <param name="sm">Will replace SpriteMesh</param>
        /// <param name="texture">new Texture.</param>
        /// <param name="mat">Mat.</param>
        public void ChangeSpriteMesh(SpriteMesh sm, Texture texture, Material mat = null, bool useMaterialBlock = true)
        {
            if (!sm || !texture)
            {
                return;
            }

            TextureFrame frame = new TextureFrame();

            frame.material  = mat == null ? sm.material : mat;
            frame.texture   = texture;
            frame.isRotated = false;

            frame.rect.x             = 0;
            frame.rect.y             = 0;
            frame.rect.width         = texture.width;
            frame.rect.height        = texture.height;
            frame.frameSize          = new Rect(0, 0, texture.width, texture.height);
            frame.atlasTextureSize.x = texture.width;
            frame.atlasTextureSize.y = texture.height;
            if (!sm.isCreatedMesh)
            {
                sm.CreateMesh();
            }
            sm.frame = frame;
            if (mat)
            {
                sm.material = mat;
            }

            if (useMaterialBlock)
            {
                sm.render.GetPropertyBlock(materialPropertyBlock);
                m_MatBlock.SetTexture("_MainTex", texture);
                sm.render.SetPropertyBlock(materialPropertyBlock);
            }
            else
            {
                sm.material.mainTexture = texture;
            }
        }
Пример #10
0
        /// <summary>
        /// Changes the sprite frame.
        /// </summary>
        /// <param name="sf">Will replace SpriteFrame</param>
        /// <param name="texture">new Texture.</param>
        /// <param name="mat">Mat.</param>
        public void ChangeSpriteFrame(SpriteFrame sf, Texture texture, Material mat = null, bool useMaterialBlock = true)
        {
            if (!sf || !texture)
            {
                return;
            }

            //new textureframe
            TextureFrame frame = new TextureFrame();

            frame.atlasTextureSize = new Vector2(texture.width, texture.height);
            frame.material         = mat == null ? sf.material : mat;
            frame.rect.x           = 0f;
            frame.rect.y           = 0f;
            frame.rect.width       = texture.width;
            frame.rect.height      = texture.height;
            frame.frameSize        = new Rect(0, 0, texture.width, texture.height);
            frame.isRotated        = false;
            if (!sf.isCreatedMesh)
            {
                sf.CreateQuad();
            }
            sf.frame = frame;
            if (mat)
            {
                sf.material = mat;
            }

            //change texture
            if (useMaterialBlock)
            {
                sf.meshRenderer.GetPropertyBlock(materialPropertyBlock);
                m_MatBlock.SetTexture("_MainTex", texture);
                sf.meshRenderer.SetPropertyBlock(materialPropertyBlock);
            }
            else
            {
                sf.material.mainTexture = texture;
            }
        }
Пример #11
0
        static void ShowSpriteMesh(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor)
        {
            GameObject go = new GameObject(attachmentData.name);
            SpriteMesh sm = go.AddComponent <SpriteMesh>();

            sm.transform.parent = skinParent;

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;
            go.transform.localScale = localSc;

            go.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation);

            Transform[] bones = SetMeshVertex <SpriteMesh>(sm, attachmentData, armatureEditor);
            sm.uvs       = attachmentData.uvs;
            sm.triangles = attachmentData.triangles;
            sm.colors    = new Color[sm.vertices.Length];
            for (int i = 0; i < sm.colors.Length; ++i)
            {
                sm.colors[i] = Color.white;
            }
            if (armatureEditor.genMeshCollider && attachmentData.edges != null)
            {
                sm.edges = attachmentData.edges;
            }
            if (attachmentData.weights != null && attachmentData.weights.Count > 0)
            {
                sm.CreateMesh();
                if (armatureEditor.ffdKV.ContainsKey(attachmentData.textureName))
                {
                    //Vertex controller
                    sm.vertControlTrans = new Transform[sm.vertices.Length];
                    for (int i = 0; i < sm.vertices.Length; ++i)
                    {
                        GameObject gov = new GameObject(go.name + "_v" + i);
                        gov.transform.parent        = go.transform;
                        gov.transform.localPosition = sm.vertices[i];
                        gov.transform.localScale    = Vector3.zero;
                        sm.vertControlTrans[i]      = gov.transform;
                        gov.SetActive(false);
                    }
                }
            }
            else
            {
                sm.CreateMesh();
                //Vertex controller
                sm.vertControlTrans = new Transform[sm.vertices.Length];
                for (int i = 0; i < sm.vertices.Length; ++i)
                {
                    GameObject gov = new GameObject(go.name + "_v" + i);
                    gov.transform.parent        = go.transform;
                    gov.transform.localPosition = sm.vertices[i];
                    gov.transform.localScale    = Vector3.zero;
                    sm.vertControlTrans[i]      = gov.transform;
                    gov.SetActive(false);
                }
            }

            if (attachmentData.weights != null && attachmentData.weights.Count > 0)
            {
                List <Armature.BoneWeightClass> boneWeights = new List <Armature.BoneWeightClass>();
                for (int i = 0; i < attachmentData.weights.Count; ++i)
                {
                    int boneCount = (int)attachmentData.weights[i];                    //骨骼数量
                    List <KeyValuePair <int, float> > boneWeightList = new List <KeyValuePair <int, float> >();
                    for (int j = 0; j < boneCount * 2; j += 2)
                    {
                        int   boneIdx = (int)attachmentData.weights[i + j + 1];
                        float weight  = attachmentData.weights[i + j + 2];
                        boneWeightList.Add(new KeyValuePair <int, float>(boneIdx, weight));
                    }
                    //sort boneWeightList,desc
                    boneWeightList.Sort(delegate(KeyValuePair <int, float> x, KeyValuePair <int, float> y) {
                        if (x.Value == y.Value)
                        {
                            return(0);
                        }
                        return(x.Value < y.Value? 1: -1);
                    });
                    Armature.BoneWeightClass bw = new Armature.BoneWeightClass();
                    for (int j = 0; j < boneWeightList.Count; ++j)
                    {
                        if (j == 0)
                        {
                            bw.boneIndex0 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight0    = boneWeightList[j].Value;
                        }
                        else if (j == 1)
                        {
                            bw.boneIndex1 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight1    = boneWeightList[j].Value;
                        }
                        else if (j == 2)
                        {
                            bw.boneIndex2 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight2    = boneWeightList[j].Value;
                        }
                        else if (j == 3)
                        {
                            bw.boneIndex3 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight3    = boneWeightList[j].Value;
                        }
                        else if (j == 4)
                        {
                            bw.boneIndex4 = GlobalBoneIndexToLocalBoneIndex(armatureEditor, boneWeightList[j].Key, bones);
                            bw.weight4    = boneWeightList[j].Value;
                            break;
                        }
                    }
                    boneWeights.Add(bw);
                    i += boneCount * 2;
                }
                Matrix4x4[] matrixArray = new Matrix4x4[bones.Length];
                for (int i = 0; i < matrixArray.Length; ++i)
                {
                    Transform bone = bones[i];
                    matrixArray[i]  = bone.worldToLocalMatrix * armatureEditor.armature.localToWorldMatrix;
                    matrixArray[i] *= Matrix4x4.TRS(slot.localPosition, slot.localRotation, slot.localScale);
                }
                sm.bones     = bones;
                sm.bindposes = matrixArray;
                sm.weights   = boneWeights.ToArray();
            }
            sm.color = attachmentData.color;
            sm.UpdateMesh();
            sm.UpdateVertexColor();
            sm.frame = frame;
        }
Пример #12
0
        private static SpriteRenderer ShowUnitySprite(SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpriteMetaData metaData, TextureFrame frame)
        {
            Sprite sprite = Sprite.Create((Texture2D)frame.texture, metaData.rect, metaData.pivot, 100f, 0, SpriteMeshType.Tight);

            return(ShowUnitySpriteSingle(sprite, attachmentData, slot, skinParent, frame));
        }
Пример #13
0
        private static SpriteRenderer ShowUnitySpriteSingle(Sprite sprite, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, TextureFrame frame)
        {
            GameObject     go       = new GameObject(attachmentData.name);
            SpriteRenderer renderer = go.AddComponent <SpriteRenderer>();

            renderer.material   = frame.material;
            renderer.sprite     = sprite;
            go.transform.parent = skinParent;

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;

            if (frame.isRotated)
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.height;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.width;
                }
            }
            else
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.width;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.height;
                }
            }
            go.transform.localScale = localSc;

            renderer.color = attachmentData.color;

            go.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation + (frame.isRotated?-90f:0f));

            return(renderer);
        }
Пример #14
0
        private static Image ShowUIImageSingle(Sprite sprite, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, TextureFrame frame)
        {
            GameObject go       = new GameObject(attachmentData.name);
            Image      renderer = go.AddComponent <Image>();

            renderer.sprite        = sprite;
            renderer.raycastTarget = false;
            renderer.SetNativeSize();
            renderer.material = frame.uiMaterial;
            go.transform.SetParent(skinParent);
            BoxCollider2D col = go.GetComponent <BoxCollider2D>();

            if (col)
            {
                col.size = renderer.rectTransform.sizeDelta;
            }

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;

            if (frame.isRotated)
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.height;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.width;
                }
            }
            else
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.width;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.height;
                }
            }
            go.transform.localScale    = localSc;
            renderer.color             = attachmentData.color;
            go.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation + (frame.isRotated?-90f:0f));

            return(renderer);
        }
Пример #15
0
        static void ShowUIFrame(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor, SpineData.SlotData slotData)
        {
            GameObject go       = new GameObject();
            UIFrame    newFrame = go.AddComponent <UIFrame>();

            newFrame.raycastTarget = false;
            newFrame.GetComponent <Graphic>().raycastTarget = false;
            newFrame.CreateQuad();
            newFrame.frame = frame;
            newFrame.name  = attachmentData.textureName;
            newFrame.transform.SetParent(skinParent);

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            go.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;

            if (newFrame.frame.isRotated)
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.height;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.width;
                }
            }
            else
            {
                if (attachmentData.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.width;
                }
                if (attachmentData.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.height;
                }
            }
            newFrame.transform.localScale = localSc;
            newFrame.color = attachmentData.color;
            newFrame.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation);

            if (armatureEditor.genImgCollider)
            {
                BoxCollider2D collider = newFrame.gameObject.AddComponent <BoxCollider2D>();
                if (newFrame.frame.isRotated)
                {
                    collider.size = new Vector2(newFrame.frame.rect.size.y, newFrame.frame.rect.size.x) * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
                else
                {
                    collider.size = newFrame.frame.rect.size * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
            }
            newFrame.UpdateAll();
        }
Пример #16
0
        private static void ShowSpriteFrame(TextureFrame frame, SpineData.SkinAttachment attachmentData, Transform slot, Transform skinParent, SpineArmatureEditor armatureEditor)
        {
            GameObject  go       = new GameObject();
            SpriteFrame newFrame = go.AddComponent <SpriteFrame>();

            newFrame.CreateQuad();
            newFrame.textureFrames    = armatureEditor.m_TextureFrames;
            newFrame.frame            = frame;
            newFrame.name             = attachmentData.name;
            newFrame.pivot            = Vector2.one * 0.5f;
            newFrame.transform.parent = skinParent;

            Vector3 localPos = Vector3.zero;

            localPos.x = attachmentData.x;
            localPos.y = attachmentData.y;
            newFrame.transform.localPosition = localPos;

            Vector3 localSc = Vector3.one;

            localSc.x = attachmentData.scaleX;
            localSc.y = attachmentData.scaleY;

            if (newFrame.frame.isRotated)
            {
                if (attachmentData.width > 0 && frame.rect.height > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.height;
                }
                if (attachmentData.height > 0 && frame.rect.width > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.width;
                }
            }
            else
            {
                if (attachmentData.width > 0 && frame.rect.width > 0)
                {
                    localSc.x *= attachmentData.width / frame.rect.width;
                }
                if (attachmentData.height > 0 && frame.rect.height > 0)
                {
                    localSc.y *= attachmentData.height / frame.rect.height;
                }
            }

            newFrame.transform.localScale = localSc;
            newFrame.color = attachmentData.color;
            newFrame.transform.localRotation = Quaternion.Euler(0, 0, attachmentData.rotation);

            if (armatureEditor.genImgCollider)
            {
                BoxCollider2D collider = newFrame.gameObject.AddComponent <BoxCollider2D>();
                if (newFrame.frame.isRotated)
                {
                    collider.size = new Vector2(newFrame.frame.rect.size.y, newFrame.frame.rect.size.x) * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
                else
                {
                    collider.size = newFrame.frame.rect.size * armatureEditor.unit;

                    Vector2 center = new Vector2(
                        -newFrame.frame.frameSize.width / 2 - newFrame.frame.frameSize.x + newFrame.frame.rect.width / 2,
                        newFrame.frame.frameSize.height / 2 + newFrame.frame.frameSize.y - newFrame.frame.rect.height / 2);
                    collider.offset = center * armatureEditor.unit;
                }
            }
            newFrame.UpdateVertexColor();
        }
Пример #17
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;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #18
0
        public static List <TextureFrame> ParseSpineAtlasText(string atlasText, Material material)
        {
            if (atlasText != null && material != null && material.mainTexture != null)
            {
                Vector2             textureSize = GetTextureSize(material.mainTexture);
                List <TextureFrame> framesList  = new List <TextureFrame>();
                using (TextReader reader = new StringReader(atlasText))
                {
                    string       pngName   = null;
                    string       frameName = "";
                    TextureFrame frame     = null;
                    while (reader.Peek() != -1)
                    {
                        string line = reader.ReadLine().Trim();
                        if (line.Length > 0)
                        {
                            if (line.LastIndexOf(".png") > -1 || line.LastIndexOf(".PNG") > -1)
                            {
                                if (line.Contains(material.mainTexture.name))
                                {
                                    if (pngName != null)
                                    {
                                        break;                                                  //have a png name , over
                                    }
                                    pngName = line;
                                }
                                else
                                {
                                    pngName = null;
                                }
                            }
                            if (pngName == null)
                            {
                                continue;
                            }

                            if (line.IndexOf(":") == -1)
                            {
                                frameName = line;
                            }
                            else
                            {
                                string[] lineArray = line.Split(':');
                                string   key       = lineArray[0].Trim();
                                string   value     = lineArray[1].Trim();
                                if (key.ToLower() == "rotate")
                                {
                                    frame                  = new TextureFrame();
                                    frame.texture          = material.mainTexture;
                                    frame.atlasTextureSize = textureSize;
                                    if (material.name.LastIndexOf("_UI_Mat") > -1)
                                    {
                                        frame.uiMaterial = material;
                                    }
                                    else
                                    {
                                        frame.material = material;
                                    }
                                    framesList.Add(frame);

                                    frame.isRotated = value.ToLower() == "true";
                                    frame.name      = frameName;
                                }
                                else if (key == "xy")
                                {
                                    string[] xy = value.Split(',');
                                    frame.rect.x = float.Parse(xy[0].Trim());
                                    frame.rect.y = float.Parse(xy[1].Trim());
                                }
                                else if (key == "size")
                                {
                                    if (frame != null)
                                    {
                                        string[] size = value.Split(',');
                                        if (frame.isRotated)
                                        {
                                            frame.rect.height = float.Parse(size[0].Trim());
                                            frame.rect.width  = float.Parse(size[1].Trim());
                                        }
                                        else
                                        {
                                            frame.rect.width  = float.Parse(size[0].Trim());
                                            frame.rect.height = float.Parse(size[1].Trim());
                                        }
                                    }
                                }
                                else if (key == "orig")
                                {
                                    string[] orig = value.Split(',');
                                    if (frame.isRotated)
                                    {
                                        frame.frameSize.height = float.Parse(orig[0].Trim());
                                        frame.frameSize.width  = float.Parse(orig[1].Trim());
                                    }
                                    else
                                    {
                                        frame.frameSize.width  = float.Parse(orig[0].Trim());
                                        frame.frameSize.height = float.Parse(orig[1].Trim());
                                    }
                                }
                                else if (key == "offset")
                                {
                                    string[] xy = value.Split(',');
                                    frame.frameSize.x = float.Parse(xy[0].Trim());
                                    frame.frameSize.y = float.Parse(xy[1].Trim());
                                }
                                else if (key == "index")
                                {
                                }
                            }
                        }
                    }
                    reader.Close();
                }
                return(framesList);
            }
            return(null);
        }