Exemplo n.º 1
0
        private static SpineData.AnimationSlotData ParseSlotAnimTimeline(string slotName, Bones2D.JSONClass slotAnimObj, string animType)
        {
            Bones2D.JSONArray animObjArr = slotAnimObj[animType].AsArray;

            SpineData.AnimationSlotData spineAnimSlotData = new SpineData.AnimationSlotData();
            spineAnimSlotData.name      = slotName;
            spineAnimSlotData.timelines = new SpineData.SlotTimeline[animObjArr.Count];
            for (int i = 0; i < animObjArr.Count; ++i)
            {
                Bones2D.JSONClass      animObj  = animObjArr[i].AsObject;
                SpineData.SlotTimeline timeline = new SpineData.SlotTimeline();
                timeline.type = animType;
                spineAnimSlotData.timelines[i] = timeline;

                if (animObj.ContainKey("time"))
                {
                    timeline.time = animObj["time"].AsFloat;
                }
                if (animObj.ContainKey("name"))
                {
                    timeline.attachmentName = animObj["name"].ToString();                    //attachment name
                    if (timeline.attachmentName == "null")
                    {
                        timeline.attachmentName = null;
                    }
                }
                if (animObj.ContainKey("color"))
                {
                    timeline.color = animObj["color"].ToString();                                            //Keyframes for changing a slot's color.
                }
                if (animObj.ContainKey("curve"))
                {
                    if (animObj["curve"] == "stepped")
                    {
                        timeline.tweenEasing = "stepped";
                    }
                    else if (animObj["curve"] == "linear")
                    {
                        //default
                    }
                    else
                    {
                        timeline.curve = ConvertJsonArrayToFloatArr(animObj["curve"].AsArray);
                    }
                }
            }
            return(spineAnimSlotData);
        }
Exemplo n.º 2
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);
                }
            }
        }