示例#1
0
        /// <summary>
        /// Gets a JSON object of the prefab.
        /// </summary>
        /// <param name="flags">Configuration flags to configure the output of the prefab builder.</param>
        /// <returns>A JSONNode object</returns>
        public JSONNode ToJson(PrefabBuildFlags flags = PrefabBuildFlags.None)
        {
            JSONObject json = new JSONObject();

            json["name"]   = Name;
            json["type"]   = ((int)Type).ToString();
            json["offset"] = Offset.ToString();

            PrefabObject[] prefabObjects = new PrefabObject[PrefabObjects.Count];
            PrefabObjects.Values.CopyTo(prefabObjects, 0);

            if ((flags & PrefabBuildFlags.SortObjects) != 0)
            {
                Array.Sort(prefabObjects, (x, y) => x.StartTime.CompareTo(y.StartTime));
            }

            foreach (PrefabObject obj in prefabObjects)
            {
                json["objects"].Add(obj.ToJson(flags));
            }

            return(json);
        }
示例#2
0
        public JSONNode ToJson(PrefabBuildFlags flags)
        {
            JSONObject json = new JSONObject();

            json["name"]  = Name;
            json["id"]    = ID;
            json["p"]     = ParentID;
            json["pt"]    = (PositionParenting ? "1" : "0") + (ScaleParenting ? "1" : "0") + (RotationParenting ? "1" : "0");
            json["po"][0] = PositionParentOffset.ToString();
            json["po"][1] = ScaleParentOffset.ToString();
            json["po"][2] = RotationParentOffset.ToString();
            json["d"]     = RenderDepth.ToString();
            json["ot"]    = ((int)ObjectType).ToString();
            json["shape"] = ((int)Shape).ToString();
            json["so"]    = ShapeOption.ToString();

            if (Shape == PrefabObjectShape.Text)
            {
                json["text"] = Text;
            }

            json["st"]           = StartTime.ToString();
            json["akt"]          = ((int)AutoKillType).ToString();
            json["ako"]          = AutoKillOffset.ToString();
            json["o"]["x"]       = Origin.X;
            json["o"]["y"]       = Origin.Y;
            json["ed"]["locked"] = EditorLocked.ToString();
            json["ed"]["shrink"] = EditorCollapse.ToString();
            json["ed"]["bin"]    = EditorBin.ToString();
            json["ed"]["layer"]  = EditorLayer.ToString();

            if ((flags & PrefabBuildFlags.SortKeyframes) != 0)
            {
                PositionKeyframes.Sort((x, y) => x.Time.CompareTo(y.Time));
                ScaleKeyframes.Sort((x, y) => x.Time.CompareTo(y.Time));
                ColorKeyframes.Sort((x, y) => x.Time.CompareTo(y.Time));
            }

            if ((flags & (PrefabBuildFlags.AbsoluteRotation | PrefabBuildFlags.SortKeyframes)) != 0)
            {
                RotationKeyframes.Sort((x, y) => x.Time.CompareTo(y.Time));
            }

            json["events"]["pos"] = new JSONArray();
            for (int i = 0; i < PositionKeyframes.Count; i++)
            {
                JSONNode         kfJson = json["events"]["pos"][i] = new JSONObject();
                PositionKeyframe kf     = PositionKeyframes[i];

                kfJson["t"]  = kf.Time.ToString();
                kfJson["x"]  = kf.Value.X.ToString();
                kfJson["y"]  = kf.Value.Y.ToString();
                kfJson["ct"] = kf.Easing.ToString();

                if (kf.RandomMode != PrefabObjectRandomMode.None)
                {
                    kfJson["r"]  = ((int)kf.RandomMode).ToString();
                    kfJson["rx"] = kf.RandomValue.X.ToString();
                    kfJson["ry"] = kf.RandomValue.Y.ToString();
                    kfJson["rz"] = kf.RandomInterval.ToString();
                }
            }

            json["events"]["sca"] = new JSONArray();
            for (int i = 0; i < ScaleKeyframes.Count; i++)
            {
                JSONNode      kfJson = json["events"]["sca"][i] = new JSONObject();
                ScaleKeyframe kf     = ScaleKeyframes[i];

                kfJson["t"]  = kf.Time.ToString();
                kfJson["x"]  = kf.Value.X.ToString();
                kfJson["y"]  = kf.Value.Y.ToString();
                kfJson["ct"] = kf.Easing.ToString();

                if (kf.RandomMode != PrefabObjectRandomMode.None)
                {
                    kfJson["r"]  = ((int)kf.RandomMode).ToString();
                    kfJson["rx"] = kf.RandomValue.X.ToString();
                    kfJson["ry"] = kf.RandomValue.Y.ToString();
                    kfJson["rz"] = kf.RandomInterval.ToString();
                }
            }

            json["events"]["rot"] = new JSONArray();
            float currentRot = 0.0f;

            for (int i = 0; i < RotationKeyframes.Count; i++)
            {
                JSONNode         kfJson = json["events"]["rot"][i] = new JSONObject();
                RotationKeyframe kf     = RotationKeyframes[i];

                kfJson["t"]  = kf.Time.ToString();
                kfJson["x"]  = (kf.Value - currentRot).ToString();
                kfJson["ct"] = kf.Easing.ToString();

                if (kf.RandomMode != PrefabObjectRandomMode.None)
                {
                    kfJson["r"]  = ((int)kf.RandomMode).ToString();
                    kfJson["rx"] = (kf.RandomValue - currentRot).ToString();
                    kfJson["rz"] = kf.RandomInterval.ToString();
                }

                if ((flags & PrefabBuildFlags.AbsoluteRotation) != 0)
                {
                    currentRot += kf.Value;
                }
            }

            json["events"]["col"] = new JSONArray();
            for (int i = 0; i < ColorKeyframes.Count; i++)
            {
                JSONNode      kfJson = json["events"]["col"][i] = new JSONObject();
                ColorKeyframe kf     = ColorKeyframes[i];

                kfJson["t"]  = kf.Time.ToString();
                kfJson["x"]  = kf.Value.ToString();
                kfJson["ct"] = kf.Easing.ToString();
            }

            return(json);
        }
示例#3
0
 /// <summary>
 /// Writes the prefab to a file.
 /// </summary>
 /// <param name="path">A file path</param>
 /// <param name="flags">Build flags</param>
 public void ExportToFile(string path, PrefabBuildFlags flags = PrefabBuildFlags.None)
 {
     File.WriteAllText(path, ToJson(flags).ToString());
 }