public static void Load(ref ScriptKeys sKeys, ref List<BoneCurves> rBC, string file)
    {
        Debug.Log("loading");
        XmlDocument doc = new XmlDocument();
        doc.Load(file);
        XmlNode AnimeName = doc.SelectSingleNode("AnimeName");
        sKeys.AnimeName = AnimeName.Attributes["Name"].Value;
        sKeys.ID = int.Parse(AnimeName.Attributes["ID"].Value);
        foreach (XmlNode node in AnimeName.ChildNodes)
        {
            switch (node.Name)
            {
                case "Windom_TopScript":
                    sKeys.TopScript = node.InnerText;
                    break;
                case "Time":
                    scriptKey key;
                    key.Frame = int.Parse(node.Attributes["Value"].Value);
                    key.script = node.FirstChild.InnerText;
                    sKeys.SK.Add(key);
                    break;
                case "BoneData":
                    string curvetype = "";
                    XmlNode BoneData = node;

                    foreach (XmlNode Bone in BoneData.ChildNodes)
                    {
                        BoneCurves BC = new BoneCurves { name = Bone.Attributes["Text"].Value };
                        //Debug.Log(Bone.Name);

                        XmlNodeList childs = Bone.ChildNodes;
                        for (int i = 0; i < childs.Count; i++)
                        {
                            switch (childs[i].Name)
                            {
                                case "RotateKey":
                                    curvetype = "RotateKey";
                                    break;
                                case "ScaleKey":
                                    curvetype = "Scalekey";
                                    break;
                                case "PosKey":
                                    curvetype = "PosKey";
                                    break;
                                case "Time":
                                    switch (curvetype)
                                    {
                                        case "RotateKey":
                                            RotPoint RP = new RotPoint();
                                            RP.Frame = int.Parse(childs[i].Attributes["Value"].Value);
                                            XmlNode Rota = childs[i].ChildNodes[0];
                                            RP.Value = new Quaternion(float.Parse(Rota.Attributes["x"].Value),
                                                float.Parse(Rota.Attributes["y"].Value),
                                                float.Parse(Rota.Attributes["z"].Value),
                                                float.Parse(Rota.Attributes["w"].Value));
                                            RP.calctype = int.Parse(childs[i].ChildNodes[1].Attributes["Value"].Value);
                                            if (RP.calctype == 1)
                                                RP.PowVal = int.Parse(childs[i].ChildNodes[2].Attributes["Value"].Value);
                                            BC.RotCurve.Add(RP);

                                            break;
                                        case "ScaleKey":
                                            ScalePoint SP = new ScalePoint();
                                            SP.Frame = int.Parse(childs[i].Attributes["Value"].Value);
                                            XmlNode Scale = childs[i].ChildNodes[0];
                                            SP.Value = new Vector3(float.Parse(Scale.Attributes["x"].Value),
                                                float.Parse(Scale.Attributes["y"].Value),
                                                float.Parse(Scale.Attributes["z"].Value));
                                            SP.calctype = int.Parse(childs[i].ChildNodes[1].Attributes["Value"].Value);
                                            if (SP.calctype == 1)
                                                SP.PowVal = int.Parse(childs[i].ChildNodes[2].Attributes["Value"].Value);

                                            BC.ScaleCurve.Add(SP);

                                            break;
                                        case "PosKey":
                                            PosPoint PP = new PosPoint();
                                            PP.Frame = int.Parse(childs[i].Attributes["Value"].Value);
                                            XmlNode Pos = childs[i].ChildNodes[0];
                                            PP.Value = new Vector3(float.Parse(Pos.Attributes["x"].Value),
                                                float.Parse(Pos.Attributes["y"].Value),
                                                float.Parse(Pos.Attributes["z"].Value));
                                            PP.calctype = int.Parse(childs[i].ChildNodes[1].Attributes["Value"].Value);
                                            if (PP.calctype == 1)
                                                PP.PowVal = int.Parse(childs[i].ChildNodes[2].Attributes["Value"].Value);

                                            BC.PosCurve.Add(PP);

                                            break;
                                    }

                                    break;
                            }
                        }
                        rBC.Add(BC);
                    }
                    break;
            }
        }
    }
    public static List<BoneCurves> Load(string file)
    {
        Debug.Log("loading");
        List<BoneCurves> BClist = new List<BoneCurves>();
        XmlDocument doc = new XmlDocument();
        doc.Load(file);
        string curvetype = "";
        XmlNode BoneData = doc.SelectSingleNode("AnimeName/BoneData");

        foreach (XmlNode Bone in BoneData.ChildNodes)
        {
            int keyvalue = 0;
            BoneCurves BC = new BoneCurves { name = Bone.Attributes["Text"].Value };
            //Debug.Log(Bone.Name);

            XmlNodeList childs = Bone.ChildNodes;
            for (int i = 0; i < childs.Count; i++)
            {
                switch (childs[i].Name)
                {
                    case "RotateKey":
                        curvetype = "RotateKey";
                        break;
                    case "ScaleKey":
                        curvetype = "Scalekey";
                        break;
                    case "PosKey":
                        curvetype = "PosKey";
                        break;
                    case "Time":
                        switch (curvetype)
                        {
                            case "RotateKey":
                                RotPoint RP = new RotPoint();
                                RP.Frame = int.Parse(childs[i].Attributes["Value"].Value);
                                XmlNode Rota = childs[i].ChildNodes[0];
                                RP.Value = new Quaternion(float.Parse(Rota.Attributes["x"].Value),
                                    float.Parse(Rota.Attributes["y"].Value),
                                    float.Parse(Rota.Attributes["z"].Value),
                                    float.Parse(Rota.Attributes["w"].Value));
                                RP.calctype = int.Parse(childs[i].ChildNodes[1].Attributes["Value"].Value);
                                if (RP.calctype == 1)
                                    RP.PowVal = int.Parse(childs[i].ChildNodes[2].Attributes["Value"].Value);
                                BC.RotCurve.Add(RP);

                                break;
                            case "ScaleKey":
                                ScalePoint SP = new ScalePoint();
                                SP.Frame = int.Parse(childs[i].Attributes["Value"].Value);
                                XmlNode Scale = childs[i].ChildNodes[0];
                                SP.Value = new Vector3(float.Parse(Scale.Attributes["x"].Value),
                                    float.Parse(Scale.Attributes["y"].Value),
                                    float.Parse(Scale.Attributes["z"].Value));
                                SP.calctype = int.Parse(childs[i].ChildNodes[1].Attributes["Value"].Value);
                                if (SP.calctype == 1)
                                    SP.PowVal = int.Parse(childs[i].ChildNodes[2].Attributes["Value"].Value);

                                BC.ScaleCurve.Add(SP);

                                break;
                            case "PosKey":
                                PosPoint PP = new PosPoint();
                                PP.Frame = int.Parse(childs[i].Attributes["Value"].Value);
                                XmlNode Pos = childs[i].ChildNodes[0];
                                PP.Value = new Vector3(float.Parse(Pos.Attributes["x"].Value),
                                    float.Parse(Pos.Attributes["y"].Value),
                                    float.Parse(Pos.Attributes["z"].Value));
                                PP.calctype = int.Parse(childs[i].ChildNodes[1].Attributes["Value"].Value);
                                if (PP.calctype == 1)
                                    PP.PowVal = int.Parse(childs[i].ChildNodes[2].Attributes["Value"].Value);

                                BC.PosCurve.Add(PP);

                                break;
                        }

                        break;
                }
            }
            BClist.Add(BC);
        }
        return BClist;
    }
    void GUI_BoneMenu()
    {
        //content box
        GUI.Box(new Rect(10, Screen.height / 2 - 200, 275, 400), "Animation Editor");

        //transform data
        GUI.Label(new Rect(30, Screen.height / 2 - 180, 50, 20), elist[editmode]);
        GUI.Label(new Rect(30, Screen.height / 2 - 160, 20, 20), "  X");
        PositionText.x = GUI.TextField(new Rect(50, Screen.height / 2 - 160, 50, 20), PositionText.x);
        GUI.Label(new Rect(100, Screen.height / 2 - 160, 20, 20), "  Y");
        PositionText.y = GUI.TextField(new Rect(120, Screen.height / 2 - 160, 50, 20), PositionText.y);
        GUI.Label(new Rect(170, Screen.height / 2 - 160, 20, 20), "  Z");
        PositionText.z = GUI.TextField(new Rect(190, Screen.height / 2 - 160, 50, 20), PositionText.z);
        editmode = GUI.SelectionGrid(new Rect(30, Screen.height / 2 - 130, 235, 20), editmode, elist, 3);

        if (editmode != editModeChange)
        { UpdateTransformValues(); UpdateCalcType(); }
        editModeChange = editmode;

        ApplyTransformValues();
        try
        {
            if (selection == 1)
            {
                GUI.Box(new Rect(30, Screen.height / 2 - 100, 235, 205), RB.parts[selectedPiece].name + " Frames");
                GUI_Framelist();
                GUI.Label(new Rect(35, Screen.height / 2 + 115, 60, 20), "CalcType");
                calctype = GUI.TextField(new Rect(95, Screen.height / 2 + 115, 50, 20), calctype);
                GUI.Label(new Rect(160, Screen.height / 2 + 115, 60, 20), "PowVal");
                powval = GUI.TextField(new Rect(210, Screen.height / 2 + 115, 50, 20), powval);
                ApplyCalcType();
                if (GUI.Button(new Rect(30, Screen.height / 2 + 140, 116, 20), "Add"))
                {
                    bool hasFrames = false;
                    for (int i = 0; i < RB.BC.Count; i++)
                    {
                        if (RB.BC[i].GO == RB.parts[selectedPiece])
                        { hasFrames = true;   break; }

                    }

                    if (!hasFrames)
                    {
                        
                        RB.BC.Add(new BoneCurves());
                        indexBC = RB.BC.Count - 1;
                        RB.BC[indexBC].GO = RB.parts[selectedPiece];
                        RB.BC[indexBC].name = RB.parts[selectedPiece].name;
                       
                        Debug.Log("test");
                    }

                    switch (elist[editmode])
                    {
                        case "Position":
                            if (RB.BC[indexBC].PosCurve.Count == 0)
                            {
                                PosPoint PP = new PosPoint();
                                PP.Frame = 0;
                                PP.Value = Vector3.zero;
                                PP.calctype = 0;
                                PP.PowVal = 1;
                                RB.BC[indexBC].PosCurve.Add(PP);
                            }
                            else
                            {
                                if (!isFrameOccupied(Mathf.RoundToInt(FrameSlider)))
                                {

                                    int newFramePos = Mathf.RoundToInt(FrameSlider);
                                    int insertFrame = -1;
                                    for (int i = 0; i < RB.BC[indexBC].PosCurve.Count; i++)
                                    {
                                        if (RB.BC[indexBC].PosCurve[i].Frame > newFramePos && (insertFrame == -1 || RB.BC[indexBC].PosCurve[i].Frame < RB.BC[indexBC].PosCurve[insertFrame].Frame))
                                            insertFrame = i;
                                    }

                                    if (insertFrame == -1)
                                    {
                                        PosPoint FPP = RB.BC[indexBC].PosCurve[RB.BC[indexBC].PosCurve.Count - 1];
                                        FPP.Frame = newFramePos;
                                        RB.BC[indexBC].PosCurve.Add(FPP);
                                    }
                                    else
                                    {
                                        PosPoint FPP = RB.BC[indexBC].PosCurve[insertFrame - 1];
                                        FPP.Frame = newFramePos;
                                        RB.BC[indexBC].PosCurve.Insert(insertFrame, FPP);
                                    }
                                }

                            }
                            break;
                        case "Rotation":
                            if (RB.BC[indexBC].RotCurve.Count == 0)
                            {
                                RotPoint RP = new RotPoint();
                                RP.Frame = 0;
                                RP.Value = Quaternion.identity;
                                RP.calctype = 0;
                                RP.PowVal = 1;
                                RB.BC[indexBC].RotCurve.Add(RP);
                            }
                            else
                            {
                                if (!isFrameOccupied(Mathf.RoundToInt(FrameSlider)))
                                {

                                    int newFramePos = Mathf.RoundToInt(FrameSlider);
                                    int insertFrame = -1;
                                    for (int i = 0; i < RB.BC[indexBC].RotCurve.Count; i++)
                                    {
                                        if (RB.BC[indexBC].RotCurve[i].Frame > newFramePos && (insertFrame == -1 || RB.BC[indexBC].RotCurve[i].Frame < RB.BC[indexBC].RotCurve[insertFrame].Frame))
                                            insertFrame = i;
                                    }

                                    if (insertFrame == -1)
                                    {
                                        RotPoint FPP = RB.BC[indexBC].RotCurve[RB.BC[indexBC].RotCurve.Count - 1];
                                        FPP.Frame = newFramePos;
                                        RB.BC[indexBC].RotCurve.Add(FPP);
                                    }
                                    else
                                    {
                                        RotPoint FPP = RB.BC[indexBC].RotCurve[insertFrame - 1];
                                        FPP.Frame = newFramePos;
                                        RB.BC[indexBC].RotCurve.Insert(insertFrame, FPP);
                                    }
                                }

                            }
                            break;
                        case "Scale":
                            if (RB.BC[indexBC].ScaleCurve.Count == 0)
                            {
                                ScalePoint SP = new ScalePoint();
                                SP.Frame = 0;
                                SP.Value = Vector3.zero;
                                SP.calctype = 0;
                                SP.PowVal = 1;
                                RB.BC[indexBC].ScaleCurve.Add(SP);
                            }
                            else
                            {
                                if (!isFrameOccupied(Mathf.RoundToInt(FrameSlider)))
                                {

                                    int newFramePos = Mathf.RoundToInt(FrameSlider);
                                    int insertFrame = -1;
                                    for (int i = 0; i < RB.BC[indexBC].ScaleCurve.Count; i++)
                                    {
                                        if (RB.BC[indexBC].ScaleCurve[i].Frame > newFramePos && (insertFrame == -1 || RB.BC[indexBC].ScaleCurve[i].Frame < RB.BC[indexBC].ScaleCurve[insertFrame].Frame))
                                            insertFrame = i;
                                    }

                                    if (insertFrame == -1)
                                    {
                                        ScalePoint FPP = RB.BC[indexBC].ScaleCurve[RB.BC[indexBC].ScaleCurve.Count - 1];
                                        FPP.Frame = newFramePos;
                                        RB.BC[indexBC].ScaleCurve.Add(FPP);
                                    }
                                    else
                                    {
                                        ScalePoint FPP = RB.BC[indexBC].ScaleCurve[insertFrame - 1];
                                        FPP.Frame = newFramePos;
                                        RB.BC[indexBC].ScaleCurve.Insert(insertFrame, FPP);
                                    }
                                }

                            }
                            break;
                    }
                }
                if (GUI.Button(new Rect(150, Screen.height / 2 + 140, 115, 20), "Remove"))
                {
                    switch (elist[editmode])
                    {
                        case "Position":
                            RB.BC[indexBC].PosCurve.RemoveAt(selectedFrame);
                            break;
                        case "Rotation":
                            RB.BC[indexBC].RotCurve.RemoveAt(selectedFrame);
                            break;
                        case "Scale":
                            RB.BC[indexBC].ScaleCurve.RemoveAt(selectedFrame);
                            break;
                    }
                }

                if (selectedFrame != prevSelectedFrame)
                    RB.AnimeFrameGo(frameNum);
                prevSelectedFrame = selectedFrame;
                UpdateTransformValues();
                if (checkIfEmpty())
                    indexBC = 0;
            }
            else
            {
                GUI.Box(new Rect(30, Screen.height / 2 - 100, 235, 260), "Parts");
                string[] Piecelist = listModels();
                ScrollPositions[1] = GUI.BeginScrollView(new Rect(30, Screen.height / 2 - 75, 235, 235), ScrollPositions[1], new Rect(0, 0, 219, Piecelist.Length * 25));
                try
                {
                    selectedPiece = GUI.SelectionGrid(new Rect(0, 0, 219, (float)25 * (float)Piecelist.Length), selectedPiece, Piecelist, 1);
                }
                catch { }
                GUI.EndScrollView();
            }
        }
        catch { }
        selection = GUI.SelectionGrid(new Rect(30, Screen.height / 2 + 165, 235, 20), selection, slist, 2);

        if (selection != prevselection)
            UpdateCalcType();
        prevselection = selection;

    }