Ejemplo n.º 1
0
        public static void ShowSlots(SpineArmatureEditor armatureEditor)
        {
            if (armatureEditor.genericAnim || armatureEditor.isUGUI)
            {
                GameObject rootSlot = new GameObject("slots");
                m_rootSlot = rootSlot.transform;
                m_rootSlot.SetParent(armatureEditor.armature);
                m_rootSlot.localScale    = Vector3.one;
                m_rootSlot.localPosition = Vector3.zero;
            }
            foreach (Transform s in armatureEditor.slotsKV.Values)
            {
                Slot slot = s.GetComponent <Slot>();
                SpineData.SlotData slotData = armatureEditor.slotsDataKV[s.name];
                if (!string.IsNullOrEmpty(slotData.bone))
                {
                    if (armatureEditor.bonesKV.ContainsKey(slotData.bone))
                    {
                        Transform parent = armatureEditor.bonesKV[slotData.bone];

                        if (m_rootSlot)
                        {
                            s.transform.parent = m_rootSlot;

                            GameObject go = new GameObject(s.name);
                            go.transform.parent     = parent.transform;
                            go.transform.localScale = Vector3.one;
                            if (armatureEditor.isUGUI)
                            {
                                go.transform.localPosition = Vector3.zero;
                            }
                            else
                            {
                                go.transform.localPosition = new Vector3(0, 0, -slot.zOrder * armatureEditor.zoffset);
                            }
                            go.transform.localEulerAngles = Vector3.zero;
                            slot.inheritSlot = go.transform;
                        }
                        else
                        {
                            s.transform.parent = parent.transform;
                        }
                    }
                }
                s.transform.localScale = Vector3.one;
                if (armatureEditor.isUGUI)
                {
                    s.localPosition = Vector3.zero;
                }
                else
                {
                    s.localPosition = new Vector3(0, 0, -slot.zOrder * armatureEditor.zoffset);
                }
                s.transform.localEulerAngles = Vector3.zero;
            }
        }
Ejemplo n.º 2
0
 private static void ParseSlots(SpineArmatureEditor armatureEditor, Bones2D.JSONClass armtureObj)
 {
     if (armtureObj.ContainKey("slots"))
     {
         Bones2D.JSONArray    slots     = armtureObj["slots"].AsArray;
         SpineData.SlotData[] slotDatas = new SpineData.SlotData[slots.Count];
         for (int i = 0; i < slots.Count; ++i)
         {
             Bones2D.JSONClass  slotObj  = slots[i].AsObject;
             SpineData.SlotData slotData = new SpineData.SlotData();
             slotData.displayIndex = i;
             if (slotObj.ContainKey("name"))
             {
                 slotData.name = slotObj["name"].ToString();
             }
             if (slotObj.ContainKey("bone"))
             {
                 slotData.bone = slotObj["bone"].ToString();
             }
             if (slotObj.ContainKey("color"))
             {
                 slotData.color = SpineArmatureEditor.HexToColor(slotObj["color"].ToString());
             }
             if (slotObj.ContainKey("attachment"))
             {
                 slotData.attachment = slotObj["attachment"].ToString();
             }
             if (slotObj.ContainKey("blend"))
             {
                 slotData.blend = slotObj["blend"].ToString();
             }
             slotData.index = i;
             slotDatas[i]   = slotData;
             armatureEditor.slotsDataKV[slotData.name] = slotData;
         }
         armatureEditor.armatureData.slots = slotDatas;
     }
 }
Ejemplo n.º 3
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;
                }
            }
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
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;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        static void CreateAnimSlot(SpineArmatureEditor armatureEditor, AnimationClip clip, SpineData.AnimationSlotData[] animSlotDatas)
        {
            for (int i = 0; i < animSlotDatas.Length; ++i)
            {
                SpineData.AnimationSlotData animSlotData = animSlotDatas[i];
                string             slotName        = animSlotData.name;
                Transform          slot            = armatureEditor.slotsKV[slotName];
                SpineData.SlotData defaultSlotData = armatureEditor.slotsDataKV[slotName];
                Color defaultColorData             = defaultSlotData.color;

                AnimationCurve color_rcurve = new AnimationCurve();
                AnimationCurve color_gcurve = new AnimationCurve();
                AnimationCurve color_bcurve = new AnimationCurve();
                AnimationCurve color_acurve = new AnimationCurve();

                AnimationCurve display_curve = new AnimationCurve();

                bool isHaveCurve = false;
                for (int j = 0; j < animSlotData.timelines.Length; ++j)
                {
                    SpineData.SlotTimeline timeline = animSlotData.timelines[j];
                    string  prevTweeneasing         = "linear";           //前一帧的tweenEasing
                    float[] prevCurves = null;
                    if (j > 0)
                    {
                        prevTweeneasing = animSlotData.timelines[j - 1].tweenEasing;
                        prevCurves      = animSlotData.timelines[j - 1].curve;
                    }
                    TangentMode tanModeL = GetPrevFrameTangentMode(prevTweeneasing, prevCurves);
                    TangentMode tanModeR = TangentMode.Linear;

                    if (timeline.curve != null && timeline.curve.Length > 0)
                    {
                        tanModeR    = TangentMode.Editable;
                        isHaveCurve = true;
                    }
                    else
                    {
                        if (timeline.tweenEasing == "stepped")
                        {
                            tanModeR = TangentMode.Stepped;
                        }
                        else
                        {
                            tanModeR = TangentMode.Linear;
                        }
                    }

                    if (timeline.type == "color")
                    {
                        if (!string.IsNullOrEmpty(timeline.color))
                        {
                            Color c = SpineArmatureEditor.HexToColor(timeline.color);
                            color_rcurve.AddKey(KeyframeUtil.GetNew(timeline.time, c.r, tanModeL, tanModeR));
                            color_gcurve.AddKey(KeyframeUtil.GetNew(timeline.time, c.g, tanModeL, tanModeR));
                            color_bcurve.AddKey(KeyframeUtil.GetNew(timeline.time, c.b, tanModeL, tanModeR));
                            color_acurve.AddKey(KeyframeUtil.GetNew(timeline.time, c.a, tanModeL, tanModeR));                         //*defaultColorData.a
                        }
                        else if (color_acurve.length > 0)
                        {
                            color_rcurve.AddKey(KeyframeUtil.GetNew(timeline.time, defaultColorData.r, tanModeL, tanModeR));
                            color_gcurve.AddKey(KeyframeUtil.GetNew(timeline.time, defaultColorData.g, tanModeL, tanModeR));
                            color_bcurve.AddKey(KeyframeUtil.GetNew(timeline.time, defaultColorData.b, tanModeL, tanModeR));
                            color_acurve.AddKey(KeyframeUtil.GetNew(timeline.time, defaultColorData.a, tanModeL, tanModeR));
                        }
                    }
                    else if (timeline.type == "attachment")
                    {
                        if (string.IsNullOrEmpty(timeline.attachmentName))
                        {
                            display_curve.AddKey(new Keyframe(timeline.time, -1f, float.PositiveInfinity, float.PositiveInfinity));
                        }
                        else
                        {
                            for (int r = 0; r < slot.childCount; ++r)
                            {
                                if (slot.GetChild(r).name.Equals(timeline.attachmentName))
                                {
                                    display_curve.AddKey(new Keyframe(timeline.time, r, float.PositiveInfinity, float.PositiveInfinity));
                                    break;
                                }
                            }
                        }
                    }
                }
                CurveExtension.OptimizesCurve(color_rcurve);
                CurveExtension.OptimizesCurve(color_gcurve);
                CurveExtension.OptimizesCurve(color_bcurve);
                CurveExtension.OptimizesCurve(color_acurve);
                CurveExtension.OptimizesCurve(display_curve);

                string path = "";
                if (slotPathKV.ContainsKey(slot.name))
                {
                    path = slotPathKV[slot.name];
                }
                else
                {
                    path = GetNodeRelativePath(armatureEditor, slot);
                    slotPathKV[slot.name] = path;
                }

                SetColorCurve <Slot>(path, clip, color_rcurve, "color.r", isHaveCurve, defaultColorData.r, animSlotData.timelines);
                SetColorCurve <Slot>(path, clip, color_gcurve, "color.g", isHaveCurve, defaultColorData.g, animSlotData.timelines);
                SetColorCurve <Slot>(path, clip, color_bcurve, "color.b", isHaveCurve, defaultColorData.b, animSlotData.timelines);
                SetColorCurve <Slot>(path, clip, color_acurve, "color.a", isHaveCurve, defaultColorData.a, animSlotData.timelines);

                //add pose
                AnimationCurve pose_color_rcurve = new AnimationCurve();
                AnimationCurve pose_color_gcurve = new AnimationCurve();
                AnimationCurve pose_color_bcurve = new AnimationCurve();
                AnimationCurve pose_color_acurve = new AnimationCurve();
                pose_color_rcurve.AddKey(new Keyframe(0f, defaultColorData.r));
                pose_color_gcurve.AddKey(new Keyframe(0f, defaultColorData.g));
                pose_color_bcurve.AddKey(new Keyframe(0f, defaultColorData.b));
                pose_color_acurve.AddKey(new Keyframe(0f, defaultColorData.a));
                AnimationUtility.SetEditorCurve(poseClip, EditorCurveBinding.FloatCurve(path, typeof(Slot), "color.r"), pose_color_rcurve);
                AnimationUtility.SetEditorCurve(poseClip, EditorCurveBinding.FloatCurve(path, typeof(Slot), "color.g"), pose_color_gcurve);
                AnimationUtility.SetEditorCurve(poseClip, EditorCurveBinding.FloatCurve(path, typeof(Slot), "color.b"), pose_color_bcurve);
                AnimationUtility.SetEditorCurve(poseClip, EditorCurveBinding.FloatCurve(path, typeof(Slot), "color.a"), pose_color_acurve);

                if (display_curve.keys != null && display_curve.keys.Length > 0 &&
                    CheckCurveValid(display_curve, slot.GetComponent <Slot>().displayIndex))
                {
                    clip.SetCurve(path, typeof(Slot), "m_DisplayIndex", display_curve);
                    //add pose
                    AnimationCurve pose_display_curve = new AnimationCurve();
                    pose_display_curve.AddKey(new Keyframe(0f, slot.GetComponent <Slot>().displayIndex));
                    AnimationUtility.SetEditorCurve(poseClip, EditorCurveBinding.FloatCurve(path, typeof(Slot), "m_DisplayIndex"), pose_display_curve);
                }
            }
        }